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

前端之家收集整理的这篇文章主要介绍了java – spring-nullpointerexception-无法在无注释类中访问自动连接的带注释的服务(或dao)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

从我的@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上下文 –

factorybean">
    sql">${hibernate.show_sql}

任何线索?

最佳答案
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应根据您的需要使用其中一个进行注释.

原文链接:https://www.f2er.com/spring/432653.html

猜你在找的Spring相关文章