|
| 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 | +} |
0 commit comments