[英]Spring Boot - Autowiring null inside abstract class
I have below classes.我有以下课程。
public interface Operation
public abstract class AbstractOperation implements Operation
public class Operation extends AbstractOperation
Class Operation班级运营
public interface Operation {
GenericClientResponse validateCustomerId();
Class AbstractOperation类抽象操作
public abstract class AbstractOperation implements Operation {
protected GenericClientRequest buildClientValidationRequest() {
return new GenericClientRequest();
}
In Operation class there are properties autowired.在 Operation 类中有自动装配的属性。
public class Operation extends AbstractOperation {
@Autowired
UserAccountsRepository userAccountsRepository; // not autowiring
@Autowired
WebClient.Builder webClientBuilder; // not autowiring
But none of these properties are autowired.但是这些属性都不是自动装配的。 They are null at run time.
它们在运行时为空。 But if i autowire these in my service class autowiring is successfully done.
但是如果我在我的服务类中自动装配这些自动装配成功完成。
@Service
@Transactional
public class TransactionServiceImpl implements TransactionService {
@Autowired
UserAccountsRepository userAccountsRepository; // Autowires successfully.
Please find repository class below.请在下面找到存储库类。
public interface UserAccountsRepository extends
JpaRepository<UserAccountsEntity, Integer> {
@Query("SELECT account FROM UserAccountsEntity account where
account.accountNo = :acc")
UserAccountsEntity getAccountByAccNo(@Param("acc") String userId);
In my code i keep a map of Operation classes and get related operation class according to a key.在我的代码中,我保留了操作类的映射并根据键获取相关的操作类。
operationMap.put("test_op1", new Operation());
operationMap.put("test_op2", new Operation2());
Then i call this to get correct operation然后我调用它来获得正确的操作
Optional.ofNullable(operationMap.get("test_op1"));
Now i undestands that creating new instance is causing null autowiring.现在我明白创建新实例会导致空自动装配。 How i get new object without new key word?
我如何在没有新关键字的情况下获得新对象?
You're probably instantiating Operation
with new
as follows:您可能正在使用
new
实例化Operation
,如下所示:
Operation operation = new Operation(...);
This is making your Operation
instance non-Spring managed.这使您的
Operation
实例不受 Spring 管理。 Hence, since Spring doesn't know about your instance, it cannot autowire your dependencies.因此,由于 Spring 不知道您的实例,因此它无法自动装配您的依赖项。
If you do like:如果你喜欢:
@Component
public class Operation {
...
}
you will get your dependencies autowired correctly.您将正确地自动装配依赖项。 If you really need to instantiate
Operation
yourself and keep the autowired dependencies, you'll need to go for Configurable
.如果您真的需要自己实例化
Operation
并保留自动装配的依赖项,则需要使用Configurable
。 See a couple of references:请参阅几个参考资料:
I can see a couple of possibilities, based on what you've shared:根据您分享的内容,我可以看到几种可能性:
The package structure is not correct - if the class Operation
isn't in the same package (or a sub-package) as the main
class, the component scan may pick your class.包结构不正确 - 如果类
Operation
与main
类不在同一个包(或子包)中,组件扫描可能会选择您的类。 Please refer this answer to make sure you've implemented the package structure correctly.请参考此答案以确保您已正确实施包结构。
You may be missing a Spring annotation on the class Operation
, such as @Component
or @Service
.您可能缺少类
Operation
上的 Spring 注释,例如@Component
或@Service
。 Or you may be instantiating the class manually in you code, outside of the Spring application context.或者您可能在 Spring 应用程序上下文之外的代码中手动实例化该类。 Please verify that you're creating the bean properly.
请验证您是否正确创建了 bean。
In case you're trying to create the bean manually, you should do it using @Bean
in a configuration class.如果您尝试手动创建 bean,则应该在配置类中使用
@Bean
来完成。
I have below classes.我有以下课程。
public interface Operation
public abstract class AbstractOperation implements Operation
public class Operation extends AbstractOperation
Class Operation Class 操作
public interface Operation {
GenericClientResponse validateCustomerId();
Class AbstractOperation Class 抽象操作
public abstract class AbstractOperation implements Operation {
protected GenericClientRequest buildClientValidationRequest() {
return new GenericClientRequest();
}
In Operation class there are properties autowired.在操作 class 中有自动装配的属性。
public class Operation extends AbstractOperation {
@Autowired
UserAccountsRepository userAccountsRepository; // not autowiring
@Autowired
WebClient.Builder webClientBuilder; // not autowiring
But none of these properties are autowired.但是这些属性都不是自动装配的。 They are null at run time.
它们在运行时是 null。 But if i autowire these in my service class autowiring is successfully done.
但是如果我在我的服务中自动装配这些 class 自动装配成功完成。
@Service
@Transactional
public class TransactionServiceImpl implements TransactionService {
@Autowired
UserAccountsRepository userAccountsRepository; // Autowires successfully.
Please find repository class below.请在下面找到存储库 class。
public interface UserAccountsRepository extends
JpaRepository<UserAccountsEntity, Integer> {
@Query("SELECT account FROM UserAccountsEntity account where
account.accountNo = :acc")
UserAccountsEntity getAccountByAccNo(@Param("acc") String userId);
In my code i keep a map of Operation classes and get related operation class according to a key.在我的代码中,我保留了操作类的 map 并根据密钥获取相关操作 class。
operationMap.put("test_op1", new Operation());
operationMap.put("test_op2", new Operation2());
Then i call this to get correct operation然后我调用它以获得正确的操作
Optional.ofNullable(operationMap.get("test_op1"));
Now i undestands that creating new instance is causing null autowiring.现在我不明白创建新实例会导致 null 自动装配。 How i get new object without new key word?
我如何在没有新关键字的情况下获得新的 object?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.