I want to build a web project which uses minimum libraries. So what I did is just described below
- Make a Dynamic Web Project in Eclipse(Luna)
- Configure as Maven Project
- And import some libraries in pom.xml
- Implement
WebApplicationInitializer
I made 2 configuration classes and initializer it looks like...
RootConfig.java
which intend to replace 'root-context.xml'...
@Configuration
public class RootConfig {
@Value(value="${jdbc.driverClassName}")
private String jdbcDriverClassName;
@Value(value="${jdbc.url}")
private String jdbcUrl;
@Value(value="${jdbc.username}")
private String jdbcUsername;
@Value(value="${jdbc.password}")
private String jdbcPassword;
private static final String RESOURCE_LOCATION = "resources";
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholder() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[]{
new ClassPathResource(RESOURCE_LOCATION + File.separator + "properties"
+ File.separator + "jdbc" + File.separator + "jdbc.properties")
};
ppc.setLocations(resources);
return ppc;
}
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(this.jdbcDriverClassName);
dataSource.setUrl(this.jdbcUrl);
dataSource.setUsername(this.jdbcUsername);
dataSource.setPassword(this.jdbcPassword);
return dataSource;
}
};
and this is ServletConfig.java
which suppose to be replacement of 'servlet-context.xml'...
@Configuration
@EnableWebMvc
@EnableAsync
@ComponentScan(
basePackages= {
"com.gosports.api.controller"
, "com.gosports.common.controller"
, "com.gosports.test.controller"
}
, [email protected](Configuration.class)
)
public class ServletConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver () {
InternalResourceViewResolver vResolver = new InternalResourceViewResolver();
vResolver.setPrefix("/WEB-INF/views/");
vResolver.setSuffix(".jsp");
return vResolver;
}
}
Finally, My initializer...
public class WASInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext arg0) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(RootConfig.class);
context.register(ServletConfig.class);
context.setServletContext(arg0);
Dynamic servlet = arg0.addServlet("dispatcher", new DispatcherServlet(context));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
};
and it works since I put my 'jdbc.properties' file in /{project_location}/src/resources/properties/
.
But what I want to do is put this file in /WEB-INF/resources/properties/
.
I can not absolute path in code, considering distribution.
I need to refer relational location from project server. There are some ways to find that directory with HttpServletRequest
and ServletContext
, but I can not do like that because those part- propertyPlaceholder()
part - is static method.
Is there any good example or solution to do that? I really do not want to use Spring Templates or xml. Is it possible what I am trying to do with only java files?
Thanks for you answer :D
First you shouldn't be using a
PropertyPlaceholderConfigurer
anymore but aPropertySOurcesPlaceholderConfigurer
instead. Next instead of loading your files yourself, just use@PropertySource
on your class. Put your files insrc/main/resources
and then maven will add it to the class path.This assumes that all your properties are in
src/main/resources
in the directory structure as you created.