Introduction to Spring Factorybean mechanism

Source: Internet
Author: User

Frontier:

Before you say spring, mention spring's rationale, dependency injection, and control inversion. There are two types of beans that we can configure when we configure Spring,

One is a normal bean: The configured class is the real object of the generated bean.

One is the factory bean, Factorybean: The configuration class is not the real object that generates the bean.

They're all managed by the spring container. The object returned by the factory Bean is not an instance of the specified class, but is the object returned by the GetObject method of the Factorybean.

Ordinary people may know only one, within spring, there are many places to use the Factorybean implementation function,

Applications: They are embodied in many applications such as (Spring AOP, ORM, transaction management ) and integration with other third frameworks (EhCache).

Realize:

1. Factorybean interface

1  Packageorg.springframework.beans.factory;2 3  Public InterfaceFactorybean<t> {4 5T GetObject ()throwsException;6     7Class<?>Getobjecttype ();8  9     BooleanIssingleton ();Ten  One}

This is the Java generic mechanism implementation, that is, T represents the object to be built. can also not use, recommended to use.
The GetObject () method returns the object that you want to build. Getobjecttype () To return the object type, Issingleton () Gets the object's mechanism (singleton|prototype|request|session).

2. Example: MyBatis and Spring integration examples

1  Packagecom.debug.factory;2 3 ImportJava.io.InputStream;4 5 Importorg.apache.ibatis.io.Resources;6 Importorg.apache.ibatis.session.SqlSessionFactory;7 ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;8 ImportOrg.springframework.beans.factory.FactoryBean;9 Ten  Public classSqlsessionfactorybeanImplementsFactorybean<sqlsessionfactory> { One sqlsessionfactory sessionfactory; A  - @Override -      PublicSqlsessionfactory GetObject ()throwsException { theSystem.out.println ("GetObject"); -String resource = "Mybatis-config1.xml"; -InputStream in =Resources.getresourceasstream (Resource); -Sessionfactory =NewSqlsessionfactorybuilder (). build (in); +         returnsessionfactory; -     } +  A @Override at      PublicClass<?>Getobjecttype () { -         //TODO auto-generated Method Stub -System.out.println ("Getobjecttype"); -         returnSqlsessionfactory.class; -     } -  in @Override -      Public BooleanIssingleton () { toSystem.out.println ("Issingleton"); +         return true; -     } the}

Application.xml Configuration

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5xmlns:context= "Http://www.springframework.org/schema/context"6xmlns:tx= "Http://www.springframework.org/schema/tx"7xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd8http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd9http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdTenhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> One  A<bean id= "Mysqlsessionfactory"class= "Com.debug.factory.SqlSessionFactoryBean" scope= "prototype" > -      -</bean> the</beans>

Calling test code

1ApplicationContext app =NewClasspathxmlapplicationcontext ("Application.xml");2 {3Sqlsessionfactory sqlsessionfactory = (sqlsessionfactory) app.getbean ("Mysqlsessionfactory");4 System.out.println (sqlsessionfactory);5 }6 {7Sqlsessionfactory sqlsessionfactory = (sqlsessionfactory) app.getbean ("Mysqlsessionfactory");8 System.out.println (sqlsessionfactory);9}

Generated:

1 Issingleton 2 Getobjecttype 3 Issingleton 4 GetObject 5 [Email protected]f04 6 [email protected]f04

Note that the container returned by the Getbean method is not the sqlsessionfactory itself, but rather the object returned by the GetObject () method in the Factorybean implementation class.

The above code is your own integrated mybatis if you use the official package can be used spring The MyBatis project provides org.mybatis.spring.SqlSessionFactoryBean in the code mybatis-spring-1.2.0.tar.gz.

The spring configuration is as follows:

1 class= "Org.mybatis.spring.SqlSessionFactoryBean" >2     <property name= "DataSource" ref= " DataSource "/>3     <property name=" configlocation "value=" Classpath:mybatis-config.xml "/>4 </bean>

3. Factorybean's extended application Spring integration Ehcache

1<bean id= "CacheManager"class= "Org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >2<property name= "Configlocation" >3<value>classpath:ehcache.xml</value>4</property>5</bean>6<bean id= "Levelonecache"class= "Org.springframework.cache.ehcache.EhCacheFactoryBean" >7<property name= "CacheManager" >8<ref local= "CacheManager"/>9</property>Ten<property name= "CacheName" > One<value>levelOneCache</value> A</property> -</bean>

Calling code

1  Public classMaintest {2      Public Static voidMain (string[] args) {3Resource res =NewClasspathresource ("Bean.xml"); 4Beanfactory factory =Newxmlbeanfactory (RES); 5         //take an instance of the CacheManager class6CacheManager CacheManager = (CacheManager) factory. Getbean ("CacheManager"); 7         //fetch An instance of the cache class8Cache Levelonecache = Cachemanager.getcache ("Levelonecache"); 9     }  Ten}

More detailed settings refer to other details!

Introduction to Spring Factorybean mechanism

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.