How to set resources directory in eclipse dynamic web project?

4k views Asked by At

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

3

There are 3 answers

3
M. Deinum On BEST ANSWER

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.

@Configuration
@PropertySource("classpath:/properties/jdbc/jdbc.properties")
public class RootConfig {

    @Bean
    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.

0
Sheetal Mohan Sharma On

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.

classLoader.getResourceAsStream("/jdbc.properties");

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

AppServiceClass.class.getClassLoader().getResourceAsStream("/jdbc.properties");
0
Juneyoung Oh On

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.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // TODO Auto-generated method stub
    //super.addResourceHandlers(registry);
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);;
}   

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