Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import properties file within shared jar in Spring?

Tags:

java

spring

I'd like to create a shared project jar, that has some services. These service should make use of a properties file.

Ideally I just want to make use of the services lateron when I add the shared jar as a dependency to other projects. I don't want to make any further configuration like importing the shared property file or so.

Within the shared jar, I'd like to inject the properties using Spring. But how can I do this?

project-commons/src/main/java:

@Service
public class MyService {
    @Value("${property.value}") private String value;

    public String getValue() {
        return value;
    }
}

project-commons/src/main/resources/application.properties:

property.value=test

project-web/src/main/java:

@Component
public class SoapService {
    @Autowired
    private MyService service;

    //should return "test"
    public String value() {
        return service.getValue();
    }
}

When I run it:

Illegal character in path at index 1: ${property.value}

So, the propertyfile is not resolved. But how can I tell spring to automatically use it when using the appropriate service?

like image 244
membersound Avatar asked Sep 30 '22 06:09

membersound


1 Answers

Thanks to the comment of Hank Lapidez, adding the following statement fixes the problem:

@Configuration
@PropertySource("classpath:appdefault.properties")
public CommonConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
like image 131
membersound Avatar answered Nov 14 '22 22:11

membersound