小编use*_*806的帖子

spring-nullpointerexception-无法在无注释类中访问自动连接的带注释的服务(或dao)

我有这个问题,我无法解决.

从我@Controller,我可以轻松访问我的自动连接@Service类,并使用它没有问题.但是当我从一个没有注释的单独课程那样做时,它给了我一个NullPointerException.

我的控制器(工程) -

@Controller
 public class UserController {
 @Autowired
 UserService userService;...
Run Code Online (Sandbox Code Playgroud)

我独立的Java类(不工作) -

public final class UsersManagementUtil {
  @Autowired
  UserService userService;
Run Code Online (Sandbox Code Playgroud)

要么

@Autowired
UserDao userDao;
Run Code Online (Sandbox Code Playgroud)

userService或userDao始终为null!只是尝试其中任何一个有效.

我的组件扫描设置具有用于扫描的根级别包,因此应该没问题.

我的servlet上下文 -

<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- the application context definition for the
         springapp DispatcherServlet -->
<!-- Enable annotation driven controllers, validation etc... -->

<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="x" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

    <!-- package …
Run Code Online (Sandbox Code Playgroud)

java spring annotations spring-mvc autowired

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

annotations ×1

autowired ×1

java ×1

spring ×1

spring-mvc ×1