简体   繁体   English

从外部类调用时解析@Value失败-Spring Java

[英]resolving @Value fails when invoked from external class - spring java

In the property file (config.properties) I defined several properties: 在属性文件(config.properties)中,我定义了几个属性:

my.property.first=xyz
my.property.second=12

I created a class to read these properties: 我创建了一个类来读取这些属性:

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

Now I want to use these properties in a class placed in some other package: 现在,我想在放置在其他包中的类中使用这些属性:

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

I try to solve this by using only Annotations. 我尝试通过仅使用注释来解决此问题。

I use Spring 4. 我使用Spring 4。

Thanks 谢谢

MyProperties isn't really a @Configuration class, it is a bean. MyProperties实际上不是@Configuration类,它是一个bean。 Give it the annotation @Component and move the @Bean declaration to another @Configuration class. 给它注解@Component并将@Bean声明移到另一个@Configuration类。 It doesn't matter where the bean is defined for spring (as long as it is in a configuration class), it'll pick it up if you have the @ComponentScan or @SpringBootApplication somewhere in your app, which I'm pretty sure you do. 春季为bean定义的位置无关紧要(只要它在配置类中),如果您的应用程序中有@ComponentScan或@SpringBootApplication,它将选择它。你做。 your MyProperties class you make @Component 您制作的MyProperties类@Component

You'll want: 您需要:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring TransactionProxyFactoryBean仅在从外部类调用方法时才兑现交易 - Spring TransactionProxyFactoryBean honors transactions only when method is invoked from an external class Spring:使用@Value注释时,Bean无法从外部属性文件中读取值 - Spring: Bean fails to read off values from external Properties file when using @Value annotation Spring @Value 未解析为属性文件中的值 - Spring @Value is not resolving to value from property file Spring 批处理:Tasklet 的测试用例 - 从测试 class 调用时,密钥未出现在实际 class 中 - Spring batch:Test case for Tasklet - Key is not appearing in actual class when it is invoked from Test class 在新线程中调用连接点时,Spring Aspect失败 - Spring Aspect fails when join point is invoked in new thread 从java类调用的重定向页面 - redirect page invoked from java class 是否可以从普通的Java类调用控制器 - Can a Controller be Invoked from a normal Java Class Spring Security - 从 angular 4 客户端登录时不会调用 UserDetailsS​​ervice 实现类 - Spring Security - UserDetailsService implementation class is not invoked when logging in from angular 4 client Spring @Value注释无法解析 - Spring @Value Annotation Not Resolving 从另一个类调用时,Java不会改变外观 - Java doesn't change look and feel when invoked from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM