简体   繁体   English

Spring:使用@Bean 创建 bean

[英]Spring : creating beans with @Bean

I have an issue with beans creations:我对豆类创作有疑问:

@Bean
Service carService() {
  return new Service(new CarRepository(), "car");
}

My issue is that CarRepository class has a dependency to EntityManager em variable (with @PersistenceContext )我的问题是CarRepository class依赖于EntityManager em变量(使用@PersistenceContext

So if I use new operator, I'm missing this dependency (because I'm instanciating myself the bean).因此,如果我使用new运算符,我将缺少这种依赖性(因为我正在实例化自己的 bean)。

Also I have many services (CarService, BikeService etc...) and many repositories too (CarRepository, BikeRepository etc...).我还有许多服务(CarService、BikeService 等)和许多存储库(CarRepository、BikeRepository 等)。 So using annotations directly in classes seems difficult.所以直接在类中使用注解似乎很难。

So any solutions?那么有什么解决办法吗?

I think you need to annotate every repository class with @Repository annotation.我认为您需要使用 @Repository 注释对每个存储库@Repository进行注释。 And every service class with @Service .每个服务 class 都带有@Service

In Spring you should not use the new operator for Services.在 Spring 中,您不应使用新的服务运营商。 Use the annotation使用注解

@Service 
public classSomeClass {

or或者

@Component
public classSomeClass {

and your class can be injected via depnendency Injection.并且您的 class 可以通过依赖注入进行注入。 If you want to create a new custom bean that can be used via dependencyInjection This is what the @Configuration annotation is for.如果您想创建一个可以通过 dependencyInjection 使用的新自定义 bean,这就是 @Configuration 注释的用途。

@Configuration
public class ConfigurationClass{

    @Bean
    public SomeClass createSomeClass(){
      return new SomeClass();
    }
} 

Simple.简单的。 Pass your repository as dependency into your Bean factory function:将您的存储库作为依赖项传递到您的 Bean 工厂 function:

@Bean
Service carService(final CarRepository carRepository) {
  return new Service(carRepository, "car");
}

The repository needs to exist as a bean itself.存储库需要作为 bean 本身存在。 You can create the repository bean in another bean method of a configuration class, or by annotating the class and having it created during component scanning.您可以在配置 class 的另一个 bean 方法中创建存储库 bean,或者通过注释 class 并在组件扫描期间创建它。

暂无
暂无

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

相关问题 使用 @Bean Spring 注释创建 bean 时出现异常 - Getting Exception while creating beans using @Bean Spring annotation org.springframework.beans.factory.BeanCreationException:在 Spring 中创建 bean 时出错 - org.springframework.beans.factory.BeanCreationException: Error creating bean in Spring Spring:使用工厂bean创建任意数量的bean - Spring: Creating an arbitrary number of beans using a factory bean spring - 启动(创建 bean 时出错) - org.springframework.beans.factory.UnsatisfiedDependencyException:使用名称创建 bean 时出错 - spring - boot (error while creating beans ) - org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 在春季有条件地制作豆子 - Conditionally creating beans in spring 努力在春天创造豆子 - struggling with creating beans in spring 春天:'org.springframework.beans.factory.UnsatisfiedDependencyException'。 创建带有名称的 bean 时出错 - Spring: 'org.springframework.beans.factory.UnsatisfiedDependencyException' . Error creating bean with name 春季错误:org.springframework.beans.factory.BeanCreationException:创建URL中定义的名称为'entityManagerFactory'的bean时出错 - Spring error: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL Spring Boot 项目“org.springframework.beans.factory.BeanCreationException:创建名为‘entityManagerFactory’的 bean 时出错”错误 - Spring Boot Project "org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'entityManagerFactory'" ERROR JAVA SPRING - org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“employeeController”的 bean 时出错: - JAVA SPRING - org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController':
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM