Can't access application.properties file

2019-08-17 02:41发布

I have multiple projects in which I am trying to read application.properties file from one particular project. There's one web application which I am deploying on tomcat. I want to use application.properties from another project.

I tried to import appconfig.java from that project to webAppconfig.java.

application.properties file in 1st project src/main/resources/application.properties contains driver=org.postgresql.Driver

1st project : Appconfig.java

@Configuration
@PropertySource("classpath:application.properties")
public class Appconfig{

@Value("${driver}")
private String test;
}

2nd project : WebAppconfig.java

@Import(Appconfig.java)
public class WebAppconfig{

}

I was expecting test=org.postgresql.Driver but I am getting exception as key "driver" not found.

If I change application.properties to something.properties, it works.

I tried following ways:

a)

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

b) tried changing in build path as included, excluded as shown in image

enter image description here

c)I tried autowiring environment as well.but is didn't work.. I debugged environment variable ..under property sources.it shows{spring.application.name="project 2 name"}

0条回答
登录 后发表回答