Skip to content

Commit d288004

Browse files
Anand kumarpivovarit
authored andcommitted
Spring Web Contexts (BAEL-82) (eugenp#2113)
* spring web contexts example * applicationContext util added * java based configuration added * bean name fixed
1 parent 806c17b commit d288004

17 files changed

Lines changed: 381 additions & 1 deletion
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.contexts;
2+
3+
public class Greeting {
4+
5+
private String message;
6+
7+
public String getMessage() {
8+
return message;
9+
}
10+
11+
public void setMessage(String message) {
12+
this.message = message;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return "Greeting [message=" + message + "]";
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.contexts.config;
2+
3+
import javax.servlet.ServletContext;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.ServletRegistration;
6+
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.WebApplicationInitializer;
9+
import org.springframework.web.context.ContextLoaderListener;
10+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
11+
import org.springframework.web.servlet.DispatcherServlet;
12+
13+
@Configuration
14+
public class ApplicationInitializer implements WebApplicationInitializer {
15+
16+
@Override
17+
public void onStartup(ServletContext servletContext) throws ServletException {
18+
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
19+
rootContext.register(RootApplicationConfig.class);
20+
servletContext.addListener(new ContextLoaderListener(rootContext));
21+
22+
AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext();
23+
normalWebAppContext.register(NormalWebAppConfig.class);
24+
ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
25+
normal.setLoadOnStartup(1);
26+
normal.addMapping("/api/*");
27+
28+
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
29+
secureWebAppContext.register(SecureWebAppConfig.class);
30+
ServletRegistration.Dynamic secure = servletContext.addServlet("secure-webapp", new DispatcherServlet(secureWebAppContext));
31+
secure.setLoadOnStartup(1);
32+
secure.addMapping("/s/api/*");
33+
}
34+
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.contexts.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
10+
import org.springframework.web.servlet.view.JstlView;
11+
12+
@Configuration
13+
@EnableWebMvc
14+
@ComponentScan(basePackages = { "com.baeldung.contexts.normal" })
15+
public class NormalWebAppConfig extends WebMvcConfigurerAdapter {
16+
17+
@Bean
18+
public ViewResolver viewResolver() {
19+
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
20+
resolver.setPrefix("/WEB-INF/view/");
21+
resolver.setSuffix(".jsp");
22+
resolver.setViewClass(JstlView.class);
23+
return resolver;
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.contexts.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
import com.baeldung.contexts.Greeting;
8+
9+
@Configuration
10+
@ComponentScan(basePackages = { "com.baeldung.contexts.services" })
11+
public class RootApplicationConfig {
12+
13+
@Bean
14+
public Greeting greeting() {
15+
Greeting greeting = new Greeting();
16+
greeting.setMessage("Hello World !!");
17+
return greeting;
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.contexts.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
10+
import org.springframework.web.servlet.view.JstlView;
11+
12+
@Configuration
13+
@EnableWebMvc
14+
@ComponentScan(basePackages = { "com.baeldung.contexts.secure" })
15+
public class SecureWebAppConfig extends WebMvcConfigurerAdapter {
16+
17+
@Bean
18+
public ViewResolver viewResolver() {
19+
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
20+
resolver.setPrefix("/WEB-INF/secure/view/");
21+
resolver.setSuffix(".jsp");
22+
resolver.setViewClass(JstlView.class);
23+
return resolver;
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.contexts.normal;
2+
3+
import java.util.Arrays;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.context.ContextLoader;
10+
import org.springframework.web.context.WebApplicationContext;
11+
import org.springframework.web.servlet.ModelAndView;
12+
13+
import com.baeldung.contexts.services.ApplicationContextUtilService;
14+
import com.baeldung.contexts.services.GreeterService;
15+
16+
@Controller
17+
public class HelloWorldController {
18+
19+
@Autowired
20+
WebApplicationContext webApplicationContext;
21+
22+
@Autowired
23+
private GreeterService greeterService;
24+
25+
private void processContext() {
26+
WebApplicationContext rootContext = ContextLoader.getCurrentWebApplicationContext();
27+
28+
System.out.println("root context : " + rootContext);
29+
System.out.println("root context Beans: " + Arrays.asList(rootContext.getBeanDefinitionNames()));
30+
31+
System.out.println("context : " + webApplicationContext);
32+
System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames()));
33+
}
34+
35+
@RequestMapping(path = "/welcome")
36+
public ModelAndView helloWorld() {
37+
processContext();
38+
String message = "<br><div style='text-align:center;'>" + "<h3> " + greeterService.greet() + "</h3></div>";
39+
return new ModelAndView("welcome", "message", message);
40+
}
41+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.contexts.secure;
2+
3+
import java.util.Arrays;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.context.ContextLoader;
11+
import org.springframework.web.context.WebApplicationContext;
12+
import org.springframework.web.servlet.ModelAndView;
13+
14+
import com.baeldung.contexts.services.ApplicationContextUtilService;
15+
import com.baeldung.contexts.services.GreeterService;
16+
17+
@Controller
18+
public class HelloWorldSecureController {
19+
20+
@Autowired
21+
WebApplicationContext webApplicationContext;
22+
23+
@Autowired
24+
private GreeterService greeterService;
25+
26+
@Autowired
27+
@Qualifier("contextAware")
28+
private ApplicationContextUtilService contextUtilService;
29+
30+
private void processContext() {
31+
ApplicationContext context = contextUtilService.getApplicationContext();
32+
System.out.println("application context : " + context);
33+
System.out.println("application context Beans: " + Arrays.asList(context.getBeanDefinitionNames()));
34+
35+
WebApplicationContext rootContext = ContextLoader.getCurrentWebApplicationContext();
36+
System.out.println("context : " + rootContext);
37+
System.out.println("context Beans: " + Arrays.asList(rootContext.getBeanDefinitionNames()));
38+
39+
System.out.println("context : " + webApplicationContext);
40+
System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames()));
41+
}
42+
43+
@RequestMapping(path = "/welcome")
44+
public ModelAndView helloWorld() {
45+
processContext();
46+
String message = "<br><div style='text-align:center;'>" + "<h3> " + greeterService.greet() + "</h3></div>";
47+
return new ModelAndView("welcome", "message", message);
48+
}
49+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.contexts.services;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.ApplicationContextAware;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service(value="contextAware")
9+
public class ApplicationContextUtilService implements ApplicationContextAware {
10+
11+
private ApplicationContext applicationContext;
12+
13+
@Override
14+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
15+
this.applicationContext = applicationContext;
16+
}
17+
18+
public ApplicationContext getApplicationContext() {
19+
return applicationContext;
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.contexts.services;
2+
3+
import javax.annotation.Resource;
4+
5+
import org.springframework.stereotype.Service;
6+
7+
import com.baeldung.contexts.Greeting;
8+
9+
@Service
10+
public class GreeterService {
11+
12+
@Resource
13+
private Greeting greeting;
14+
15+
public String greet(){
16+
return greeting.getMessage();
17+
}
18+
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h2>Hello World!</h2>
4+
</body>
5+
</html>

0 commit comments

Comments
 (0)