如何在 Spring Boot 类之间正确注入 @Autowired?

我有一个 classA,它实现了一个 interfaceA,一个 methodA,然后我有一个 classB,我在其中使用 @Autowired 调用 classA 以便能够使用 methodA,但它给了我一个警告,我必须为它创建一个方法A类为什么会这样?在这种情况下,@Autowired 不是这样工作的吗?我应该实例化 classA 吗?非常感谢您的回答。

A 类

export class BarcodeDecoderError extends Error {
  constructor(
    message: string,public readonly barcode: string,public readonly errorType: ErrorType
  ) {
    super(message);
  }
}

接口A

@RequiredArgsConstructor
public class RepositoryImpl implements IRepository {

    @Autowired
    private final TransactionDataMapper transactionDataMapper;

    @Autowired
    private SpringDataColminvoice springDataColminvoice;

    @Override
    public <S extends TransactionDto> S save(S s) {
        
        Colm colm = transactionDataMapper.toEntity(s);

        //methodA
        springDataColminvoice.save(colm);
        return null;
    }
}

B 类

public interface IRepository extends IRepository<TransactionDto,Integer> {}

异常

@Service
@RequiredArgsConstructor
public class ServiceImpl implements IInvoiceService {

    @Autowired
    private RepositoryImpl repositoryImpl;
        
    @Override
    public void save(CMessage cMessage) throws HandlerException {
        try {

            TransactionDto transactionDto = cMessage.getTransaction();

            // methodA
            repositoryImpl.save(transactionDto);
        } catch (Exception e) {
            throw new HandlerException(e.getMessage());
        }
    }
}
xiaoshiyy 回答:如何在 Spring Boot 类之间正确注入 @Autowired?

(将此作为答案发布,因为我没有足够的声誉发表评论)

正如其他人已经指出的那样,代码示例会大有帮助。 尽管如此,听起来您似乎缺少“ClassA”的实现。

如果你有一个“ClassA”实现的接口,你必须在“ClassA”中实现接口的方法才能使用它们。

我认为您的代码目前看起来有点像这样?

public interface InterfaceA {

    void methodA();

}

public class ClassA implements InterfaceA {
}

public class ClassB {

    @Autowired
    ClassA classA; // Cannot use {@link InterfaceA#methodA} because the class does not implement the function

}

如果这是您的代码,请确保在“ClassA”中为“methodA()”函数添加实现。有点像这样:

public class ClassA implements InterfaceA {

    @Override
    public void methodA() {
    }

}

此外,为了在 Spring (Boot) 中自动装配,您需要确保您想要自动装配的类被标记为这样。您可以自动装配 bean。

要使示例中的“ClassA”符合自动装配条件,请确保将其实例化为:

  • 一个 bean(使用 @Bean 注释)。
  • 一个组件(使用 @Component 注释)。
  • 一项服务(使用 @Service 注释)。
  • 最适合您的用例的任何其他注释。

在我们的示例中,这看起来有点像这样:

@Component // Or @Service / whatever you may need
public class ClassA implements InterfaceA {

    @Override
    public void methodA() {
    }

}

希望这些对您有所帮助。一切顺利!

-T

,

据我所知,@Autowire 意味着注入特定属性的值/实例,在其中放置注释 @Autowire。在这种情况下,@Autowire 仅在您的 Spring Boot 项目的 basePackage 中存在可以匹配它的定义/创建的 Bean 时发生,即您的 @Autowire 引用的位置(意味着没有歧义等和数据类型的冲突问题) (Class) 可以隐式转换)。在您的示例中,首先将 IRepository 和/或 RepositoryImpl 视为 Repository,而不使用 @Repository 批注通知 Spring Boot 默认配置这是一个 Repository bean。由于您没有放置 POM.xml 或发布相关代码,我假设您正在创建自己的存储库类。我认为在这里发布您的依赖关系要好得多。

但正如其他人指出的那样。您需要创建一个可以匹配您在 TransactDataManager 和 SpringDataColminvoice 上放置的 @Autowired 的 bean。您还需要通过注释通知 Spring Boot 或注册您的类 A 是 Bean

@Bean - defining a regular bean,@Component - a Component in the Project,@Service - a Service in the Project,@Repository - a Repository (if you're using Spring JPA),etc. 
@<Other Annotations depending of what other Spring Project/Dependencies your using>

由于较新版本的 Spring 正在从 XML 映射转向基于注释的注释,因此我们需要根据角色使用上述示例注释为我们希望从 @Autowired 自动注入/实例化的每个类/对象添加适当的注释/你的类/对象的目的是。

如果您不确定,我建议您。然后使用公共注解@Bean 创建一个典型的bean。所以你的A班可能是

@Component //Insert proper Annotation for your class if necessary. This is just a sample
@RequiredArgsConstructor
public class RepositoryImpl implements IRepository {

    @Autowired
    private final TransactionDataMapper transactionDataMapper;

    @Autowired
    private SpringDataColminvoice 
    springDataColminvoice;//segunda

    @Override
    public <S extends TransactionDto> S save(S s) {
       //Some implementation
    }

    @Bean
    private TransactionDataMapper getTransactionDataMapper(<Some parameters if needed>){
       return <an instance for TransactionDataManager>;
    }

    @Bean
    private SpringDataColminvoice getSpringDataColmInvoice(<Some parameters if needed>){
       return <an instance for SpringDataColminvoice>;
    }
}

请注意,如果在外部类上已经定义了一个 Bean,或者它被其他注解(如@Service、@Component 或其他适当的注解)标记,并且另一个 bean 只是一个引用参数,则 2 个 bean 定义是可选的。其他 bean 以便正确实例化。

在您的 B 类中是以下内容:

public class ClassB {

    @Autowired
    ClassA classA; 

    /*Note: ignore this Bean definition if Class A is annotated with @Component,@Service,or other proper @Annotation for Class A.
    */
    @Bean
    private ClassA getClassA(<Some Parameters if Needed>){
       return <Instance of Class A>
    }

}

请注意,如果您为 A 类添加了适当的注释(如@Component、@Service 等),则无需在 B 类中放置 Bean 定义。

本文链接:https://www.f2er.com/1538.html

大家都在问