In Spring configuration, the bean class uses the factory Bean meaning for parsing. springbean

Source: Internet
Author: User

In Spring configuration, the bean class uses the factory Bean meaning for parsing. springbean

Parsing the meaning of factory bean used by Bean class in Spring Configuration

Beautiful Life of the sun and fire god (http://blog.csdn.net/opengl_es)

This article follows the "signature-non-commercial use-consistency" creation public agreement

Reprinted please keep this sentence: Sun huoshen's beautiful life-this blog focuses on Agile development and mobile and IOT device research: iOS, Android, Html5, Arduino, pcDuino, otherwise, this blog post is rejected or reprinted. Thank you for your cooperation.



    <bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean">        <property name="mapperInterface" value="com.manager.data.StudentMapper"/>        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>    </bean>

1. This is a bean configured in the Spring configuration file;

Of course, there can be multiple Spring configuration files, and the final Bean will be instantiated and assembled according to the XML configuration,

The so-called assembly is the attribute of who it is, it is assigned to its attributes, first-level assembly is complete.


2. Let's look at the definition of bean instantiation in XML:

<bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean">
Id is used to identify the bean, or the name of the class during Java code instantiation;

Who does this class mean? Of course it is the class pointed to later!

However, it is a coincidence that the following classes are FactoryBean and cannot be instantiated. Therefore, the Spring framework automatically calls the factory method of the factory Bean to obtain the StudentMapper class instance,

In this case, the instance referenced by the previous id is not the class instance. Of course, the reference type represented by id is no longer.

This time I will know the reason for MyBatis Mapper.


3. However, there is a simpler way to achieve the integration of MyBatis and Srping, that is, the MyBatis-Srping component. After introducing the various packet classes in the MyBatis package, replace the corresponding class with the same name:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

In addition, it is no longer so troublesome to obtain Mapper instance objects. Use the method in MyBatis-Spring:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

With this sentence, all Mapper searches based on the position specified by its attribute expression value, all of which can be correctly instantiated;

All configuration files in the context of Spring can be freely referenced.







Related Article

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.