Announcement Announcement Module
Collapse
No announcement yet.
No ContextLoaderListener with AbstractAnnotationConfigDispatcherServletInitializ er ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No ContextLoaderListener with AbstractAnnotationConfigDispatcherServletInitializ er ?

    Hi there,
    I use Spring version 3.2.2.RELEASE.

    I want to create a webapp with hibernate and have the folllowing configuration:

    Code:
    public class LifepulseServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
    	@Override
    	protected Class<?>[] getRootConfigClasses() {
    		return new Class[] { PersistenceConfig.class};
    	}
    
    	@Override
    	protected Class<?>[] getServletConfigClasses() {
    		return new Class[] { LifepulseWebConfig.class };
    	}
    
    	@Override
    	protected String[] getServletMappings() {
    		return new String[] { "/" };
    	}
    
    	@Override
    	protected WebApplicationContext createRootApplicationContext() {
    		return super.createRootApplicationContext();
    	}
    
    }
    My two with @Configuration annotated files look like this:

    My PersistenceConfig should be used for the ContextLoaderListener:
    Code:
    @Configuration
    @EnableTransactionManagement(proxyTargetClass=true, mode=AdviceMode.PROXY)
    public class PersistenceConfig{
    	
    	Logger logger = LoggerFactory.getLogger(PersistenceConfig.class);
    	
    	/** The Application Context. */
    	@Autowired
    	ApplicationContext context;
    	
    	/**
    	 * Gets the database url.
    	 *
    	 * @return the database url
    	 */
    	@Bean(name="databaseUrl")
    	public String getDatabaseUrl(){
    		return "jdbc:mysql://localhost/lifepulse";
    	}
    	
    	/**
    	 * Gets the session factory properties.
    	 *
    	 * @return the session factory properties
    	 */
    	@Bean(name="sessionFactoryProperties")
    	public Properties getSessionFactoryProperties(){
    		
    		
    		Properties props = new Properties();
    		props.put("hibernate.dialect", MySQL5InnoDBDialect.class.getName());
    //		String config = context.getEnvironment().getProperty("hibernate.hbm2ddl.auto", "update");
    		props.put("hibernate.hbm2ddl.auto", "create-drop");
    //		config = context.getEnvironment().getProperty("hibernate.hbm2ddl.show_sql", "false");
    		props.put("hibernate.show_sql", "true");
    		props.put("hibernate.format_sql", "true");
    		return props;
    	}
    	
    	
    	/** The Constant ANNOTATED_CLASSES. */
    	@SuppressWarnings("unchecked")
    	private static final Class<? extends Serializable>[] ANNOTATED_CLASSES=new Class[]{
    		Melder.class,
    		LogEntry.class
    	};
    	
    	
    	/**
    	 * Gets the annotated classes.
    	 *
    	 * @return the annotated classes
    	 */
    	private static Class<? extends Serializable>[] getAnnotatedClasses(){
    		return ANNOTATED_CLASSES;
    	}
    	
    	/**
    	 * Gets the data source.
    	 * This bean represents the application's MYSQL datasource, without using xml.
    	 *
    	 * @return the data source
    	 */
    	@Bean
    	public DataSource dataSource() {
    		DriverManagerDataSource dataSource = new DriverManagerDataSource();
    		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    		dataSource.setUrl(getDatabaseUrl());
    		dataSource.setUsername("lifepulse");
    		dataSource.setPassword("lifepulse");
    		return dataSource;
    	}
    
    	/**
    	 * Session factory.
    	 * This bean represents the Hibernate Session Factory. By declaring this bean
    	 * it can easily be injected into Spring DAOs later on.
    	 * 
    	 * @return the local session factory bean
    	 */
    	@Bean
    	public LocalSessionFactoryBean sessionFactory() {
    		
    		LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
    		factory.setAnnotatedClasses(getAnnotatedClasses());		
    		factory.setHibernateProperties(getSessionFactoryProperties());
    		factory.setDataSource(dataSource());
    		return factory;
    	}
    
    	@Bean()
    	public GenericDao genericDao() {
    		return new HibernateDaoImpl();
    	}
    
    	@Bean
    	PlatformTransactionManager txManager(){
    		HibernateTransactionManager htm = new HibernateTransactionManager(sessionFactory().getObject());
    		return htm;
    	}
    }
    My LifepulseWebConfig should be used for the DispatcherSerlvet:

    Code:
    @Configuration
    @EnableWebMvc
    @ComponentScan(value= {"com.ansiworks.lifepulse.controllers"})
    @EnableTransactionManagement(proxyTargetClass=true, mode=AdviceMode.PROXY)
    public class LifepulseWebConfig extends WebMvcConfigurerAdapter{ 
    	@Autowired
    	ApplicationContext context;
    	
    	
    	@Bean
    	ViewResolver viewResolver(){
    		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    		//resolver.setPrefix("");
    		resolver.setSuffix(".jsp");
    		return resolver;
    	}
    	
    	@Bean
    	MainService mainService(){
    		return new MainServiceImpl();
    	}
    
    }
    This configuration doesn't work. The dispathcer servlet doesn't load my PersistenceConfig, and so the beans are not herited to the LifepulseWebConfig.

    But: When I add the PersistenceConfig to the class-array of the Method LifepulseServletInitializer#getServletConfigClasse s() everything works fine.
    However... I can't use sprin-security in this way....

    What am I doing wrong!? Why are the config-classes returned by LifepulseServletInitializer#getRootConfigClasses() not read by the ContextLoaderListener?

  • #2
    Now I also tried this approach with an WebApplicationInitializer Implementation:

    Code:
    public class MyWebAppInitializer implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext container) {
          // Create the 'root' Spring application context
          AnnotationConfigWebApplicationContext rootContext =
            new AnnotationConfigWebApplicationContext();
          rootContext.register(PersistenceConfig.class);
    
          // Manage the lifecycle of the root application context
          container.addListener(new ContextLoaderListener(rootContext));
    
          // Create the dispatcher servlet's Spring application context
          AnnotationConfigWebApplicationContext dispatcherContext =
            new AnnotationConfigWebApplicationContext();
          dispatcherContext.register(LifepulseWebConfig.class);
    
          // Register and map the dispatcher servlet
          ServletRegistration.Dynamic dispatcher =
            container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
        }
    
     }
    The config-classes are the same like above....
    However: The beans of the PersistenceConfig are not instatntiated.... What step is missing?

    Comment


    • #3
      Seems to be a bug in Jetty 9.0: https://bugs.eclipse.org/bugs/show_bug.cgi?id=402982

      Comment

      Working...
      X