@Value annotation is not set properly

Here is my application:

public static void main( String[] args ) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);

    //run the importer
    final ImportNewOrders importer = (ImportNewOrders) ApplicationContextProvider.getApplicationContext().getBean("importNewOrders");
    importer.run();
    //importer.runInBackground();
}

Here is my configuration:

@Configuration
@ComponentScan(basePackages = {
        "com.production"
})
@PropertySource(value = {
        "classpath:/application.properties",
        "classpath:/environment-${MY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.fettergroup.production.repositories")
@EnableTransactionManagement
public class Config {

    .... skipping things that aren't relevant

    @Bean
    public ImportNewOrders importNewOrders() {
        return new ImportNewOrders();
    }

Here is my class ...

@Component
public class ImportNewOrders implements Task {

    private final static Logger logger = Logger.getLogger(ImportNewOrders.class.getName());

    @Autowired
    private OrderService orderService;

    @Autowired
    private ImportOrderRequest importOrderRequest;

    @Value("${api.user}")
    private String apiUser;

    @Value("${api.password}")
    private String apiPassword;

    @Value("${api.orders.pingFrequency}")
    private String pingFrequency;

And finally application.properties:

# ------------------- Application settings -------------------

#Base URL to the API application
api.baseUrl=http://localhost:9998

#Unique token used to authenticate this vendor
api.vendor.token=asdf

#API credentials
api.user=myuser
api.password=mypassword

#How often to check for new orders; frequency is in seconds
api.orders.pingFrequency=60

It worked an hour or two ago, now he decided that he did not like these values. I do not understand why. Everything looks right to me.

Update

@Configuration
@ComponentScan(basePackages = {
        "com.production"
})
@PropertySource(value = {
        "classpath:/application.properties",
        "classpath:/environment-${MY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.production.repositories")
@EnableTransactionManagement
public class Config {
    @Value("${db.url}")
    private static String PROPERTY_DATABASE_URL;

    @Bean
    public DataSource dataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();

        dataSource.setUrl(PROPERTY_DATABASE_URL); //is null
        /*dataSource.setUser(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USER));
        dataSource.setPassword(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));*/

        return dataSource;
    }

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer () {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
0
source share
1 answer

Your properties file is found by yours @Configurationand uses it for your database properties in this class because of @PropertySource. But for the fields @Value, and ${}it takes more.

From Javadoc to@PropertySource

${...} @ , PropertySource, PropertySourcesPlaceholderConfigurer. XML, @ Bean @Configuration. . " " @Configuration Javadoc " BeanFactoryPostProcessor-return @Bean " @Bean Javadoc .

,

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
    p.setLocation(new ClassPathResource("your properties path"));
    // other properties
    return p;
}

, , , @PropertySource, setLocation:

@Configuration
@PropertySource(value="classpath:your_file.properties")
public class MyConfiguration{

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()    {
        PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
    return p;
    }
}

, PropertySourcesPlaceholderConfigurer

beans > , , , ${...} PropertySourcesPlaceholderConfigurer, EnvironmentAware Spring 3.1 , < : -/" > .

+6

Source: https://habr.com/ru/post/1544027/


All Articles