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


这是为什么呢!
...全文
5532 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyhh 2009-04-11
  • 打赏
  • 举报
回复
问题解决了

原因是,如果除SSH外的类(A型类),需要用到spring管理ADO的注入的其他类(B型单独类)

不能在A用new调用B,只能在applicationContext.xml中将B也注入到A类中

然后在A类中直接引用B类

就好使了

<!--单独Bean注入定义-B型单独类-->
<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注入定义-A型类->
<bean name="/organ" class="com.test.struts.action.OrganAction"
lazy-init="default" autowire="default" dependency-check="default">
<property name="iorgandao">
<ref bean="iorgandaoimpl" />
</property>
<property name="organTest">
<ref bean="organTest" />
</property>
</bean>

谢谢各位
jumpheightway 2009-03-06
  • 打赏
  • 举报
回复
楼主的关系没有弄正确吧





专业培训:JAVA J2EE JSP PHP LAMP系列架构
联系QQ:492236022
wangzhuoyan 2009-03-06
  • 打赏
  • 举报
回复
你服务起来没啊,服务起来的话你就看看在调用哪个模块出的问题,是不是没有依赖注入的set,还有实现类引用是不是对的,代码太多,
skyhh 2009-03-05
  • 打赏
  • 举报
回复
如果不使用注入,而是直接采用

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IorganDaoImpl id = (IorganDaoImpl) ctx.getBean("iorgandaoimpl");

这种方式,系统每一次都会初始化(检查)Spring相关的bean
他需要将所有注入的bean重新走一遍,很是费时间

有没有什么更好的办法!

最好是指点一下关键的配置,或代码!

先谢谢了!
jianpc 2009-03-04
  • 打赏
  • 举报
回复
假如class A 中需要注入B类的实例,则需要A类也在Spring容器中。把A类这个bean也配置进去。
skyhh 2009-03-04
  • 打赏
  • 举报
回复
czyboy123:一定要实现
ctx.getBean("XXX");

这样的话,每一次,后台显示Spring会将所有Spring注入的地方,都初始化一下,性能不是很好!

hanpoyangtitan:是我调用的地方,实现BeanFactoryAware接口码?
什么都不能 2009-03-04
  • 打赏
  • 举报
回复
对实现BeanFactoryAware接口的Bean可以用setBeanFactory方法获得BeanFactory 对象也就是applicationContext
或者直接注入也可以
skyhh 2009-03-04
  • 打赏
  • 举报
回复
jianpc:
我现在情况是
class A =organTest
B类=OrganActioin

你的意思是,需要将A类配置到B类中

<!--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注入定义-->
<bean name="/organ" class="com.test.struts.action.OrganAction"
lazy-init="default" autowire="default" dependency-check="default">
<property name="iorgandao">
<ref bean="iorgandaoimpl" />
</property>
<property name="organTest">
<ref bean="organTest" />
</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>


是不是这样!
skyhh 2009-03-03
  • 打赏
  • 举报
回复
Landor2004:好像我的代码已经排过版的,但不知怎么放上去,就没有了
hanpoyangtitan:以下完整代码
直接解析applicationContext.xml直接调用id是可以的.
看后台日志
在执行
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IorganDaoImpl id = (IorganDaoImpl) ctx.getBean("iorgandaoimpl");

时,Spring好像会初始化一次,将所有管理的bean再次注册一次
会打印出来
System.out.println("organTest--进入=进入Set--=iorgandao");
System.out.println("organTest--1:this.iorgandao=" + this.iorgandao);

但再回到程序中向下走的时候,
System.out.println("==========注入测试--开始=========");
System.out.println("iorgandao==" + iorgandao);
System.out.println("this.iorgandao=" + this.iorgandao);
System.out.println("==========注入测试--结束=========");
却还是为null

================================代码========================================
import com.test.dao.IorganDao;
import com.test.dao.IorganDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class organTest {
private IorganDao iorgandao;

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());

}

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);
}
}
什么都不能 2009-03-03
  • 打赏
  • 举报
回复
似乎由spring兽管的bean不能由new 创建实例,你的action 可以实现BeanFactoryAware接口
实现
setBeanFactory方法获取受管bean
skyhh 2009-03-03
  • 打赏
  • 举报
回复
我的目录结构是这样的
--com.test.dao
--com.test.dao.impl
--com.test.tool
--com.test.struts.action
--com.test.struts.form

这个bean是
com.test.tool下面的

我想在项目中任何地方调用(但一般都是在Action中调用)
是大概这样调用的
import com.test.tool.organTest;

public ActionForward listInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
organTest organTest=new organTest();
String sReturn="0";
sReturn=organTest.GetSelectMap();
System.out.println(sReturn);

return mapping.findForward("jspinfo");
}

sReturn都得不到正确的值

czyboy123 2009-03-03
  • 打赏
  • 举报
回复
public class organTest 这个类是在哪里调用的
在该类调动的地方要用organTest otest= (organTest )ctx.getBean("XXX"); Spring才会帮你注入,如果是用NEW的话,就不会注入了的。
什么都不能 2009-03-02
  • 打赏
  • 举报
回复
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());

}
的完整代码
Landor2004 2009-03-02
  • 打赏
  • 举报
回复
up一下先,这么长,代码又没格式,看着就头疼!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧