Spring FactoryBean configuration Bean

Source: Internet
Author: User

Summary:



Instance code details: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + xL/CvL3hubk8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140607/20140607091120354.png" alt = "\">


Car. java

package com.coslay.beans.factorybean;public class Car {private String brand;private double price;public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Car [brand=" + brand + ", price=" + price + "]";}public Car() {System.out.println("Car's Constructor...");}public Car(String brand, double price) {super();this.brand = brand;this.price = price;}}


CarFactoryBean. java

Package com. coslay. beans. factorybean; import org. springframework. beans. factory. FactoryBean; // you need to implement the FactoryBean interface public class CarFactoryBean implements FactoryBean
 
  
{Private String brand; public void setBrand (String brand) {this. brand = brand ;}// return the bean object @ Overridepublic Car getObject () throws Exception {return new Car ("BMW", 5000000 );} // return the bean type @ Overridepublic Class
  GetObjectType () {return Car. class ;}@ Overridepublic boolean isSingleton () {return true ;}}
 


Main. java

package com.coslay.beans.factorybean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-beanfactory.xml");Car car = (Car) ctx.getBean("car");System.out.println(car);}}


Beans-beanfactory.xml

 
 
  
  
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.