【问题标题】:Spring Autowiring a property within an autowired beanSpring在自动装配的bean中自动装配一个属性
【发布时间】:2015-05-06 14:54:23
【问题描述】:

我是 Spring 的新手。我遇到了 Spring-Boot 的问题。我正在尝试将外部配置文件中的字段自动装配到自动装配的 bean 中。我有以下课程

App.java

public class App {

@Autowired
private Service service;

public static void main(String[] args) {

    final SpringApplication app = new SpringApplication(App.class);
    //app.setShowBanner(false);
    app.run();
}

@PostConstruct
public void foo() {
    System.out.println("Instantiated service name = " + service.serviceName);
}
}

AppConfig.java

@Configuration
@ConfigurationProperties
public class AppConfig {

@Bean
public Service service()    {
    return new Service1();
}
}

服务接口

public interface Service {
    public String serviceName ="";
    public void getHistory(int days , Location location );
    public void getForecast(int days , Location location );
}

服务1

@Configurable
@ConfigurationProperties
public class Service1 implements Service {

@Autowired
@Value("${serviceName}") 
public String serviceName;
//Available in external configuration file.
//This autowiring is not reflected in the main method of the application.



public void getHistory(int days , Location location)
{
    //history code
}

public void getForecast(int days , Location location )
{
    //forecast code
}
}

我无法在 App 类的 postconstruct 方法中显示服务名称变量。我这样做对吗?

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您可以通过不同的方式加载属性:

    想象一下由 spring-boot 自动加载的以下 application.properties。

    spring.app.serviceName=Boot demo
    spring.app.version=1.0.0
    
    1. 使用@Value 注入值

      @Service
      public class ServiceImpl implements Service {
      
      @Value("${spring.app.serviceName}") 
      public String serviceName;
      
      }
      
    2. 使用@ConfigurationProperties 注入值

      @ConfigurationProperties(prefix="spring.app")
      public class ApplicationProperties {
      
         private String serviceName;
      
         private String version;
      
         //setters and getters
      }
      

    您可以使用 @Autowired 从另一个类访问此属性

    @Service
    public class ServiceImpl implements Service {
    
    @Autowired
    public ApplicationProperties applicationProperties;
    
    }
    

    您可以注意到前缀是 spring.app,然后 spring-boot 将匹配属性前缀并查找 serviceNameversion 并注入值。

    【讨论】:

      【解决方案2】:

      考虑到您的类 App@SpringBootApplicationApp 类注释在顶部包中您可以将 serviceName 放入 application.properties 并使用 @Value("${serviceName}") 注入它。如果您已经在配置上使用@Bean,请不要在类上使用@Component,它会发生冲突,因此@Autowired@Value

      更多信息请见docs

      你会以类似的方式结束

      @Service // @Component specialization
      public class Service1 implements Service {
      
      @Value("${serviceName}") 
      public String serviceName;
      //Available in external configuration file.
      //This autowiring is not reflected in the main method of the application.
      
      
      
      public void getHistory(int days , Location location)
      {
          //history code
      }
      
      public void getForecast(int days , Location location )
      {
          //forecast code
      }
      }
      

      当你有@Component/@Service/@Repository 时不需要@Bean 声明

      @Configuration
      public class AppConfig {  //other stuff here not duplicated beans  }
      

      还有你的主班

          package com.app;
      
          @SpringBootApplication // contains @EnableAutoConfiguration @ComponentScan @Configuration   
          public class App {
      
          @Autowired
          private Service service;
      
          public static void main(String[] args) {
      
              final SpringApplication app = new SpringApplication(App.class);
              //app.setShowBanner(false);
              app.run();
          }
      
          @PostConstruct
          public void foo() {
              System.out.println("Instantiated service name = " + service.serviceName);
          }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-05
        • 1970-01-01
        • 2011-01-23
        • 2016-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-22
        相关资源
        最近更新 更多