How to set resources directory in eclipse dynamic web project?

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... which intend to replace 'root-context.xml'...

public class RootConfig {

    private String jdbcDriverClassName;

    private String jdbcUrl;

    private String jdbcUsername;

    private String jdbcPassword;

    private static final String RESOURCE_LOCATION = "resources";

    public static PropertyPlaceholderConfigurer propertyPlaceholder() {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        Resource[] resources = new ClassPathResource[]{ 
                new ClassPathResource(RESOURCE_LOCATION + File.separator + "properties"
                        + File.separator + "jdbc" + File.separator + "")
        return ppc;

    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        return dataSource;

and this is which suppose to be replacement of 'servlet-context.xml'...

    basePackages= {
            , "com.gosports.common.controller"
            , "com.gosports.test.controller"
    , excludeFilters=@ComponentScan.Filter(Configuration.class)
public class ServletConfig extends WebMvcConfigurerAdapter {

    public ViewResolver viewResolver () {
        InternalResourceViewResolver vResolver = new InternalResourceViewResolver();
        return vResolver;

Finally, My initializer...

public class WASInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext arg0) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

        Dynamic servlet = arg0.addServlet("dispatcher", new DispatcherServlet(context));

and it works since I put my '' 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 a PropertySOurcesPlaceholderConfigurer instead. Next instead of loading your files yourself, just use @PropertySource on your class. Put your files in src/main/resources and then maven will add it to the class path.

public class RootConfig {

    public static PropertySources PlaceholderConfigurer propertyPlaceholder() {
        return new PropertySourcesPlaceholderConfigurer();

This assumes that all your properties are in src/main/resources in the directory structure as you created.

You can get the classLoader as returned by Thread#getContextClassLoader().

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

The /WEB-INF folder is not in the root of the classpath but its /WEB-INF/classes folder from where you need to load the properties files relative to.


Or you could read them as resource which shall always work.


Thank for your answers :D

but maybe my explanation was not enough, so I have to find my way out. Following is my solution to set resources folder for Dynamic Web Project.

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // TODO Auto-generated method stub

Frankly, I under stood till addResourceLocations, but did not about setCachePeriod. This was a solution I was looking for.

Need Your Help

mysql_num_rows() expects parameter 1 to be resource, boolean error message in PHP

php mysql

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given on line 16

Objective C -- initializing inherited variables of a subclass object

objective-c object initialization subclass

I have a class MyClass. I am exaggerating here, but let's say MyClass has 1000 instance variables. I then create a subclass called MySubClass with all the instance variables MyClass has, plus one...