hibernate - Spring constructor injection shows null When accessing it on methods -
i developing small application struts2 + spring + hibernate...spring beans injected on server start-up .. have stepped through setters on start , injecting properly. however, run post method , post method(execute() in struts2) , values injected null. why happen?
bean injection :
<bean id="useraction" class="com.example.user.action.useraction"> <constructor-arg index="0"> <ref bean="userservicetarget"/> </constructor-arg> </bean>
my struts2 constructor :
public useraction(iuserservice userservice) { this.userservice=userservice; }
struts2 method :
public string execute() { this.user=(user)userservice.findbyid(this.id); }
but inside execute method userservice value null... when inject injected prperly..
thank you...
i think constructor-args not way ijecting beans another. give example:
applicationcontext.xml:
<bean id="useraction" class="com.example.user.action.useraction"/> <bean id="userservicetarget" class="com.example.user.userservicetarget">
useraction.java:
@autowired private userservicetarget userservice;
you can use other configurations also. example:
<bean id="useraction" class="com.example.user.action.useraction"> <property name="userservice" ref="userservicetarget"/> </bean>
by way autowired annotation not needed, setter.
i don't xml much, best way using stereotype annotations. can use @service annotation on service class , can forget declare bean in appcontext, should add 2 lines way:
<context:annotation-config /> <context:component-scan base-package="com.example"/>
hope helped!
Comments
Post a Comment