update to spring 5, fix startup
This commit is contained in:
+4
-2
@@ -4,9 +4,11 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
|
||||
|
||||
public class AnnotationsBasedApplicationAndServletInitializer extends AbstractDispatcherServletInitializer {
|
||||
public class AnnotationsBasedApplicationAndServletInitializer //extends AbstractDispatcherServletInitializer
|
||||
{
|
||||
|
||||
@Override
|
||||
//uncomment to run the multiple contexts example
|
||||
//@Override
|
||||
protected WebApplicationContext createRootApplicationContext() {
|
||||
//If this is not the only class declaring a root context, we return null because it would clash
|
||||
//with other classes, as there can only be a single root context.
|
||||
|
||||
+4
-3
@@ -4,9 +4,10 @@ import org.springframework.web.context.AbstractContextLoaderInitializer;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
public class AnnotationsBasedApplicationInitializer extends AbstractContextLoaderInitializer {
|
||||
|
||||
@Override
|
||||
public class AnnotationsBasedApplicationInitializer //extends AbstractContextLoaderInitializer
|
||||
{
|
||||
//uncomment to run the multiple contexts example
|
||||
// @Override
|
||||
protected WebApplicationContext createRootApplicationContext() {
|
||||
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
|
||||
rootContext.register(RootApplicationConfig.class);
|
||||
|
||||
@@ -12,9 +12,10 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class ApplicationInitializer implements WebApplicationInitializer {
|
||||
|
||||
@Override
|
||||
public class ApplicationInitializer //implements WebApplicationInitializer
|
||||
{
|
||||
//uncomment to run the multiple contexts example
|
||||
//@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
//Here, we can define a root context and register servlets, among other things.
|
||||
//However, since we've later defined other classes to do the same and they would clash,
|
||||
|
||||
@@ -5,14 +5,14 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.contexts.normal" })
|
||||
public class NormalWebAppConfig extends WebMvcConfigurerAdapter {
|
||||
public class NormalWebAppConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
|
||||
+8
-6
@@ -4,27 +4,29 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
|
||||
|
||||
public class SecureAnnotationsBasedApplicationAndServletInitializer extends AbstractDispatcherServletInitializer {
|
||||
|
||||
@Override
|
||||
public class SecureAnnotationsBasedApplicationAndServletInitializer// extends AbstractDispatcherServletInitializer
|
||||
{
|
||||
|
||||
//uncomment to run the multiple contexts example
|
||||
//@Override
|
||||
protected WebApplicationContext createRootApplicationContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
//@Override
|
||||
protected WebApplicationContext createServletApplicationContext() {
|
||||
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
|
||||
secureWebAppContext.register(SecureWebAppConfig.class);
|
||||
return secureWebAppContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
//@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/s/api/*" };
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
//@Override
|
||||
protected String getServletName() {
|
||||
return "secure-dispatcher";
|
||||
}
|
||||
|
||||
@@ -5,14 +5,14 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.contexts.secure" })
|
||||
public class SecureWebAppConfig extends WebMvcConfigurerAdapter {
|
||||
public class SecureWebAppConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
|
||||
@@ -3,14 +3,12 @@ package com.baeldung.contexts.normal;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.contexts.services.ApplicationContextUtilService;
|
||||
import com.baeldung.contexts.services.GreeterService;
|
||||
|
||||
@Controller
|
||||
|
||||
+9
-11
@@ -4,29 +4,27 @@ import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class StudentControllerConfig implements WebApplicationInitializer {
|
||||
public class StudentControllerConfig //implements WebApplicationInitializer
|
||||
{
|
||||
|
||||
@Override
|
||||
//uncomment to run the student controller example
|
||||
//@Override
|
||||
public void onStartup(ServletContext sc) throws ServletException {
|
||||
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||
root.register(WebConfig.class);
|
||||
|
||||
root.setServletContext(sc);
|
||||
sc.addListener(new ContextLoaderListener(root));
|
||||
|
||||
//Manages the lifecycle of the root application context.
|
||||
//Conflicts with other root contexts in the application, so we've manually set the parent below.
|
||||
//sc.addListener(new ContextLoaderListener(root));
|
||||
|
||||
GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
|
||||
webApplicationContext.setParent(root);
|
||||
DispatcherServlet dv = new DispatcherServlet(webApplicationContext);
|
||||
|
||||
DispatcherServlet dv = new DispatcherServlet(root);
|
||||
|
||||
ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv);
|
||||
appServlet.setLoadOnStartup(1);
|
||||
appServlet.addMapping("/test/*");
|
||||
|
||||
@@ -6,13 +6,13 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "org.baeldung.controller.controller", "org.baeldung.controller.config" })
|
||||
public class WebConfig extends WebMvcConfigurerAdapter {
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();
|
||||
|
||||
@@ -8,11 +8,11 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.core")
|
||||
public class CoreConfig extends WebMvcConfigurerAdapter {
|
||||
public class CoreConfig implements WebMvcConfigurer {
|
||||
|
||||
public CoreConfig() {
|
||||
super();
|
||||
|
||||
@@ -6,13 +6,16 @@ import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class MainWebAppInitializer implements WebApplicationInitializer {
|
||||
public class MainWebAppInitializer implements WebApplicationInitializer
|
||||
{
|
||||
|
||||
/**
|
||||
* Register and configure all Servlet container components necessary to power the web application.
|
||||
@@ -26,14 +29,11 @@ public class MainWebAppInitializer implements WebApplicationInitializer {
|
||||
root.scan("org.baeldung.spring.config");
|
||||
// root.getEnvironment().setDefaultProfiles("embedded");
|
||||
|
||||
//Manages the lifecycle of the root application context.
|
||||
//Conflicts with other root contexts in the application, so we've manually set the parent below.
|
||||
//sc.addListener(new ContextLoaderListener(root));
|
||||
sc.addListener(new ContextLoaderListener(root));
|
||||
|
||||
// Handles requests into the application
|
||||
GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
|
||||
webApplicationContext.setParent(root);
|
||||
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(webApplicationContext));
|
||||
DispatcherServlet dv = new DispatcherServlet(root);
|
||||
|
||||
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc",dv);
|
||||
appServlet.setLoadOnStartup(1);
|
||||
final Set<String> mappingConflicts = appServlet.addMapping("/");
|
||||
if (!mappingConflicts.isEmpty()) {
|
||||
|
||||
@@ -5,13 +5,13 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
public class MvcConfig extends WebMvcConfigurerAdapter {
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
|
||||
public MvcConfig() {
|
||||
super();
|
||||
@@ -21,8 +21,6 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
super.addViewControllers(registry);
|
||||
|
||||
registry.addViewController("/sample.html");
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@ import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ScopesConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
public HelloMessageGenerator globalSessionMessage() {
|
||||
return new HelloMessageGenerator();
|
||||
}
|
||||
|
||||
+2
-2
@@ -6,13 +6,13 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableWebMvc
|
||||
public class AttributeAnnotationConfiguration extends WebMvcConfigurerAdapter {
|
||||
public class AttributeAnnotationConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
|
||||
Reference in New Issue
Block a user