spring注入为空

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring注入为空相关的知识,希望对你有一定的参考价值。

spring 配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="username" value="ssi"/>
<property name="password" value="ssi"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="4"/>
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml">
</property>
<property name="dataSource" ref="dataSource">
</property>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="sqlMapClientProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="select*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>

<bean id="userDao" class="com.soft.ssi.dao.UserDaoImpl">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>

<bean id="userService" class="com.soft.ssi.service.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>

<bean id="userServicePrxy" parent="sqlMapClientProxyFactoryBean">
<property name="target" ref="userService"></property>
</bean>

<bean id="UserAction" class="com.soft.ssi.web.UserAction">
<property name="userService" ref="userService"></property>
</bean>

</beans>

action:
public class UserAction extends ActionSupport

private UserService userService;

private UserInfo userInfo;

public UserInfo getUserInfo()
return userInfo;


public void setUserInfo(UserInfo userInfo)
this.userInfo = userInfo;


public UserService getUserService()
return userService;


public void setUserService(UserService userService)
this.userService = userService;


public String execute() throws Exception
return super.execute();


public String regedit() throws Exception

boolean flag = false;
System.out.println(userInfo);
System.out.println(this.userService);
flag = userService.insertUser(userInfo);
if(flag)
return "ok";
else
return "error";





userService对象为空

检查 struts.xml配置中是否正确,以及struts.properties中是否配置了struts.objectFactory=spring 参考技术A 不建议用xml配置注入对象,建议用注解,@components("userService")
@Resource
参考技术B 没有用过sqlMapClientProxyFactoryBean 是不是被代理了,原型找不到了。你跟下代码吧 参考技术C userService被代理了

activiti中实现TaskListener注入Spring的bean

一开始我写的bpmn中

技术图片

 然后在我的监听器中注入的Spring的bean为空,注入不进来。

技术图片

像这样,tenderService为null 注入不了。

 

此时需要把bpmn中这样设置,sendDocumentStartListener是监听器的类名(在这里首字母要小写)

技术图片

监听器里要这么写(注意要加上@Component注解)

技术图片

此时就可以注入Spring的bean了

以上是关于spring注入为空的主要内容,如果未能解决你的问题,请参考以下文章

Spring注入的DataSource为空

关于Spring依赖注入的问题

手动注入bean时 , 根据条件注入bean

activiti中实现TaskListener注入Spring的bean

为啥Spring推荐使用构造器注入

工具类中怎么注入service,spring mvc +mybatiss