parent-child contexts, properties file included in each

This commit is contained in:
eugenp 2014-01-26 14:41:33 +02:00
parent 5b91988d0e
commit c3820519cb
10 changed files with 83 additions and 12 deletions

View File

@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription> <beansProjectDescription>
<version>1</version> <version>1</version>
<pluginVersion><![CDATA[3.3.0.201307091516-RELEASE]]></pluginVersion> <pluginVersion><![CDATA[3.4.0.201310051539-RELEASE]]></pluginVersion>
<configSuffixes> <configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix> <configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes> </configSuffixes>
<enableImports><![CDATA[false]]></enableImports> <enableImports><![CDATA[false]]></enableImports>
<configs> <configs>
<config>src/main/webapp/WEB-INF/api-servlet.xml</config>
</configs> </configs>
<configSets> <configSets>
</configSets> </configSets>

View File

@ -2,6 +2,7 @@ package org.baeldung.config;
import java.util.Set; import java.util.Set;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletRegistration; import javax.servlet.ServletRegistration;
@ -9,6 +10,7 @@ import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer; import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
public class MainWebAppInitializer implements WebApplicationInitializer { public class MainWebAppInitializer implements WebApplicationInitializer {
@ -43,6 +45,11 @@ public class MainWebAppInitializer implements WebApplicationInitializer {
if (!mappingConflicts.isEmpty()) { if (!mappingConflicts.isEmpty()) {
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
} }
// spring security filter
final DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy("springSecurityFilterChain");
final Dynamic addedFilter = sc.addFilter("springSecurityFilterChain", springSecurityFilterChain);
addedFilter.addMappingForUrlPatterns(null, false, "/*");
} }
} }

View File

@ -2,8 +2,10 @@ package org.baeldung.config.child;
import java.util.List; import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -12,6 +14,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
@ComponentScan("org.baeldung.web") @ComponentScan("org.baeldung.web")
// @ImportResource({ "classpath:prop.xml" })
// @PropertySource("classpath:foo.properties")
public class WebConfig extends WebMvcConfigurerAdapter { public class WebConfig extends WebMvcConfigurerAdapter {
public WebConfig() { public WebConfig() {
@ -26,6 +30,13 @@ public class WebConfig extends WebMvcConfigurerAdapter {
converters.add(new MappingJackson2HttpMessageConverter()); converters.add(new MappingJackson2HttpMessageConverter());
} }
// // beans
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
} }

View File

@ -1,14 +1,28 @@
package org.baeldung.config.parent; package org.baeldung.config.parent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration @Configuration
@ComponentScan("org.baeldung.service") @ComponentScan("org.baeldung.service")
// @ImportResource({ "classpath:prop.xml" })
@PropertySource("classpath:foo.properties")
public class ServiceConfig { public class ServiceConfig {
public ServiceConfig() { public ServiceConfig() {
super(); super();
} }
// beans
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
} }

View File

@ -1,10 +1,20 @@
package org.baeldung.service; package org.baeldung.service;
import org.baeldung.web.dto.Foo; import org.baeldung.web.dto.Foo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class FooService implements IFooService { public class FooService implements IFooService, InitializingBean {
@Value("${foo1}")
private String foo1;
@Autowired
private Environment env;
public FooService() { public FooService() {
super(); super();
@ -17,4 +27,10 @@ public class FooService implements IFooService {
return new Foo(); return new Foo();
} }
@Override
public final void afterPropertiesSet() {
System.out.println("In Parent Context, property via @Value = " + foo1);
System.out.println("In Parent Context, property via env = " + env.getProperty("foo2"));
}
} }

View File

@ -2,7 +2,10 @@ package org.baeldung.web.controller;
import org.baeldung.service.IFooService; import org.baeldung.service.IFooService;
import org.baeldung.web.dto.Foo; import org.baeldung.web.dto.Foo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -10,8 +13,14 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@Controller @Controller
@RequestMapping(value = "/foo") @RequestMapping(value = "/foos")
public class FooController { public class FooController implements InitializingBean {
@Value("${foo1}")
private String foo1;
@Autowired
private Environment env;
@Autowired @Autowired
private IFooService service; private IFooService service;
@ -28,4 +37,10 @@ public class FooController {
return service.findOne(id); return service.findOne(id);
} }
@Override
public final void afterPropertiesSet() {
System.out.println("In Child Context, property via @Value = " + foo1);
System.out.println("In Child Context, property via env = " + env.getProperty("foo2"));
}
} }

View File

@ -0,0 +1,2 @@
foo1=bar1
foo2=bar2

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:property-placeholder location="classpath:foo.properties" />
</beans>

View File

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
</beans>

View File

@ -37,7 +37,6 @@
<url-pattern>/api/*</url-pattern> <url-pattern>/api/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<!-- Spring Security --> <!-- Spring Security -->
<filter> <filter>
<filter-name>springSecurityFilterChain</filter-name> <filter-name>springSecurityFilterChain</filter-name>