I saw a lot people has similar problem but no answer works for me. What I'm doing is trying to embed Jetty with Spring 4 with zero configuration. I put some of my code below for better describe my question:
JettyStarter.class
public class JettyStarter {
...
private static final String CONFIG_LOCATION = "com....config";
private static final String DEFAULT_MAPPING_URL = "/*";
private static final String DEFAULT_CONTEXT_PATH = "/";
...
private ServletContextHandler getServletContextHandler() throws IOException {
WebApplicationContext context = getContext();
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setErrorHandler(null);
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
ServletHolder servletHolder = new ServletHolder("Servlet Dispatcher", dispatcherServlet);
servletHolder.setInitOrder(1);
contextHandler.addServlet(servletHolder, DEFAULT_MAPPING_URL);
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
contextHandler.setContextPath(DEFAULT_CONTEXT_PATH);
return contextHandler;
}
private void startJetty() throws Exception
{
...
HandlerCollection handlers = new HandlerCollection();
handlers.addHandler(getServletContextHandler());
...
server.setHandler(handlers);
server.start();
server.join();
}
private WebApplicationContext getContext() throws IOException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
...
return context;
}
...
}
WebMvcConfig.class
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com...controllers")
public class WebMvcConfig extends WebMvcConfigurerAdapter{
...
#Bean
public InternalResourceViewResolver getInternalResourceViewResolver()
{
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/jsp/view");
internalResourceViewResolver.setSuffix(".jsp");
internalResourceViewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
return internalResourceViewResolver;
}
}
HomeController.class
#Controller
public class HomeController {
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(#RequestParam(value="error", required=false) boolean error, ModelMap model)
{
...
return "/pub/login";
}
}
Finally, after the server started, I got 404 error with the following message while trying to access localhost:8080/login
WARNING: No mapping found for HTTP request with URI [/WEB-INF/jsp/view/pub/login.jsp] in DispatcherServlet with name 'Servlet Dispatcher'
I'm pretty sure resource "login.jsp" is under the folder "/webapp/WEB-INF/jsp/view/pub" within the package. But why it keeping saying it can not be found?!
Solution:
Weird restriction that I cannot answer myself question within 8 hours.
By traced in Spring's source code, I eventually got my page display. The reason is I set Jetty ServletContextHandler's ResourceBase as "webapp", and created "WEB-INF" sub folder under it, all resources under "WEB-INF/jsp/view/..." as well. But the folder WEB-INF is unseeable by ResourceHttpRequestHandler as we can see the code there:
protected boolean isInvalidPath(String path) {
return (path.contains("WEB-INF") || path.contains("META-INF") || StringUtils.cleanPath(path).startsWith(".."));
}
So, the solution is change the resource base to "webapp/WEB-INF", and change InternalResourceViewResolver prefix to "/jsp/view" as well. It does work though!
Now my question is, ResourceHttpRequestHandler is known by to be used to restrict directly resource access, a servlet should not use it, why my none-config version load it in? but not for XML-config version? is XML-config using any other handler by pass this restriction or something I'm missing? Appreciate for anybody to forward.
You need to tell Spring that you have annotated your beans. This is usually done in an XML saying annotation-driven. But I believe you'll have to use #AnnotationDrivenConfig alon with your #Configuration element so that your beans get autowired.
EDIT: As OP mentioned #AnnotationDrivenConfig is deprecated.
Can you try https://stackoverflow.com/a/8076045/2231632 ?
Related
I have Spring Boot application with REST API mapped on /api. I need to define additional servlet on /. I want all request that match /api was handled by REST API and all others requests by the servlet. How to do this?
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
#RestController
#RequestMapping("/api")
public class ApiController {
#GetMapping
public String get() {
return "api";
}
}
#Bean
public ServletRegistrationBean customServletBean() {
return new ServletRegistrationBean<>(new HttpServlet() {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().println("custom");
}
}, "/*");
}
}
In code above I want something like this:
curl http://localhost:8080/api/
> api⏎
curl http://localhost:8080/custom/
> custom
I have tried with filter to redirect requests, but all requests go to custom servlet:
#Bean
public FilterRegistrationBean apiResolverFilter() {
final FilterRegistrationBean registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter((req, response, chain) -> {
HttpServletRequest request = (HttpServletRequest) req;
String path = request.getRequestURI().substring(request.getContextPath().length());
if (path.startsWith("/api/")) {
request.getRequestDispatcher(path).forward(request, response);
} else {
chain.doFilter(request, response);
}
});
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
This project is available on github: https://github.com/mariuszs/nestedweb
When mapping a servlet to the root path you will override the mapping for the DispatcherServlet which, by default, is mapped to /.
There are basically 3 solutions you could try
Map the DispatcherServlet to /api and modify the mappings in your controllers
Use a ServletForwardingController to forward the request to the configured but unmapped Servlet
Use a ServletWrappingController to wrap a Servlet instance
Number 2 and 3 are almost the same, with this difference that with option 3 Spring also manages the Servlet instance whereas with option 2, the Servlet container manages the Servlet.
Mapping DispatcherServlet to /api
Option 1 can be an option if all of your controllers are mapped under /api, if they aren't this isn't an option. In your application.properties you would set the spring.mvc.servlet.path to /api. Then you would configure your other Servlet like you did in your question.
Use a ServletForwardingController
Spring provides a ServletForwardingController which will lookup a Servlet in the ServletContext given the name of the servlet and forward the request to it. You will still have to register the Servlet but prevent it from being mapped.
Next you would need a SimpleUrlHandlerMapping to map the URLs to this controller or set it as the default handler (basically a catch all).
#Bean
public ServletForwardingController forwarder() {
ServletForwardingController controller = new ServletForwardingController();
controller.setServletName("my-servlet");
return controller;
}
#Bean
public CustomServlet customServlet() {
return new CustomServlet();
}
#Bean
public ServletRegistrationBean customServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(customServlet(), false);
registration.setServletName("customServlet");
return registration;
}
#Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setDefaultHandler(forwarder());
mapping.setOrder(LOWEST_PRECEDENCE - 2);
return mapping;
}
Use a ServletWrappingController
Spring provides a ServletWrappingController which will internally create and configure a Servlet instance. It acts as an adapter from/to the Servlet to a Spring Controller. You don't have to register the CustomServlet in this case and is thus slightly easier to configure the then ServletForwardingController.
Next you would need a SimpleUrlHandlerMapping to map the URLs to this controller or set it as the default handler (basically a catch all).
#Bean
public ServletWrappingController wrapper() {
ServletWrappingController controller = new ServletWrappingController ();
controller.setServletName("my-servlet");
controller.setServletClass(CustomerServlet.class);
return controller;
}
#Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setDefaultHandler(wrapper());
mapping.setOrder(LOWEST_PRECEDENCE - 2);
return mapping;
}
Depending on your architecture and url structure you might want to go for option 1 or option 3.
I have an application that I would like to automatically validate messages that are received and sent. I've attached the PayloadValidatingInterceptor and set the schema I would like it to use:
#EnableWs
#Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
#Autowired
private ApplicationContext ctx;
#Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
// modifies the wsdl to serve the correct locations
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/*");
}
#Bean
protected PayloadValidatingInterceptor getValidatingInterceptor() {
PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingInterceptor();
validatingInterceptor.setSchema(getResource("classpath:CARetriever.xsd"));
validatingInterceptor.setValidateRequest(true);
validatingInterceptor.setValidateResponse(true);
return validatingInterceptor;
}
private Resource getResource(String resource) {
return ctx.getResource(resource);
}
}
I can see that the interceptor is getting loaded
2016-01-21 14:22:08 INFO org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor,164 - Validating using class path resource [CARetriever.xsd]
However, when I throw an invalid SOAP message against it, I get a NullPointerException rather than a validation message. So, either my configuration or expectations are wrong. Can someone point to which?
I got the same error: a SOAPFault with a NPE inside with no stack trace. While debugging I noticed that the validator instance used in PayloadValidatingInterceptor is null, because setSchema does not initialize it. But there is the method setXsdSchema, which does initialize it. So, I could fix the NPE using that method instead of setSchema as follows:
payloadValidatingInterceptor.setXsdSchema(new SimpleXsdSchema(new ClassPathResource("test.xsd")));
For a complete example one may check this example: http://memorynotfound.com/spring-ws-validate-soap-request-response-xsd-schema/
Hope that helps.
I am creating a REST-based web application with AngularJS as front end and REST-based backend (with Spring 4). I am following the code-based configuration approach found here: WebApplicationInitializer
When I run the project on the server, I get a null value in the line:
FilterRegistration.Dynamic filter = container.addFilter("prerender", seoFilter);
What am I missing? I am a bit new to creating web-apps from scratch using annotations.
Here is the class in question:
public class MyWebAppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container) throws ServletException {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("classpath:MyContext.xml");
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/api/*");
com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();
FilterRegistration.Dynamic filter = container.addFilter("prerender", seoFilter);
filter.setInitParameter("prerenderToken", "123456789123456789");
filter.addMappingForUrlPatterns(null , true, "/*");
ServletRegistration.Dynamic initSysConfiguration
= container.addServlet("initSysConfiguration", new InitSystemConfigurations());
initSysConfiguration.setLoadOnStartup(1);
initSysConfiguration.addMapping("/InitSystemConfigurations");
}
This line is give me null
com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();
I tried this,but same result
FilterRegistration.Dynamic filter1 = container.addFilter("prerender", com.github.greengerong.PreRenderSEOFilter.class);
When the method addFilter returns null it means there is already a filter registered for that name.
Returns:
a FilterRegistration object that may be used to further configure the given filter, or null if this ServletContext already contains a complete FilterRegistration for a filter with the given filterName or if the same filter instance has already been registered with this or another ServletContext in the same container
Make sure that you don't have a web.ml which already registers this filter.
Another tip instead of implementing WebApplicationInitializer yourself extend the AbstractDispatcherServletInitializer and implement the needed methods.
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
#Override
public void onStartup(ServletContext container) throws ServletException {
super.onStartup(container);
com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();
FilterRegistration.Dynamic filter = container.addFilter("prerender", seoFilter);
filter.setInitParameter("prerenderToken", "123456789123456789");
filter.addMappingForUrlPatterns(null , true, "/*");
ServletRegistration.Dynamic initSysConfiguration
= container.addServlet("initSysConfiguration", new InitSystemConfigurations());
initSysConfiguration.setLoadOnStartup(1);
initSysConfiguration.addMapping("/InitSystemConfigurations");
}
protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("classpath:MyContext.xml");
return appContext;
}
protected String[] getServletMappings() {
return new String[] {"/api/*"};
}
}
I solved it by running maven command mvn clean, I dont know some thing was wrong somewhere and giving me strange behaviour. I m not sure what i learn from this problem :(
Thanks for your help.
I am currently using Spring MVC 4.0.5 and would like to use Spring Web Flow for some process oriented pages. However, I think there is still some problem with my configuration.
In the server logs:
2014-09-15 20:54:49,280 [localhost-startStop-1] DEBUG org.springframework.webflow.definition.registry.FlowDefinitionRegistryImpl - Registering flow definition 'ServletContext resource [/WEB-INF/flows/registration/registration-flow.xml]' under id 'registration'
However, when accessing it, the log says
2014-09-15 20:54:49,820 [http-bio-8080-exec-2] DEBUG org.springframework.webflow.mvc.servlet.FlowHandlerMapping - No flow mapping found for request with URI '/appContext/registration/'
Here is my configuration for the web flow
#Configuration
public class WebFlowConfig extends AbstractFlowConfiguration {
private Logger logger = Logger.getLogger(WebFlowConfig.class);
#Bean
#Autowired
public FlowExecutor flowExecutor(FlowDefinitionRegistry flowRegistry,
PlatformTransactionManager txManager, SessionFactory sessionFactory) {
return getFlowExecutorBuilder(flowRegistry)
.addFlowExecutionListener(new SecurityFlowExecutionListener(),
"*")
.addFlowExecutionListener(
new HibernateFlowExecutionListener(sessionFactory,
txManager), "*").build();
}
#Bean
#Autowired
public FlowDefinitionRegistry flowRegistry(
FlowBuilderServices flowBuilderServices) {
return getFlowDefinitionRegistryBuilder(flowBuilderServices)
.setBasePath("/WEB-INF/flows")
.addFlowLocationPattern("/**/*-flow.xml").build();
}
#Bean
#Autowired
public FlowBuilderServices flowBuilderServices(
MvcViewFactoryCreator mvcViewFactoryCreator, Validator validator) {
return getFlowBuilderServicesBuilder()
.setViewFactoryCreator(mvcViewFactoryCreator)
.setValidator(validator).setDevelopmentMode(true).build();
}
#Bean
#Autowired
public MvcViewFactoryCreator mvcViewFactoryCreator(
InternalResourceViewResolver viewResolver) {
MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
factoryCreator.setViewResolvers(Arrays.asList(viewResolver));
return factoryCreator;
}
#Bean
#Autowired
public FlowHandlerMapping flowHandlerMapping(FlowDefinitionRegistry registry) {
FlowHandlerMapping handlerMapping = new FlowHandlerMapping();
handlerMapping.setOrder(-1);
handlerMapping.setFlowRegistry(registry);
return handlerMapping;
}
#Bean
#Autowired
public FlowHandlerAdapter flowHandlerAdapter(FlowExecutor executor) {
FlowHandlerAdapter handlerAdapter = new FlowHandlerAdapter();
handlerAdapter.setFlowExecutor(executor);
handlerAdapter.setSaveOutputToFlashScopeOnRedirect(true);
return handlerAdapter;
}
}
Hope that someone can help. Thanks.
You are specifying FlowHandlerMapping in webflow config:
Implementation of HandlerMapping that follows a simple convention
for creating URL path mappings from the ids of registered flow definitions. This
implementation returns a FlowHandler that invokes a flow if the current request
path matches the id of a flow in the configured FlowDefinitionRegistry.
The default FlowUrlHandler implementation for Spring Web Flow is DefaultFlowUrlHandler.
Expects URLs to launch flow to be of this pattern:
http://<host>/[app context path]/[app servlet path]/<flow path>
The flow id is treated as the path info component of the request URL string.
If the path info is null, the servletPath will be used as the flow id. Also, if
the servlet path ends in an extension it will be stripped when calculating the flow id.
The flow definition URL for the given flow id will be built by appending the
flow id to the base app context and servlet paths.
No flow mapping found for request with URI '/appContext/registration/'
Your path info must have been null and servlet mapping is something like: /appContext/registration/* resulting in flow id as /appContext/registration/ which is not registered.
So check your servlet mapping.
Remove MvcViewFactoryCreator Definition And .setViewFactoryCreator(mvcViewFactoryCreator)
I have a web application on Tomcat 7.0.34, Spring 3.2.3, Spring Security 3.2.0.RC1 and Spring Social 1.1.
For some reason the Spring context is being loaded twice. The second load is happening immediately after the first load has finished. The log below shows the Context Loader loading the Root WebApplicationContext. Everything progresses normally and all the RequstMappingHandlers are registering correctly. Then immediately the context is refreshed again.
I've read several solutions on SO about ensuring you don't load the configuration as part of the Context Loader and the DispatcherServlet at the same time and have tested various combinations of this but that doesn't seem to have fixed it and I'm becoming code blind as well.
All my testing on this has pushed me to an annotation only configuration both for the container and Spring components but the config classes are pretty much copy and paste from the Spring Social examples on github. Although I've included the SocialConfig.java details below, this problem has been happening before I implemented Spring Social but I can't move on without fixing it.
Also, the problem was present with a hybrid xml (web.xml, security-app-context.xml) and annotation configuration.
I'm implementing WebApplicationInitializer rather than having a web.xml
public class WebClientInitialiser implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
// Manage the lifecycle of the root application context
appContext.setConfigLocation("com.mycompany.webclient.config");
appContext.setServletContext(container);
container.addListener(new ContextLoaderListener(appContext));
container.addListener(new MyCompanyContextListener());
container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain"))
.addMappingForUrlPatterns(null, false, "/*");
// Register and map the dispatcher servlet
Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
}
}
My MainConfig.java
/**
* Main configuration class for the application.
* Turns on #Component scanning, loads externalized application properties
* and imports legacy security configuration
*/
#Configuration
#ComponentScan(basePackages = "com.mycompany.webclient", excludeFilters = { #Filter(Configuration.class) })
#PropertySource("classpath:wc.properties")
#ImportResource("/WEB-INF/spring/appServlet/security-app-context.xml")
public class MainConfig {
#Bean
public PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
My WebMvcConfig.java
#Configuration
#EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
#Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/messages/messages");
return messageSource;
}
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
My SocialConfig.java
#Configuration
#EnableSocial
public class SocialConfig implements SocialConfigurer {
private SocialUserDAO socialUserDao;
//
// SocialConfigurer implementation methods
//
#Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
String clientId="XXXXXX";
String clientSecret="XXXXX";
cfConfig.addConnectionFactory(new FacebookConnectionFactory(clientId, clientSecret));
}
#Override
public UserIdSource getUserIdSource() {
return new UserIdSource() {
#Override
public String getUserId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return authentication.getName();
}
};
}
#Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
return new HibernateUsersConnectionRepository(socialUserDao, connectionFactoryLocator, Encryptors.noOpText());
}
//
// API Binding Beans
//
#Bean
#Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public Facebook facebook(ConnectionRepository repository) {
Connection<Facebook> connection = repository.findPrimaryConnection(Facebook.class);
return connection != null ? connection.getApi() : null;
}
#Bean
#Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public Twitter twitter(ConnectionRepository repository) {
Connection<Twitter> connection = repository.findPrimaryConnection(Twitter.class);
return connection != null ? connection.getApi() : null;
}
#Bean
#Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public LinkedIn linkedin(ConnectionRepository repository) {
Connection<LinkedIn> connection = repository.findPrimaryConnection(LinkedIn.class);
return connection != null ? connection.getApi() : null;
}
//
// Web Controller and Filter Beans
//
#Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
ConnectController connectController = new ConnectController(connectionFactoryLocator, connectionRepository);
connectController.addInterceptor(new PostToWallAfterConnectInterceptor());
connectController.addInterceptor(new TweetAfterConnectInterceptor());
return connectController;
}
#Bean
public ProviderSignInController providerSignInController(ConnectionFactoryLocator connectionFactoryLocator, UsersConnectionRepository usersConnectionRepository) {
return new ProviderSignInController(connectionFactoryLocator, usersConnectionRepository, new SimpleSignInAdapter(new HttpSessionRequestCache()));
}
#Bean
public DisconnectController disconnectController(UsersConnectionRepository usersConnectionRepository, Environment env) {
return new DisconnectController(usersConnectionRepository, env.getProperty("facebook.clientSecret"));
}
#Bean
public ReconnectFilter apiExceptionHandler(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) {
return new ReconnectFilter(usersConnectionRepository, userIdSource);
}
}
Any help, comments, pointers is greatly appreciated.
This log output is repeated twice at the start of each context refresh:
org.springframework.web.context.ContextLoader- Root WebApplicationContext: initialization started
org.springframework.web.context.support.AnnotationConfigWebApplicationContext- Refreshing Root WebApplicationContext: startup date [Mon Nov 25 22:43:39 GMT 2013]; root of context hierarchy
org.springframework.context.annotation.ClassPathBeanDefinitionScanner- JSR-330 'javax.inject.Named' annotation found and supported for component scanning
org.springframework.web.context.support.AnnotationConfigWebApplicationContext- Registering annotated classes: [class com.mycompany.webclient.config.WebMvcConfig,class com.mycompany.webclient.config.SocialConfig]
You are passing the same context to both the ContextLoaderListener and DispatcherServlet and hence this will trigger loading the configuration twice.
You should have 2 seperate AnnotationConfigWebApplicationContext instances one for the ContextLoaderListener loading all your generic beans (services etc.) and one for the DispatcherServlet loading the web related things.
public class WebClientInitialiser implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(MainConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
container.addListener(new MyCompanyContextListener());
container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain"))
.addMappingForUrlPatterns(null, false, "/*");
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(WebMvcConfig.class, SocialConfig.class);
// Register and map the dispatcher servlet
Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
}
}
Something like this. Also you don't need to set the contextConfigLocation simply register the #Configuration annotated classes. Also setting the ServletContext is already done by Spring so no need for that to.
A note on your configuration, the PropertySourcesPlaceHolderConfigurer is enabled by default so no need to register that again in in MainConfig class.
Another thing to take into account is that now probably your application fails (i.e. your #Controllers don't work anymore). This is due to the fact that everything is inside the root application context whereas #Controllers should be loaded by the DispatcherServlet. To fix this you need to exclude #Controller scanning in your MainConfig and enable #Controller scanning on the WebMvcConfig class.