日期:2014-05-19  浏览次数:20730 次

spring 属性注入问题,为什么生成的对象总是为空?
[size=14px][size=12px]在ssh2项目整合过程中遇到一个问题:
java 代码:
public class LoginAction extends ActionSupport {
  private ILoginManager iLoginManager;
private Customer customer;
private String username;
private String password;

public void setILoginManager(ILoginManager loginManager) {
iLoginManager = loginManager;
}

public ILoginManager getILoginManager() {
return iLoginManager;
}
   
//用户登录
public String customerLogin() throws Exception {

System.out.println("username = " + username);
System.out.println("password = " + password);
System.out.println("iLoginManager = " + iLoginManager); Customer customer = new Customer();
customer.setUsername(username);
customer.setPassword(password);

if (iLoginManager.login(customer)) {
return "success";
}
if(username != null && password != null){
return "success";
}

//PropertyConfigurator.configure("log4j.properties");
//logger.debug("111111111111111");
return "loginfail";
}
 
}
上面标红的地方就是提示 iLoginManager 为空

spring 配置文件代码:

applicationContext-action.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/oxm
  http://www.springframework.org/schema/oxm/spring-oxm-2.5.xsd">

<bean id="userLogin" class="com.z2sci.soa.web.action.LoginAction">
<property name="ILoginManager" ref="ILoginManager" />
</bean>

</beans>

applicationContext-bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/oxm
  http://www.springframework.org/schema/oxm/spring-oxm-2.5.xsd">



<bean id="iLoginDao" class="com.z2sci.soa.dao.impl.LoginDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="ILoginManager" class="com.z2sci.soa.manager.impl.LoginManagerImpl">
<property name="iLoginDao" ref="iLoginDao" />
</bean>
</beans>

applicationContext-commons.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springfra