Spring 在自动装配的 bean 中自动装配 属性

Spring Autowiring a property within an autowired bean

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

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
}
}

我无法在应用程序的构建后方法中显示服务名称变量class。我这样做对吗?

考虑到你的 class App 在 top 包中用 @SpringBootApplicationApp class 注释 你可以把你的 serviceNameapplication.properties 内并使用 @Value("${serviceName}") 注入它。如果您已经在配置上使用 @Bean,请不要在 class 上使用 @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  }

还有你的主要 class

    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);
    }
    }

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

假设下面的application.properties是由spring-boot.

自动加载的
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

从另一个 class 访问此属性
@Service
public class ServiceImpl implements Service {

@Autowired
public ApplicationProperties applicationProperties;

}

正如您所注意到的,前缀将是 spring.app 然后 spring-boot 将匹配属性前缀并查找 serviceNameversion 并且值将被注射。