简体   繁体   English

Spring @Autowired在某些课程中不起作用

[英]Spring @Autowired not working in some classes

So i am using Spring to access my DB and fetch Users based on different Properties. 所以我正在使用Spring访问我的数据库并基于不同的属性获取用户。 With a @Restcontroller I get the Data. 使用@Restcontroller可以获取数据。 I created a UserRepository which extends the CrudRepository. 我创建了一个UserRepository,它扩展了CrudRepository。

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Integer> {
Iterable<User> findByNachname(String nachname);

    Iterable<User> findByRolle(Rolle rolle);

    Optional<User> findByBenutzername(String benutzername);

    @Transactional
    String deleteByBenutzername(String benutzername);
}

I use @Autowire to get an Instance of the UserRepo in my Controller-class 我使用@Autowire在控制器类中获取UserRepo的实例

@RestController
public class LoginController {
    @Autowired
    private UserRepository userRepository;
}

This works perfectly fine in all Controllers i have. 这在我拥有的所有控制器中都可以正常工作。 But now when i try the same in another class the userRepository Instance is null. 但是现在当我在另一个类中尝试相同时,userRepository Instance为null。

public class Authentificator {
    @Autowired
    private UserRepository userRepository;
}

The Authentificator and the LoginController are not in the same package. Authentificator和LoginController不在同一程序包中。 But both of them are not in the same package as the UserRepo. 但是,它们和UserRepo不在同一个程序包中。

  • project 项目
    • UserRepo-package UserRepo包
    • Controller-Package 控制器 - 包装
    • Authentificator-Package Authentificator,包

you must make sure Authentificator is also a spring bean - I mean you must annotate it with something like @Component or @Service. 您必须确保Authentificator也是一个spring bean-我的意思是,您必须使用@Component或@Service之类的内容对其进行注释。 After this step you'll also have to “get” the Authentificator instance from spring instead of instantiating it with the new keyword. 完成此步骤后,您还必须从spring中“获取” Authentificator实例,而不是使用new关键字实例化它。

@Autowired does work only with the spring context. @Autowired确实仅在spring上下文中有效。 This means that it will work only with class instances which are managed by Spring. 这意味着它仅适用于由Spring管理的类实例。 Your Authentificator class is managed by you and Spring does not have to take care or know about it, it's not important for the Java Framework. 您的Authentificator类由您管理,Spring不必照顾或了解它,这对于Java Framework而言并不重要。

This is more of a configuration issue rather than an annotation issue. 这更多是配置问题,而不是注释问题。

If you want Spring to inject a field in Authenticator object the dependent object must be also created by Spring. 如果您想让Spring在Authenticator对象中注入一个字段,则依赖对象也必须由Spring创建。 You can do it by marking this class as a @Component or by creating a method with Authenticator return type marked with @Bean annotation. 您可以通过将此类标记为@Component或通过使用Authenticator返回类型标记有@Bean注释的方法来创建@Bean Then it must be injected somewhere. 然后必须将其注入某个位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM