Java Spring自动关联自动关联bean中的属性
我是春天的新手。我的弹簧靴有问题。我正在尝试将一个字段从外部配置文件自动关联到一个自动关联bean中。我有以下课程 App.javaJava Spring自动关联自动关联bean中的属性,java,spring,spring-boot,Java,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.setSho
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方法中显示service name变量。我这样做对吗?考虑到您的class
App
带有@SpringBootApplication
和App
类的注释,您可以将serviceName
放在应用程序中。属性
并使用@Value(${serviceName}”)
注入它。不要在类上使用@Component
,如果您已经在配置上使用@Bean
,它将发生冲突,因此@Autowired
与@Value
有关更多信息,请参阅
你会以这样的方式结束
@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);
}
}
您可以通过不同的方式加载属性: 想象一下下面的application.properties由spring引导自动加载
spring.app.serviceName=Boot demo
spring.app.version=1.0.0
@Service
public class ServiceImpl implements Service {
@Value("${spring.app.serviceName}")
public String serviceName;
}
@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引导将属性前缀与之匹配,并查找serviceName
和version
,然后将注入值