Skip to content

Commit 5b91988

Browse files
author
eugenp
committed
introducing parent-child context to experiment with properties and httpclient cleanup
1 parent 4c5bb1b commit 5b91988

File tree

11 files changed

+107
-18
lines changed

11 files changed

+107
-18
lines changed

spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java renamed to httpclient/src/test/java/org/baeldung/httpclient/HttpClientUnshortenLiveTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.live;
1+
package org.baeldung.httpclient;
22

33
import static org.hamcrest.Matchers.equalTo;
44
import static org.junit.Assert.assertThat;
@@ -25,7 +25,7 @@
2525
import com.google.common.base.Preconditions;
2626
import com.google.common.collect.Lists;
2727

28-
public class HttpLiveServiceTemp {
28+
public class HttpClientUnshortenLiveTest {
2929

3030
private CloseableHttpClient client;
3131

spring-security-rest-custom/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55

66
### Relevant Articles:
77
- [Spring Security Authentication Provider](http://www.baeldung.com/spring-security-authentication-provider)
8-
- [Unshorten URLs with HttpClient](http://www.baeldung.com/unshorten-url-httpclient)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.config;
2+
3+
import java.util.Set;
4+
5+
import javax.servlet.ServletContext;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.ServletRegistration;
8+
9+
import org.springframework.web.WebApplicationInitializer;
10+
import org.springframework.web.context.ContextLoaderListener;
11+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
12+
import org.springframework.web.servlet.DispatcherServlet;
13+
14+
public class MainWebAppInitializer implements WebApplicationInitializer {
15+
16+
public MainWebAppInitializer() {
17+
super();
18+
}
19+
20+
//
21+
22+
/**
23+
* Register and configure all Servlet container components necessary to power the web application.
24+
*/
25+
@Override
26+
public void onStartup(final ServletContext sc) throws ServletException {
27+
System.out.println("MyWebAppInitializer.onStartup()");
28+
29+
// Create the 'root' Spring application context
30+
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
31+
root.scan("org.baeldung.config.parent");
32+
// root.getEnvironment().setDefaultProfiles("embedded");
33+
34+
// Manages the lifecycle of the root application context
35+
sc.addListener(new ContextLoaderListener(root));
36+
37+
// Handles requests into the application
38+
final AnnotationConfigWebApplicationContext childWebApplicationContext = new AnnotationConfigWebApplicationContext();
39+
childWebApplicationContext.scan("org.baeldung.config.child");
40+
final ServletRegistration.Dynamic appServlet = sc.addServlet("api", new DispatcherServlet(childWebApplicationContext));
41+
appServlet.setLoadOnStartup(1);
42+
final Set<String> mappingConflicts = appServlet.addMapping("/");
43+
if (!mappingConflicts.isEmpty()) {
44+
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");
45+
}
46+
}
47+
48+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.spring;
1+
package org.baeldung.config.child;
22

33
import java.util.List;
44

spring-security-rest-custom/src/main/java/org/baeldung/spring/SecSecurityConfig.java renamed to spring-security-rest-custom/src/main/java/org/baeldung/config/parent/SecurityConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.spring;
1+
package org.baeldung.config.parent;
22

33
import org.springframework.context.annotation.ComponentScan;
44
import org.springframework.context.annotation.Configuration;
@@ -7,9 +7,9 @@
77
@Configuration
88
@ImportResource({ "classpath:webSecurityConfig.xml" })
99
@ComponentScan("org.baeldung.security")
10-
public class SecSecurityConfig {
10+
public class SecurityConfig {
1111

12-
public SecSecurityConfig() {
12+
public SecurityConfig() {
1313
super();
1414
}
1515

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.config.parent;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("org.baeldung.service")
8+
public class ServiceConfig {
9+
10+
public ServiceConfig() {
11+
super();
12+
}
13+
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.service;
2+
3+
import org.baeldung.web.dto.Foo;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class FooService implements IFooService {
8+
9+
public FooService() {
10+
super();
11+
}
12+
13+
// API
14+
15+
@Override
16+
public Foo findOne(final Long id) {
17+
return new Foo();
18+
}
19+
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.service;
2+
3+
import org.baeldung.web.dto.Foo;
4+
5+
public interface IFooService {
6+
7+
Foo findOne(final Long id);
8+
9+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.baeldung.web.controller;
22

3+
import org.baeldung.service.IFooService;
34
import org.baeldung.web.dto.Foo;
45
import org.springframework.beans.factory.annotation.Autowired;
5-
import org.springframework.context.ApplicationEventPublisher;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.web.bind.annotation.PathVariable;
88
import org.springframework.web.bind.annotation.RequestMapping;
@@ -14,7 +14,7 @@
1414
public class FooController {
1515

1616
@Autowired
17-
private ApplicationEventPublisher eventPublisher;
17+
private IFooService service;
1818

1919
public FooController() {
2020
super();
@@ -25,7 +25,7 @@ public FooController() {
2525
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
2626
@ResponseBody
2727
public Foo findOne(@PathVariable("id") final Long id) {
28-
return new Foo();
28+
return service.findOne(id);
2929
}
3030

3131
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?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-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" >
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
54

65
</beans>

0 commit comments

Comments
 (0)