Spring introduction learning notes (1.07) -- use spring factorybean to create Bean

Source: Internet
Author: User

I. knowledge points

Factory Bean is a bean used as a factory for creating other beans in the IOC container. In concept, factory beans are similar to factory methods, but they are spring proprietary beans that can be recognized by Spring IoC containers during bean construction.

The basic requirement of factory beans is to implement the factorybean interface. For convenience, Spring provides the abstract template class abstractfactorybean for your extension. Factory beans are mainly used to implement the Framework mechanism. The following are some examples:

(1) jndiobjectfactorybean can be used to search for objects (such as a data source) in JNDI.

(2) When using classic Spring AOP to create a proxy for a bean, you can use proxyfactorybean.

(3) when creating a hibernate session factory in the IOC container, localsessionfactorybean can be used.


Factory beans are framework-specific and cannot be used outside the Spring IoC container.

Ii. Sample Code

(1) create a product with applicable price discounts and compile a factory bean.

/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springioc; import Org. springframework. beans. factory. config. abstractfactorybean;/*** title: discountfactorybean. java class function description ** @ author Jackie * @ since APR 19,201 3 9:45:00 * @ version V1.0 */@ suppresswarnings ("rawtypes ") public class discountfactorybean extends abstractfactorybean {private product; private double discount; @ override public class getobjecttype () {return product. getclass () ;}@ override protected object createinstance () throws exception {product. setprice (product. getprice () * (1-discount); return product;}/*** @ Param product * the product to set */Public void setproduct (product) {This. product = product;}/*** @ Param Discount * The discount to set */Public void setdiscount (double discount) {This. discount = discount ;}}

By inheriting the abstractfactorybean class, your factory bean can overload the createinstance () method to create the target bean instance. In addition, you must return the type of the target bean in the getobjecttype () method. The auto-wiring function works properly.

(2) bean Configuration

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">     <bean id="aaa" class="com.jackie.codeproject.springrecipesnote.springioc.DiscountFactoryBean"><property name="product">   <bean class="com.jackie.codeproject.springrecipesnote.springioc.Battery">      <constructor-arg value="AAA" />      <constructor-arg value="2.5" />   </bean></property><property name="discount" value="0.2" />     </bean>     <bean id="cdrw" class="com.jackie.codeproject.springrecipesnote.springioc.DiscountFactoryBean"><property name="product">   <bean class="com.jackie.codeproject.springrecipesnote.springioc.Disc">      <constructor-arg value="CD-RW" />      <constructor-arg value="1.5" />           </bean></property><property name="discount" value="0.1" />     </bean></beans>

(3) product class

/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springioc;/*** title: product. java * product abstract class ** @ author Jackie * @ since APR 14,201 3 8:09:49 * @ version V1.0 */public abstract class product {private string name; private double price; Public Product () {}/*** <p> title: </P> * <p> constructor with parameters </P> * @ Param name * @ Param price */Public Product (string name, double price) {This. name = Name; this. price = price;}/*** @ return the name */Public String getname () {return name ;} /*** @ Param name the name to set */Public void setname (string name) {This. name = Name;}/*** @ return the price */Public double getprice () {return price ;} /*** @ Param price the price to set */Public void setprice (double price) {This. price = price;} Public String tostring () {return name + "" + price ;}}

(4) battery class

/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springioc;/*** title: Battery. java class function description ** @ author Jackie * @ since APR 14,201 3 8:14:26 * @ version V1.0 */public class battery extends product {private Boolean rechargeable; Public battery () {super ();} public battery (string name, double price) {super (name, price);}/*** @ return the rechargeable */Public Boolean isrechargeable () {return rechargeable;}/*** @ Param rechargeable * The Rechargeable to set */Public void setrechargeable (Boolean rechargeable) {This. rechargeable = rechargeable ;}}

(5) Disc class

/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springioc;/*** title: disc. java class function description ** @ author Jackie * @ since APR 14,201 3 8:18:30 * @ version V1.0 */public class disc extends product {private int capacity; Public disc () {super ();} public disc (string name, double price) {super (name, price);}/*** @ return the capacity */Public int getcapacity () {return capacity;}/*** @ Param capacity * the capacity to set */Public void setcapacity (INT capacity) {This. capacity = capacity ;}}

(6) test class producttest

Package COM. jackie. codeproject. springrecipesnote. springioc; import Org. JUnit. after; import Org. JUnit. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; public class producttest {private applicationcontext; @ test public void testclasspathxmlapplicationcontext () {// use classpathxmlapplicationcontext to be AP An Implementation of the plicationcontext interface. It can load an xml configuration file applicationcontext = new classpathxmlapplicationcontext ("applicationcontext. XML "); // use the getbean method and pass the unique bean name configured for it. Its return type is object, which must be forcibly transformed. Product AAA = (product) applicationcontext. getbean ("AAA"); system. out. println (AAA. tostring (); product cdrw = (product) applicationcontext. getbean ("cdrw"); system. out. println (cdrw. tostring () ;}@ after public void after () {If (null! = Applicationcontext) {applicationcontext = NULL ;}}}

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.