从外部类调用时解析@Value失败-spring java
在属性文件(config.properties)中,我定义了几个属性:从外部类调用时解析@Value失败-spring java,java,spring,Java,Spring,在属性文件(config.properties)中,我定义了几个属性: my.property.first=xyz my.property.second=12 package my.package.first; ............ @Configuration public Class MyProperties { @Value("${my.property.first}") private String propertyFirst; @Value ("${my.p
my.property.first=xyz
my.property.second=12
package my.package.first;
............
@Configuration
public Class MyProperties {
@Value("${my.property.first}") private String propertyFirst;
@Value ("${my.property.second}") private String propertySecond;
public String getPropertyFirst() {
return propertyFirst;
}
public int getPropertySecond() {
return propertySecond
}
@Bean
public MyProperties getInstance() {
return this;
}
}
我创建了一个类来读取以下属性:
my.property.first=xyz
my.property.second=12
package my.package.first;
............
@Configuration
public Class MyProperties {
@Value("${my.property.first}") private String propertyFirst;
@Value ("${my.property.second}") private String propertySecond;
public String getPropertyFirst() {
return propertyFirst;
}
public int getPropertySecond() {
return propertySecond
}
@Bean
public MyProperties getInstance() {
return this;
}
}
现在我想在放置在其他包中的类中使用这些属性:
package my.otherpackage.third;
import my.property.package.first.MyProperties;
.............................
public class GetMyProperties{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyProperties.class);
MyProperties myProperties =context.getBean("getInstance",MyProperties.class);
//This returns ${my.property.first}
String propertyFirst = myProperties.getPropertyFirst();
// This returns ${my.property.second}
int propertySecond = context.getBeanFactory().resolveEmbeddedValue(("${my.property.first}"));
}
我试图通过只使用注释来解决这个问题
我用弹簧4
谢谢MyProperties不是一个真正的@Configuration类,它是一个bean。给它一个注解@Component,并将@Bean声明移动到另一个@Configuration类。无论bean是在哪里为spring定义的(只要它在配置类中),如果你的应用程序中有@ComponentScan或@SpringBootApplication,它都会选择它,我很肯定你会这样做。您创建的MyProperties类@Component 您将需要:
@Component
public class MyProperties {
@Value("${my.property.first}") private String propertyFirst;
@Value ("${my.property.second}") private String propertySecond;
public String getPropertyFirst() {
return propertyFirst;
}
public void setPropertyFirst(String propertyFirst){
this.propertyFirst = propertyFirst;
}
public int getPropertySecond() {
return propertySecond
}
public void setPropertySecond(String propertySecond){
this.propertySecond= propertySecond;
}
}
@Configuration
public class Config {
@Bean
public MyProperties getInstance() {
return new MyProperties();
}
}
package my.otherpackage.third;
import my.property.package.first.MyProperties;
.....
@EnableAutoConfiguration
@ComponentScan(basePackages = {"my"})
public class GetMyProperties{
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(GetMyProperties.class);
MyProperties myProperties =context.getBean("getInstance",MyProperties.class);
//This returns the value of ${my.property.first}
String propertyFirst = myProperties.getPropertyFirst();
// This returns the value of ${my.property.second}
int propertySecond = myProperties.getPropertySecond();
}
}
我添加bean getInstance()只是为了获取myProperties实例。如果我删除这个bean,我如何访问GetMyProperties类中的属性?在另一个地方定义这个bean,你的bean不能同时是一个配置和一个bean。不管bean是在哪里为spring定义的(只要它在一个配置类中),如果你的应用程序中有\@ComponentScan或\@SpringBootApplication,它都会选择它,我敢肯定你是这样做的。我的意思是,有没有一种方法可以不使用这个bean就访问这些属性呢?你可以在每个bean中访问这些属性,从而在每个带有注释的类中访问这些属性。@Component、.@Service、.@Repository或@Controller,方法是像你这样使用@Value注释。我添加了这两个注释(@Component和@Configure)到MyProperties类-它不起作用。