search

Home  >  Q&A  >  body text

Spring4.3.7注解 @Autowired java.lang.NullPointerException

UserAction中通过@Autowired注入UserServiceImpl无效,得到对象为空
UserAction.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<code>@Namespace("/")

@ParentPackage("struts-default")

@Controller

public class UserAction extends ActionSupport implements ModelDriven<User> {

    private User user = new User();

 

    public User getModel() {

        return this.user;

    }

 

    // *****************************

    @Autowired

    private UserService userService;

 

    @Action(value = "userAction_add", results = { @Result(name = "add", location = "/success.jsp") })

    public String add() {

        this.userService.saveUser(user);

        return "add";

    }

}

</code>

UserServiceImpl.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<code>@Service

public class UserServiceImpl implements UserService {

    @Autowired

    private UserDao userDao;

 

    @Transactional

    public void saveUser(User user) {

        this.userDao.save(user);

    }

 

    @Transactional

    public void updateUser(User user) {

        this.userDao.update(user);

    }

 

    @Transactional

    public void deleteUser(User user) {

        this.userDao.delete(user);

    }

 

    @Transactional(readOnly = true)

    public User findUserById(Integer id) {

        return this.userDao.findById(id);

    }

 

    @Transactional(readOnly = true)

    public List<User> findAllUser() {

        return this.userDao.findAll();

    }

}</code>

配置文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

<code><?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:tx="http://www.springframework.org/schema/tx"

    xmlns:aop="http://www.springframework.org/schema/aop"

    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/tx

            http://www.springframework.org/schema/tx/spring-tx.xsd

            http://www.springframework.org/schema/aop

            http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 扫描 com包-->

    <context:component-scan base-package="com"></context:component-scan>

    <context:annotation-config/>

    <!--

    <context:property-placeholder location="classpath:redis.properties"/>

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"

        <property name="maxIdle" value="${redis.pool.maxIdle}" /> 

        <property name="maxTotal" value="${redis.pool.maxActive}" />  

        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />  

        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> 

        <property name="testOnReturn" value="${redis.pool.testOnReturn}" /> 

    </bean>  -->

     

    <!--  加载c3p0-db.properties -->

    <context:property-placeholder location="classpath:c3p0-db.properties" />

    <!-- 配置dataSource数据源 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <property name="driverClass" value="${jdbc.driverClass}"></property>

        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

        <property name="user" value="${jdbc.user}"></property>

        <property name="password" value="${jdbc.password}"></property>

    </bean>

     

    <!-- 1 配置SessionFactory -->

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

        <!-- 1.1 配置数据源 -->

        <property name="dataSource" ref="dataSource"></property>

        <!-- 1.2 其他配置项 ,要使用hibernate全属性名,如果hibernate.不要省略 -->

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">

                    org.hibernate.dialect.MySQL5Dialect

                 </prop>

                <prop key="hibernate.show_sql">true</prop>

                <prop key="hibernate.format_sql">true</prop>

                <prop key="hibernate.hbm2ddl.auto">update</prop>

                <prop key="javax.persistence.validation.mode">none</prop>

                <prop key="hibernate.current_session_context_class">thread</prop>

                <prop key=""></prop>

            </props>

        </property>

         

        <!-- 配置实体类 -->

        <property name="packagesToScan">

            <list>

                <value>com.entity</value>

            </list>

        </property>       

    </bean>

    <!-- 配置hibernate模板,必须使用模板 -->

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">

        <!-- 通过工厂获得session,操作PO类 -->

        <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

    <!-- 事务管理 -->

    <!-- 事务管理器,就是平台,spring工具产生, 依赖于使用 持久方案(hibernate、jdbc等) -->

    <bean id="txManager"

        class="org.springframework.orm.hibernate5.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

    <!-- 将事务管理注册spring * proxy-target-class="true":

    使用cglib * proxy-target-class="false":有接口将使用 jdk -->

    <tx:annotation-driven transaction-manager="txManager" />

</beans>

</code>

迷茫迷茫2901 days ago986

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:56:46

    Please ensure that UserServiceImpl is in the scanned package.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:56:46

    There is a problem with scanning annotations

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:56:46

    I suggest you check whether UserDao is injected correctly. If one injection fails in this chain, it will cause the overall creation to fail

    reply
    0
  • Cancelreply