0

I could not load the db.properties located inside the jar (project.service.jar), which is referred by the war file (project-web.war). The output war has the jar inside WEB-INF/lib/project.service.jar. The services jar has the services and dao classes used by the web app. The jar is also used by the rest war which uses some of the services from the jar. When the war starts I could not load the db.properties located in the jar file of the services. The prop file has the datasource name to lookup in jndi.

war config

AppConfig.java

@Bean
public static PropertyPlaceholderConfigurer propertyConfigurer()
        throws IOException {
    PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
    props.setLocations(new Resource[] { new ClassPathResource(
            "application.properties") });
    return props;
}

service.jar

@Component
@Configuration
//@ComponentScan(basePackages={ "com.mgage.mvoice" })
@PropertySource(value = { "classpath:db.properties" })
public class DatabaseConfig {
    @Autowired
    Environment environment;

    private @Value(value = "${voice.datasource}") String dataSourceString;

    @Bean
public DataSource getDataSource() {
    JndiDataSourceLookup lookup = new JndiDataSourceLookup();
    try {
        DataSource dataSource = lookup.getDataSource(dataSourceString); //this always is null
        return dataSource;
    } catch (DataSourceLookupFailureException e) {
        return null;
    }
}

    @Bean
    public PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

should AppConfig.java import the DataSourceConfig.java, I tried but I still get the Environment null. Am I missing anything? Is it right to separate the services as jar file to make them available for REST, Web app and Reports applications? Or is it advisable to have a single war with all services, rest, reports classes?

vvra
  • 2,832
  • 5
  • 38
  • 82

2 Answers2

0

AFAIK, a spring application context uses only one PropertySourcesPlaceholderConfigurer. As, the @PropertySource(value = { "classpath:db.properties" }) does not seem to populate the environment, I can see only one other solution : explicitely add it to the list of locations in AppConfig.java :

    props.setLocations(new Resource[] { new ClassPathResource(
        "application.properties"), new ClassPathResource("db.properties")});

But I must admit this is more a workaroud than a clean solution.

EDIT :

But normally, is DataSourceConfig.java is scanned by spring configuration machinery, the @PropertySource(value = { "classpath:db.properties" }) should populate the environment.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

you need to specify classpath within any jar, try this: @PropertySource(value = { "classpath*:db.properties" })

Jason
  • 526
  • 7
  • 9