Skip to content

Commit c382051

Browse files
author
eugenp
committed
parent-child contexts, properties file included in each
1 parent 5b91988 commit c382051

10 files changed

Lines changed: 83 additions & 12 deletions

File tree

spring-security-rest-custom/.springBeans

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beansProjectDescription>
33
<version>1</version>
4-
<pluginVersion><![CDATA[3.3.0.201307091516-RELEASE]]></pluginVersion>
4+
<pluginVersion><![CDATA[3.4.0.201310051539-RELEASE]]></pluginVersion>
55
<configSuffixes>
66
<configSuffix><![CDATA[xml]]></configSuffix>
77
</configSuffixes>
88
<enableImports><![CDATA[false]]></enableImports>
99
<configs>
10-
<config>src/main/webapp/WEB-INF/api-servlet.xml</config>
1110
</configs>
1211
<configSets>
1312
</configSets>

spring-security-rest-custom/src/main/java/org/baeldung/config/MainWebAppInitializer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import java.util.Set;
44

5+
import javax.servlet.FilterRegistration.Dynamic;
56
import javax.servlet.ServletContext;
67
import javax.servlet.ServletException;
78
import javax.servlet.ServletRegistration;
89

910
import org.springframework.web.WebApplicationInitializer;
1011
import org.springframework.web.context.ContextLoaderListener;
1112
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
13+
import org.springframework.web.filter.DelegatingFilterProxy;
1214
import org.springframework.web.servlet.DispatcherServlet;
1315

1416
public class MainWebAppInitializer implements WebApplicationInitializer {
@@ -43,6 +45,11 @@ public void onStartup(final ServletContext sc) throws ServletException {
4345
if (!mappingConflicts.isEmpty()) {
4446
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");
4547
}
48+
49+
// spring security filter
50+
final DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy("springSecurityFilterChain");
51+
final Dynamic addedFilter = sc.addFilter("springSecurityFilterChain", springSecurityFilterChain);
52+
addedFilter.addMappingForUrlPatterns(null, false, "/*");
4653
}
4754

4855
}

spring-security-rest-custom/src/main/java/org/baeldung/config/child/WebConfig.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import java.util.List;
44

5+
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.ComponentScan;
67
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
79
import org.springframework.http.converter.HttpMessageConverter;
810
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
911
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@@ -12,6 +14,8 @@
1214
@Configuration
1315
@EnableWebMvc
1416
@ComponentScan("org.baeldung.web")
17+
// @ImportResource({ "classpath:prop.xml" })
18+
// @PropertySource("classpath:foo.properties")
1519
public class WebConfig extends WebMvcConfigurerAdapter {
1620

1721
public WebConfig() {
@@ -26,6 +30,13 @@ public void configureMessageConverters(final List<HttpMessageConverter<?>> conve
2630
converters.add(new MappingJackson2HttpMessageConverter());
2731
}
2832

29-
//
33+
// beans
34+
35+
@Bean
36+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
37+
final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
38+
ppc.setIgnoreUnresolvablePlaceholders(true);
39+
return ppc;
40+
}
3041

3142
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
package org.baeldung.config.parent;
22

3+
import org.springframework.context.annotation.Bean;
34
import org.springframework.context.annotation.ComponentScan;
45
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.PropertySource;
7+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
58

69
@Configuration
710
@ComponentScan("org.baeldung.service")
11+
// @ImportResource({ "classpath:prop.xml" })
12+
@PropertySource("classpath:foo.properties")
813
public class ServiceConfig {
914

1015
public ServiceConfig() {
1116
super();
1217
}
1318

19+
// beans
20+
21+
@Bean
22+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
23+
final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
24+
ppc.setIgnoreUnresolvablePlaceholders(true);
25+
return ppc;
26+
}
27+
1428
}

spring-security-rest-custom/src/main/java/org/baeldung/service/FooService.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package org.baeldung.service;
22

33
import org.baeldung.web.dto.Foo;
4+
import org.springframework.beans.factory.InitializingBean;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.core.env.Environment;
48
import org.springframework.stereotype.Service;
59

610
@Service
7-
public class FooService implements IFooService {
11+
public class FooService implements IFooService, InitializingBean {
12+
13+
@Value("${foo1}")
14+
private String foo1;
15+
16+
@Autowired
17+
private Environment env;
818

919
public FooService() {
1020
super();
@@ -17,4 +27,10 @@ public Foo findOne(final Long id) {
1727
return new Foo();
1828
}
1929

30+
@Override
31+
public final void afterPropertiesSet() {
32+
System.out.println("In Parent Context, property via @Value = " + foo1);
33+
System.out.println("In Parent Context, property via env = " + env.getProperty("foo2"));
34+
}
35+
2036
}

spring-security-rest-custom/src/main/java/org/baeldung/web/controller/FooController.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22

33
import org.baeldung.service.IFooService;
44
import org.baeldung.web.dto.Foo;
5+
import org.springframework.beans.factory.InitializingBean;
56
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.core.env.Environment;
69
import org.springframework.stereotype.Controller;
710
import org.springframework.web.bind.annotation.PathVariable;
811
import org.springframework.web.bind.annotation.RequestMapping;
912
import org.springframework.web.bind.annotation.RequestMethod;
1013
import org.springframework.web.bind.annotation.ResponseBody;
1114

1215
@Controller
13-
@RequestMapping(value = "/foo")
14-
public class FooController {
16+
@RequestMapping(value = "/foos")
17+
public class FooController implements InitializingBean {
18+
19+
@Value("${foo1}")
20+
private String foo1;
21+
22+
@Autowired
23+
private Environment env;
1524

1625
@Autowired
1726
private IFooService service;
@@ -28,4 +37,10 @@ public Foo findOne(@PathVariable("id") final Long id) {
2837
return service.findOne(id);
2938
}
3039

40+
@Override
41+
public final void afterPropertiesSet() {
42+
System.out.println("In Child Context, property via @Value = " + foo1);
43+
System.out.println("In Child Context, property via env = " + env.getProperty("foo2"));
44+
}
45+
3146
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo1=bar1
2+
foo2=bar2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
10+
11+
<context:property-placeholder location="classpath:foo.properties" />
12+
13+
</beans>

spring-security-rest-custom/src/main/webapp/WEB-INF/api-servlet.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

spring-security-rest-custom/src/main/webapp/WEB-INF/web_old.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<url-pattern>/api/*</url-pattern>
3838
</servlet-mapping>
3939

40-
4140
<!-- Spring Security -->
4241
<filter>
4342
<filter-name>springSecurityFilterChain</filter-name>

0 commit comments

Comments
 (0)