Spring注入问题,注入到bean后,得到null,注入不成功
skyhh 2009-03-01 02:16:38 请教:
在SSH框架上,用Spring来管理Hibernate中实现接口DAO和实现类DAOImpl注入到单独bean中,得到的对象是NUll的问题
Spring注入DaoImpl到Struts中及Ajax框架Dwr中的Bean都是可以的,但注入到单个bean中打印对象却是Null
以下是大概的测试代码,所涉及代码及模块是没有问题的!
applicationContext.xml
==========
<!--dao及daoimpl定义-->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="iorgandao" class="com.test.dao.IorganDao" abstract="true"></bean>
<bean id="iorgandaoimpl" class="com.test.dao.impl.IorganDaoImpl" parent="iorgandao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
<!--Struts注入定义-->
<bean name="/organ" class="com.test.struts.action.OrganAction"
lazy-init="default" autowire="default" dependency-check="default">
<property name="iorgandao">
<ref bean="iorgandaoimpl" />
</property>
</bean>
<!--单独Bean注入定义-->
<bean id="organTest" class="com.test.tool.organTest"
lazy-init="default" autowire="default" dependency-check="default">
<property name="iorgandao">
<ref bean="iorgandaoimpl" />
</property>
</bean>
struts-config.xml
==========
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>
<message-resources parameter="com.test.struts.ApplicationResources" />
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
web.xml
==========
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
在单独bean中定义
==========
import com.test.dao.IorganDao;
public class organTest {
private IorganDao iorgandao;
public String GetSelectMap() {
String sReturn="0";
try {
System.out.println("==========注入测试--开始=========");
System.out.println("iorgandao=="+iorgandao);
System.out.println("this.iorgandao="+this.iorgandao);
System.out.println("==========注入测试--结束=========");
boolean bResult=false;
bResult = this.iorgandao.isExists("1","机构1");
if (bResult){
sReturn="1";
}
} catch (Exception e) {
System.out.println("a="+e.getLocalizedMessage());
System.out.println("organ.isExists:得到错误");
System.out.println("b="+e.getMessage());
}
return sReturn;
}
public IorganDao getIorgandao() {
return iorgandao;
}
public void setIorgandao(IorganDao iorgandao) {
this.iorgandao = iorgandao;
System.out.println("organTest--进入=进入Set--=iorgandao");
System.out.println("organTest--1:this.iorgandao="+this.iorgandao);
}
}
问题:
==========
在系统启动时
在Struts中是正常
但单独bean,organTest中的
Set方法中的
System.out.println("organTest--进入=进入Set--=iorgandao");
System.out.println("organTest--1:this.iorgandao="+this.iorgandao);
不是null,是可以内容来的
但在真正使用iorgandao时
却是null
其他已经试过的方案:
方案1:
==========
直接解析applicationContext.xml
public String GetSelectMap() {
String sReturn="0";
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IorganDaoImpl id = (IorganDaoImpl) ctx.getBean("iorgandaoimpl");
System.out.println("==========方案1测试--开始=========");
System.out.println("id=="+id);
System.out.println("==========方案1测试--结束=========");
System.out.println("==========注入测试--开始=========");
System.out.println("iorgandao=="+iorgandao);
System.out.println("this.iorgandao="+this.iorgandao);
System.out.println("==========注入测试--结束=========");
boolean bResult=false;
bResult = this.iorgandao.isExists("1","机构1");
if (bResult){
sReturn="1";
}
} catch (Exception e) {
System.out.println("a="+e.getLocalizedMessage());
System.out.println("organ.isExists:得到错误");
System.out.println("b="+e.getMessage());
}
IorganDaoImpl id = (IorganDaoImpl) ctx.getBean("iorgandaoimpl");
id不是null,是可以打印出来,并且可以正常使用
看后台
Set方法中的
System.out.println("organTest--进入=进入Set--=iorgandao");
System.out.println("organTest--1:this.iorgandao="+this.iorgandao);
不是null,是可以内容来的
但在真正使用iorgandao时
却是null
方案2:
==========
怀疑是Spring的注入的配置问题(作用域)
修改了定义处,加上scope
<!--单独Bean注入定义-->
<bean id="organTest" class="com.test.tool.organTest"
lazy-init="default" autowire="default" dependency-check="default" scope="prototype">
<property name="iorgandao">
<ref bean="iorgandaoimpl" />
</property>
</bean>
逐个试了各种属性,都还是不行
prototype、request、session、global session
这是为什么呢!