Note that there are some explanatory texts on larger screens.

plurals
  1. POContextLoaderListener Configuration with AbstractAnnotationConfigDispatcherServletInitialier not loaded
    primarykey
    data
    text
    <p>I use Spring version 3.2.2.RELEASE.</p> <p>I want to create a webapp with hibernate and have the folllowing configuration:</p> <pre><code>public class LifepulseServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class&lt;?&gt;[] getRootConfigClasses() { return new Class[] { PersistenceConfig.class}; } @Override protected Class&lt;?&gt;[] getServletConfigClasses() { return new Class[] { LifepulseWebConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } @Override protected WebApplicationContext createRootApplicationContext() { return super.createRootApplicationContext(); } } </code></pre> <p>My two with @Configuration annotated files look like this:</p> <p>My <strong>PersistenceConfig</strong> should be used for the ContextLoaderListener:</p> <pre><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()); props.put("hibernate.hbm2ddl.auto", "create-drop"); props.put("hibernate.show_sql", "true"); props.put("hibernate.format_sql", "true"); return props; } /** The Constant ANNOTATED_CLASSES. */ @SuppressWarnings("unchecked") private static final Class&lt;? extends Serializable&gt;[] ANNOTATED_CLASSES=new Class[]{ Melder.class, LogEntry.class }; /** * Gets the annotated classes. * * @return the annotated classes */ private static Class&lt;? extends Serializable&gt;[] 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; } } </code></pre> <p>My <strong>LifepulseWebConfig</strong> should be used for the DispatcherSerlvet:</p> <pre><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(); } } </code></pre> <p>This configuration doesn't work. The dispathcer servlet doesn't load my PersistenceConfig, and so the beans are not herited to the LifepulseWebConfig.</p> <p>But: When I add the <strong>PersistenceConfig</strong> to the class-array of the Method <strong>LifepulseServletInitializer#getServletConfigClasses()</strong> everything works fine. However... I can't use sprin-security in this way....</p> <p>What am I doing wrong!? Why are the config-classes returned by <strong>LifepulseServletInitializer#getRootConfigClasses()</strong> not read by the ContextLoaderListener?</p> <p>I also tried it this way (without the <strong>LifepulseServletInitializer</strong> ) with the same effect. The beans of my <strong>PersistenceConfig</strong> are simply not loaded...</p> <pre><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("/"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload