[英]Spring Autowiring a property within an autowired bean
I'm a newbie to Spring. 我是春天的新手。 I'm facing a problem with Spring-Boot.
我遇到了Spring-Boot的问题。 I'm trying to autowire a field from an external config file into an autowired bean.
我正在尝试将外部配置文件中的字段自动装入自动装配的bean中。 I have the following classes
我有以下课程
App.java 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 AppConfig.java
@Configuration
@ConfigurationProperties
public class AppConfig {
@Bean
public Service service() {
return new Service1();
}
}
Service Interface 服务接口
public interface Service {
public String serviceName ="";
public void getHistory(int days , Location location );
public void getForecast(int days , Location location );
}
Service1 服务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
}
}
I'm unable to display the service name variable in the postconstruct method of the App class. 我无法在App类的postconstruct方法中显示服务名称变量。 Am I doing this right?
我这样做了吗?
You can load properties in different ways: 您可以通过不同方式加载属性:
Imagine the following application.properties which is automatically loaded by spring-boot. 想象一下以下由spring-boot自动加载的application.properties。
spring.app.serviceName=Boot demo
spring.app.version=1.0.0
Inject values using @Value 使用@Value注入值
@Service public class ServiceImpl implements Service { @Value("${spring.app.serviceName}") public String serviceName; }
Inject values using @ConfigurationProperties 使用@ConfigurationProperties注入值
@ConfigurationProperties(prefix="spring.app") public class ApplicationProperties { private String serviceName; private String version; //setters and getters }
You can access to this properties from another class using @Autowired
您可以使用
@Autowired
从另一个类访问此@Autowired
@Service
public class ServiceImpl implements Service {
@Autowired
public ApplicationProperties applicationProperties;
}
As you can notice the prefix will be spring.app
then spring-boot will match the properties prefix with that and look for serviceName
and version
and values will be injected. 您可以注意到前缀是
spring.app
然后spring-boot将匹配属性前缀,并查找serviceName
,并注入version
和值。
Considering you have you class App
annotated with @SpringBootApplication
and App
class in the top package You can put your serviceName
inside application.properties
and inject it using @Value("${serviceName}")
. 考虑到你有你的类
App
注解为@SpringBootApplication
和App
在顶部封装类,你可以把你的serviceName
里面application.properties
并使用它注入@Value("${serviceName}")
Do not use @Component
on a class if you are already using @Bean
on configuration it will clash, and so @Autowired
with @Value
不要在
@Component
上使用@Component
,如果你已经在配置上使用@Bean
它会发生冲突,所以@Autowired
with @Value
See docs for more info 有关详细信息,请参阅文档
You will end with something like 你会以类似的东西结束
@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
}
}
No need for @Bean declaration when you have @Component/@Service/@Repository 当你有@ Component / @ Service / @ Repository时,不需要@Bean声明
@Configuration
public class AppConfig { //other stuff here not duplicated beans }
And your main 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.