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.