首页 新闻 会员 周边

为什么 @autowired注入为null

0
[已解决问题] 解决于 2018-06-19 08:54

<?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"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">

<!-- 加载属性配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>

<!-- xml配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvie" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*"/>
<tx:method name="save*"/>
<tx:method name="insert*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:pointcut expression="execution(* com.lj.service.*.*(..))" id="cut"/>
<aop:advisor advice-ref="txAdvie" pointcut-ref="cut"/>
</aop:config>

<!-- user -->
<bean id="userDao" class="com.lj.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.lj.service.impl.UserServiceImpl">
</bean>
<!-- action需要设置为原型scope=prototype,不能设置为单例,默认不写就是singleton单例 -->
<bean id="userAction" class="com.lj.action.UserAction" scope="prototype">
</bean>
</beans>

 

 

这个是action:

package com.lj.action;

import org.springframework.beans.factory.annotation.Autowired;

import com.lj.model.User;
import com.lj.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
@Autowired
private UserService userService;

public String hello() {
return SUCCESS;
}

public String findUserById(){
User user = userService.findUserById(1);
System.out.println(user.toString());
return SUCCESS;
}
}

 

 

这个是service:

package com.lj.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.lj.dao.UserDao;
import com.lj.model.User;
import com.lj.service.UserService;

public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;

@Override
public User findUserById(int id) {
return userDao.selectUserById(id);
}

}

 

 

 

问题补充:

如果不用@autowired的话,使用最老的xml的配置方式,注入是成功的,使用@autowired action中service就会为null

派小星的主页 派小星 | 初学一级 | 园豆:114
提问于:2018-06-18 15:26
< >
分享
最佳答案
0

使用@Autowired注解 那你的xml里面有没有配置注解扫描路径?
你使用注解注入,你得告诉spring去哪里扫描你的注解吧???
就算你不配置扫描路径,至少得加上<context:annotation-config/>吧>
如果你想使用Autowired注解又不想加上上面的那个配置,那么最起码你也要把这个加上吧?
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>

奖励园豆:5
、熙和 | 小虾三级 |园豆:1508 | 2018-06-19 08:41
其他回答(2)
0

应该是“spring的bean的注解扫描器“的配置有问题;

或者是你没有给service层添加注解“@Service("userService")”

 

吴桂鑫 | 园豆:166 (初学一级) | 2018-06-18 20:01

我在applicationContext.xml中已经配置了

<bean id="userService"class="com.lj.service.impl.UserServiceImpl">
</bean>

这样应该和@Service("userService")”一样吧

支持(0) 反对(0) 派小星 | 园豆:114 (初学一级) | 2018-06-18 20:19

@派小星: 那你试一下这个“@Autowired("userService")”

支持(0) 反对(0) 吴桂鑫 | 园豆:166 (初学一级) | 2018-06-18 20:28

@派小星: 我写错了,应该是

@Autowired

@Qualifier("userServiceImpl")

支持(0) 反对(0) 吴桂鑫 | 园豆:166 (初学一级) | 2018-06-18 20:56

@派小星: 不过我听到另一种想法:

有人说不能用实体类型来接收自动装配的增强类(就是动态代理类),因为它们是兄弟,所以只能用实体类的接口来接收增强类,因为实体类和增强类都实现了相同的接口。

我不知道对不对,你可以试一下,用接口来接收。

支持(0) 反对(0) 吴桂鑫 | 园豆:166 (初学一级) | 2018-06-18 21:01

@不喜欢蛀牙: 这样的话,你可以用@Resource注解

支持(0) 反对(0) 溪源 | 园豆:202 (菜鸟二级) | 2018-06-18 21:36
0

注解发生歧义了,你是不是有多个接口userDao实现类啊???

溪源 | 园豆:202 (菜鸟二级) | 2018-06-18 21:34

没有啊,就是1个userService1个userServiceImpl,1个userDao,1个dao的实现类。我如果还用原始的再applicationContext.xml中注入serviceimpl,是没问题的,但是一旦换成上面那样就有问题

支持(0) 反对(0) 派小星 | 园豆:114 (初学一级) | 2018-06-18 21:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册