introducing parent-child context to experiment with properties and httpclient cleanup

This commit is contained in:
eugenp 2014-01-26 12:30:28 +02:00
parent 4c5bb1b133
commit 5b91988d0e
11 changed files with 107 additions and 18 deletions

View File

@ -1,4 +1,4 @@
package org.baeldung.live;
package org.baeldung.httpclient;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@ -25,7 +25,7 @@ import org.junit.Test;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
public class HttpLiveServiceTemp {
public class HttpClientUnshortenLiveTest {
private CloseableHttpClient client;

View File

@ -5,4 +5,3 @@
### Relevant Articles:
- [Spring Security Authentication Provider](http://www.baeldung.com/spring-security-authentication-provider)
- [Unshorten URLs with HttpClient](http://www.baeldung.com/unshorten-url-httpclient)

View File

@ -0,0 +1,48 @@
package org.baeldung.config;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MainWebAppInitializer implements WebApplicationInitializer {
public MainWebAppInitializer() {
super();
}
//
/**
* Register and configure all Servlet container components necessary to power the web application.
*/
@Override
public void onStartup(final ServletContext sc) throws ServletException {
System.out.println("MyWebAppInitializer.onStartup()");
// Create the 'root' Spring application context
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan("org.baeldung.config.parent");
// root.getEnvironment().setDefaultProfiles("embedded");
// Manages the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(root));
// Handles requests into the application
final AnnotationConfigWebApplicationContext childWebApplicationContext = new AnnotationConfigWebApplicationContext();
childWebApplicationContext.scan("org.baeldung.config.child");
final ServletRegistration.Dynamic appServlet = sc.addServlet("api", new DispatcherServlet(childWebApplicationContext));
appServlet.setLoadOnStartup(1);
final Set<String> mappingConflicts = appServlet.addMapping("/");
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");
}
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.spring;
package org.baeldung.config.child;
import java.util.List;

View File

@ -1,4 +1,4 @@
package org.baeldung.spring;
package org.baeldung.config.parent;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ -7,9 +7,9 @@ import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource({ "classpath:webSecurityConfig.xml" })
@ComponentScan("org.baeldung.security")
public class SecSecurityConfig {
public class SecurityConfig {
public SecSecurityConfig() {
public SecurityConfig() {
super();
}

View File

@ -0,0 +1,14 @@
package org.baeldung.config.parent;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.service")
public class ServiceConfig {
public ServiceConfig() {
super();
}
}

View File

@ -0,0 +1,20 @@
package org.baeldung.service;
import org.baeldung.web.dto.Foo;
import org.springframework.stereotype.Service;
@Service
public class FooService implements IFooService {
public FooService() {
super();
}
// API
@Override
public Foo findOne(final Long id) {
return new Foo();
}
}

View File

@ -0,0 +1,9 @@
package org.baeldung.service;
import org.baeldung.web.dto.Foo;
public interface IFooService {
Foo findOne(final Long id);
}

View File

@ -1,8 +1,8 @@
package org.baeldung.web.controller;
import org.baeldung.service.IFooService;
import org.baeldung.web.dto.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class FooController {
@Autowired
private ApplicationEventPublisher eventPublisher;
private IFooService service;
public FooController() {
super();
@ -25,7 +25,7 @@ public class FooController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public Foo findOne(@PathVariable("id") final Long id) {
return new Foo();
return service.findOne(id);
}
}

View File

@ -1,6 +1,5 @@
<?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 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

@ -15,7 +15,7 @@
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.baeldung.spring</param-value>
<param-value>org.baeldung.config.parent</param-value>
</context-param>
<listener>
@ -26,6 +26,10 @@
<servlet>
<servlet-name>api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- <init-param> -->
<!-- <param-name>contextConfigLocation</param-name> -->
<!-- <param-value>org.baeldung.config.child</param-value> -->
<!-- </init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
@ -44,8 +48,4 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- <welcome-file-list> -->
<!-- <welcome-file>index.html</welcome-file> -->
<!-- </welcome-file-list> -->
</web-app>