加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – spring-nullpointerexception-无法在无注释类中访问自

发布时间:2020-12-15 01:42:02 所属栏目:大数据 来源:网络整理
导读:我有这个问题,我无法解决. 从我的@Controller,我可以轻松访问我自动连接的@Service类,并且没有问题. 但是当我从没有注释的单独类中执行此操作时,它会给我一个NullPointerException. 我的控制器(工程) @Controller public class UserController { @Autowired

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

从我的@Controller,我可以轻松访问我自动连接的@Service类,并且没有问题.
但是当我从没有注释的单独类中执行此操作时,它会给我一个NullPointerException.

我的控制器(工程) –

@Controller
 public class UserController {
 @Autowired
 UserService userService;...

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

public final class UsersManagementUtil {
  @Autowired
  UserService userService;

要么

@Autowired
UserDao userDao;

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

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

我的servlet上下文 –

任何线索?

最佳答案
从Spring Reference 3.0起

By default,classes annotated with
@Component,@Repository,@Service,
@Controller,or a custom annotation
that itself is annotated with
@Component are the only detected
candidate components.

UsersManagementUtil应根据您的需要使用其中一个进行注释.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读