简体   繁体   English

Spring Boot - 在抽象类中自动装配 null

[英]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:根据您分享的内容,我可以看到几种可能性:

  1. 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.包结构不正确 - 如果类Operationmain类不在同一个包(或子包)中,组件扫描可能会选择您的类。 Please refer this answer to make sure you've implemented the package structure correctly.请参考答案以确保您已正确实施包结构。

  2. 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.

相关问题 春季自动装配为Abstract类提供了空bean - Spring autowiring gives null bean for Abstract class Spring Boot自动装配在配置类中提供空值 - Spring boot autowiring giving null in configuration class Spring Boot无法自动装配测试类 - Spring Boot not autowiring class in test 为mapstruct制作的抽象类内部自动装配 - Autowiring inside abstract class made for mapstruct Spring Boot中的继承:@Autowired字段为空(需要在非Spring托管类中自动装配 - 多个实例) - Inheritance in Spring Boot: @Autowired fields are null (need autowiring in non spring managed class - multiple instances) java-在Spring Boot中实例化的类中的自动装配库 - java - Autowiring Repository in class that is instantiated in Spring Boot spring-具有范围原型的抽象类中的自动装配接口不起作用 - spring - autowiring interface in abstract class with scope prototype does not work Spring batch + Item reader:在Item-reader-Implementation类中获取自动装配的空值 - Spring batch + Item reader : Getting null value for autowiring inside Item-reader-Implementation class 在 Spring Boot 中自动装配枚举 - Autowiring Enums in Spring boot 春季启动:在自定义UserDetailsS​​ervice中自动装配authenticationManager时,委托构建器不能为null - Spring boot : delegateBuilder cannot be null on autowiring authenticationManager in custom UserDetailsService
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM