当前位置: 代码迷 >> Java Web开发 >> spring 经过注入无法创建bean
  详细解决方案

spring 经过注入无法创建bean

热度:127   发布时间:2016-04-13 22:43:27
spring 通过注入无法创建bean
web.xml内容
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>
  
  <filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
   </filter>

   <filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
   </filter-mapping>
   
   <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <!-- struts2 -->
    <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

application.xml中的内容
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<!-- connection -->
<property name="dataSource">
<ref local="dataSource" />
</property>

<!-- hibernate自身属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
</props>
</property>

<!-- 映射文件 -->
<!-- <property name="mappingResources">
<list>
<value>com/hontek/demo/vo/SysUser.hbm.xml</value>
</list>
</property>-->


</bean>


<!-- advice -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>


<tx:advice id="baseServiceAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" isolation="READ_COMMITTED" />
<tx:method name="*" read-only="true" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>


<bean id="userdao" class="org.dao.userdao"> </bean>
<bean id="userservice" class="org.service.userservice">
<property name="ud" ref="userdao"></property>
</bean>
然后调用时候
	private userdao ud;

public userdao getUd() {
return ud;
}

public void setUd(userdao ud) {
this.ud = ud;
}

public String adduser(){
ud.uudd();
return "success";
}


调试时候发现 ud 为空 
使用
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
userdao user = (userdao)factory.getBean("userdao");
user.uudd();

发现 user 可以使用。
这是怎么回事? 
请问如何才能使ud不为空?

------解决思路----------------------
配置文件中的 userdao 的id 与你userservice的引用name不一致,<bean id="userdao" class="org.dao.userdao"> </bean>改成<bean id="ud" class="org.dao.userdao"> </bean>
  相关解决方案