spring 属性注入问题,为什么生成的对象总是为空?

dw_java08 2012-07-02 12:37:51
[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.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 配置哪些类哪些方法使用事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* com.z2sci.soa.dao.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="allManagerMethod" />
</aop:config>


</beans>
[/size][/size]

麻烦大家帮我分析一下到底是怎么回事,谢谢了!
...全文
551 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lifeabout 2012-07-04
  • 打赏
  • 举报
回复
属性第一个和第二个字母大写的话,生成的get和set方法是错的,不信你去看下好了
tangwei070 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

private ILoginManager iLoginManager;

属性第二个字母不要大写。
[/Quote]

支持5楼。 可能会出现莫名其妙的错误。
f229866245 2012-07-04
  • 打赏
  • 举报
回复
public void setILoginManager(ILoginManager loginManager) {
iLoginManager = loginManager;
}

这个你用自动的生成一下seter吧!可能会好点!
dw_java08 2012-07-03
  • 打赏
  • 举报
回复
哪位大侠帮帮忙~ 谢谢了
dw_java08 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

private ILoginManager iLoginManager;

<bean id="userLogin" class="com.z2sci.soa.web.action.LoginAction">
<property name="ILoginManager" ref="ILoginManager" />
iLoginManager与ILoginManager名字不一致?!你……
[/Quote]
我之前配置的是 “iLoginManager”,因为在LoginAction中生成对象iLoginManager 为空我才该成“ILoginManager”的,修改了之后还是iLoginManager 对象为空
dw_java08 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

你struts2的配置文件是不是<action name="userLogin" method="add" class="userLogin" >这样写的嘛,action的id必须要在spring中拿
[/Quote]
action id 必须在spring 中拿 什么意思?能详细说说吗?
  • 打赏
  • 举报
回复
<bean id="ILoginManager" class="com.z2sci.soa.manager.impl.LoginManagerImpl">
<property name="iLoginDao" ref="iLoginDao" />
</bean>



你这边配置的跟你类中的属性名不一致。
  • 打赏
  • 举报
回复
private ILoginManager iLoginManager;

属性第二个字母不要大写。
良才2015 2012-07-02
  • 打赏
  • 举报
回复
1楼正解。
配置文件区分大小写
宏Lee 2012-07-02
  • 打赏
  • 举报
回复
你struts2的配置文件是不是<action name="userLogin" method="add" class="userLogin" >这样写的嘛,action的id必须要在spring中拿
xiongyu2006 2012-07-02
  • 打赏
  • 举报
回复


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

f229866245 2012-07-02
  • 打赏
  • 举报
回复
private ILoginManager iLoginManager;

<bean id="userLogin" class="com.z2sci.soa.web.action.LoginAction">
<property name="ILoginManager" ref="ILoginManager" />
iLoginManager与ILoginManager名字不一致?!你试一下!!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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