spring注入bean的,启动服务器加载完配置文件 bean为null
applcationContext.xml:
<bean id="CommentAction" class="com.flex.blog.action.CommentAction">
<property name="commentServiceAS" ref="CommentServiceAS" />
</bean>
====================
java:
public class ConsumerAction extends ActionSupport {
private ConsumerServiceAS consumerServiceAS; public void setConsumerServiceAS(ConsumerServiceAS consumerServiceAS) {
this.consumerServiceAS = consumerServiceAS;
} public ConsumerServiceAS getConsumerServiceAS() {
return consumerServiceAS;
}
===================
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
问题:启动服务器没任何异常,为什么现在consumerServiceAS还是null呢
applcationContext.xml:
<bean id="CommentAction" class="com.flex.blog.action.CommentAction">
<property name="commentServiceAS" ref="CommentServiceAS" />
</bean>
====================
java:
public class ConsumerAction extends ActionSupport {
private ConsumerServiceAS consumerServiceAS; public void setConsumerServiceAS(ConsumerServiceAS consumerServiceAS) {
this.consumerServiceAS = consumerServiceAS;
} public ConsumerServiceAS getConsumerServiceAS() {
return consumerServiceAS;
}
===================
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
问题:启动服务器没任何异常,为什么现在consumerServiceAS还是null呢
解决方案 »
- mybatis分页 出错
- jsp上面的file标签用了以后无法传到后台怎么办,action那里一直File都是null
- mysql 题 大家帮我看看哪里错了
- 请教有人做过公安方面的JAVA项目没? 有的话请帮忙介绍下
- Filter要想匹配jsp/stat文件夹下的所有jsp页面,url-pattern该怎么配置?
- linux下的java开发需要掌握什么?
- struts No getter methed 问题
- 打包时如何去掉jar包里面的绝对路径
- weblogic8.1配置连接池问题
- ?用什么控件(java)做状态栏??并且如何做????
- 关于做音乐网站要用到什么技术啊
- jsp从数据库获得数据怎么穿花为JSON?
</bean>你在注入时至少要有这个吧,否则怎么引用的呢?
applcationContext.xml:
<bean id="CommentAction" class="com.flex.blog.action.CommentAction">
<property name="commentServiceAS" ref="CommentServiceAS" />
</bean>
<bean id="CommentServiceAS" class="com.flex.blog.servive.impl.CommentServiceASImpl">
<property name="commentDaoAS" ref="CommentDaoAS" />
</bean>
<bean id="CommentDaoAS" class="com.flex.blog.dao.impl.CommentDaoASImpl">
<property name="commentDaoAS" ref="hibernateTemplate" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
如果是使用了struts2在strutx.xml中的类名要是CommentAction。
applcationContext.xml:
<bean id="CommentAction" class="com.flex.blog.action.CommentAction">
<property name="commentServiceAS" ref="CommentServiceAS" />
</bean><!--此处ref=“CommentServiceAS”说明你必须在applicationContext.xml再定义另外一个bean,叫CommentServiceAS--!>
红线部分的配置是什么意思:<bean id="CommentDaoAS" class="com.flex.blog.dao.impl.CommentDaoASImpl">
<property name="commentDaoAS" ref="hibernateTemplate" /></bean>
楼主是不是搞错了
!
<property name="commentServiceAS" ref="CommentServiceAS" />
//你的name=commentServiceAS,这样,它就会去找它对应的set方法,所以你的Action里面要有这个set方法,让你保持name一样也就是这个意思,当然,不一样也可以,但是只要确保有它的set方法之后你就可以取其它的名字。
<bean id="CommentDaoAS" class="com.flex.blog.dao.impl.CommentDaoASImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<action name='' class="">
</actio>
这的class是怎么配置的,值是不是CommentAction,还是你直接使用的包名+类名的形式?
private ConsumerServiceAS consumerServiceAS;public void setConsumerServiceAS(ConsumerServiceAS consumerServiceAS) {
this.consumerServiceAS = consumerServiceAS;
}
<bean id="CommentAction" class="com.flex.blog.action.CommentAction">
<property name="commentServiceAS" ref="CommentServiceAS" />
</bean>
lz啊你action里的属性有commenServiceAs吗,我没找到,只看到你action里有个属性是consumerServiceAS,这里的property 应该是你action里的属性名字,你的consumerServiceAS不为空就见鬼了
<property name="commentServiceAS" ref="CommentServiceAS" />
</bean>spring 中的property name=commentServiceAS
而action 中的名字为:consumerServiceAS
名字很明显没正确关联 如果你不改action类里面的名字consumerServiceAS,你也可以把set和get的方法名改为getCommentServiceAS() setCommentServiceAS()也可以
<property name="commentServiceAS" ref="CommentServiceAS" />
</bean>
中的commentServiceAS 在你的Action中并未对应上!