Spring Injection Sessionfactory

Source: Internet
Author: User

Reading Spring's book, the book is about getting injected through the ApplicationContext Getbean () method.

And then looked at a ready-made project, Applicationcontext.xml is equipped with DataSource and sessionfactory, and the DAO class can be persisted by inheriting Hibernatedaosupport to obtain hibernatetemplate.

Then just want to apply to the knowledge, see how hibernatedaosupport in the implementation of the Sessionfactory injection.

It turns out that there are setsessionfactory () in the Hibernatedaosupport class, but I don't see the Getbean () method called ApplicationContext, There is no related call in the DAO that inherits Hibernatedaosupport in the project ...

After all kinds of Baidu, finally know the reason. This is because of spring's automatic injection function, we just have to open the Autowire property in XML for all DAO that inherits Hibernatedaosupport. Just like in my project, in <beans default-autowire= "ByName" > Riga a Default-autowire attribute, all of our <beans> under <bean> You have the ability to find and inject JavaBean-dependent objects in a way that is the name of the property. For example, when the bean of our Mydao class is in the beans range, Mydao inherits from the hibernatedaosupport,spring IOC container to find the Id/name property sessionfactory in the configuration file. The Setsessionfactory () method is then inherited to inject it.

The following is a description of Autowier's other people's excerpt (original address: http://blog.csdn.net/virgoboy2004/article/details/7525795)

When we use the IOC capabilities of spring, Spring provides a centralized injection approach: attribute injection, constructor injection, and factory method injection, and more often we use attribute injection, or set method injection. Using Set method injection requires us to manually set the properties when we write the bean's configuration file. Such as:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <BeanID= "Testbean"class= "Com.jack.TestBean"Scope= "Prototype"/>    <BeanID= "Testaction"class= "Com.jack.TestAction"Scope= "Prototype">        < Propertyname= "Testbean"ref= "Testbean"/>    </Bean></Beans>

It is rather troublesome to use spring in this way.

"Problem analysis and solutions" We're wondering, is there a way of automatic injection? It doesn't have to be written in the Spring config file.

<name= "Testbean"  ref= "Testbean"/>

Kind of statement, in some way let spring know what kind of object to inject.

When you become a ... Spring so smart open source framework, long ago to everyone think of .... He's autowire.

Spring's bean has a Autowire property, which can be 6 values below.

1, No: Automatic assembly is not enabled. Autowire the default value. 2, ByName: The name of the property to find the JavaBean-dependent object and inject it. For example, if the class computer has a property printer, specifying that its Autowire property is ByName, the Spring IOC container looks for a bean with Id/name property printer in the configuration file and injects it using the Seter method. 3. Bytype: Finds and injects JavaBean-dependent objects through the type of the property. For example, if the class computer has an attribute printer and the type is printer, the Spring IOC container will look for the bean with the class attribute bytype and inject it with the printer method after specifying its Autowire property as Seter. 4, constructor: Pass Bytype like, is also through the type to find dependent objects. The difference from Bytype is that it is not injected using the Seter method, but instead uses a constructor injection. 5. AutoDetect: Automatic choice of injection mode between Bytype and constructor. 6. Default: Determined by the Default-autowire attribute of the superior label <beans>

In general, we use byname when we are using it, which means that when we define the bean, when we name the bean, the conventional bean ID is set to the first lowercase class name. In my example above is Testbean and testaction, so that when we are in the testaction to automatically inject Testbean. It is necessary to write a set method in it, of course, the name of the set method is also normative, that is to set+ class name, here is the Settestbean.

 PackageCom.jack;/** * @authorJack Zhang *@versionvb1.0 * @Email [Email protected] * @Date 2012-5-1*/ Public classtestaction{PrivateTestbean Testbean;  Public voidSettestbean (Testbean testbean) { This. Testbean =Testbean; }         PublicString Execute () {Testbean.getcode (); return"JSON"; }}

Then change the configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <BeanID= "Testbean"class= "Com.jack.TestBean"Scope= "Prototype"/>    <BeanID= "Testaction"class= "Com.jack.TestAction"Scope= "Prototype"Autowire= "ByName"/></Beans>

Some people may be too troublesome, because this, every bean to add this sentence, very redundant, haha, Spring project group also give you think of, that is to see as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Default-autowire= "ByName"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <BeanID= "Testbean"class= "Com.jack.TestBean"Scope= "Prototype"/>    <BeanID= "Testaction"class= "Com.jack.TestAction"Scope= "Prototype"/></Beans>

Okay, the problem is solved perfectly ...

Spring Injection Sessionfactory

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.