In SSH integration, the issue of spring dependency injection failure

Source: Internet
Author: User
Tags constructor ssh

As we all know, spring function is very powerful ~ mainly in two aspects: one is the IOC control inversion, and the other is AOP oriented aspect thinking.

Spring can host hibernate as well as transactions, and it also provides hibernatetemplate,hibernatedaosupport,hibernatecallback three classes to simplify the operation of Hibernante

Their roles are:

Hibernatetemplate: The façade class for all persisted operations. It can operate on the database without involving sessionfactory, and turn it off completely automatically for things to close.

Hibernatedaosupport: This class is used to support the operation of the DAO layer, and by inheriting the class, the Hibernatetemplate object can be obtained by the built-in Gethibernatetemplate () method and manipulated

All classes that inherit the class, when dependency injection, must add hibernatetemplate injection, which is obtained through the built-in sethibernatetemplate (), there is a setsessionfactory () method In addition to these two methods.

Hibernatecallback: This class is an abstract class, which allows the program ape to directly face the native hibernate session, such as: when the sub-query, hibernatetemplate can not complete, only rely on it.

These are the three APIs that hibernate does for post-hosting operations.

IOC it can be injected into the data access layer and the business logic layer to reduce the coupling of the two time, so that the business is clear ~ ~ ~

Among them, in the dependency injection, often there is a problem ~ ~ ~ (This is the focus of this topic)

Sometimes, when we inject a dependency into the DAO layer, service layer, action, we often prompt for null pointer exception, which is injection failure ...

God ~ ~ Find long time still can not find where wrong ~ ~ ~ had to use ApplicationContext ac=new classpathxmlapplicationcontext ("Applicationcontext.xml"); To load, substitute for ~ ~

This is every bit abnormal, normal use ~ ~ What is going on ... The reasons are as follows:

In the case of dependency injection, the injection of the DAO layer class is not the same as the action. A little bit different.

Our example explains the subtle differences between the two injections:

Example of a DAO layer injection:

<bean id= "Loginregistserviceimpl" class= "Com.serviceImpl.LoginRegistServiceImpl" >
    <constructor-arg Name= "Udao" ><ref bean= "Userdaoimpl"/></constructor-arg>
    </bean>
    
    <bean id= " ServiceManager "class=" Com.serviceImpl.ServiceManager ">
    <property name=" LRS "><ref bean=" Loginregistserviceimpl "/></property>
    </bean>


This is a very common bar ~ ~ No doubt

Take a look at the injection of action:

<bean id= "ServiceManager" class= "Com.serviceImpl.ServiceManager" >
    <property name= "LRS" ><ref Bean= "Loginregistserviceimpl"/></property>
    </bean>
    
    <bean id= "Loginregistaction" class= "Com.action.LoginRegistAction" >
       <property name= "ServiceManager" ><ref bean= "ServiceManager"/> </property>
    </bean>


See what the difference is between them. (Certainly the code is not the same)

The focus is on:

<property name= "ServiceManager" ><ref bean= "ServiceManager"/></property>


In action injection, you must require that the name of the variable defined by the action be consistent with the ID name of the injected bean, and no injection fails ~ ~ ~

Haha, understand ...

Similar to the loaded code, there is absolutely no need to appear throughout the project:

ApplicationContext ac=new classpathxmlapplicationcontext ("Applicationcontext.xml");
		Beanfactory Bf=ac;
		


There is also a little knowledge:

<property name= "LRS" ><ref bean= "Loginregistserviceimpl"/></property>


The above is injected by the Set method

The following is an injection through a constructor function

    <constructor-arg name= "Udao" ><ref bean= "Userdaoimpl"/></constructor-arg>


I'll explain it here today ~ ~ ~

Continue tomorrow ~ ~ ~

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.