Results 1 to 2 of 2

Thread: @ComponentScan can't find my Controllers

  1. #1

    Default @ComponentScan can't find my Controllers

    Hello,

    I'm going crazy...

    I'm trying to configure my application without XML files. I removed servlet-context.xml, root-context.xml and web.xml from my project.

    then i created 2 classes, AppConfiguration and AppInitializer. While application start im getting this log output:

    08.11.2012 00:10:45 org.apache.catalina.core.ApplicationContext log
    INFO: Spring WebApplicationInitializers detected on classpath: [de.jawb.testproject._cfg.AppInitializer@5c69d74]
    08.11.2012 00:10:45 org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring root WebApplicationContext
    08.11.2012 00:10:47 org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet 'dispatcher'
    08.11.2012 00:10:47 org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/testproject] is completed
    as you can see no mapping occurs... some days ago i used xml configuration like <context:component-scan base-package="de.jawb.testproject.*" /> it worked fine.

    some advices?


    AppConfiguration

    Code:
    @Configuration
    @EnableWebMvc
    @EnableTransactionManagement
    @ComponentScan(basePackages={
    		"de.jawb.testproject.controllers",
    		"de.jawb.testproject.db.tables.user.service",
    		"de.jawb.testproject.db.tables.user.dao",
    		"de.jawb.testproject.db.tables.tour.service",
    		"de.jawb.testproject.db.tables.tour.dao"
    // AND 10000000 other packages couse recursively scan IS NOT PISSIBLE....
    		})
    public class AppConfiguration extends WebMvcConfigurerAdapter {
    	
    	@Bean
    	public InternalResourceViewResolver getInternalResourceViewResolver() {
    		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    		resolver.setPrefix("/WEB-INF/views/");
    		resolver.setSuffix(".jsp");
    		return resolver;
    	}	
    	@Bean
    	public DataSource myDataSource() {
    		DriverManagerDataSource dataSource = new DriverManagerDataSource();
    		dataSource.setDriverClassName(Driver.class.getName());
    		dataSource.setUrl("jdbc:mysql://localhost/testproject");
    		dataSource.setUsername("root");
    		dataSource.setPassword("");
    		return dataSource;
    	}	
    	@Bean
    	public AnnotationSessionFactoryBean sessionFactory() {
    		
    		Properties props = new Properties();
    		props.put("hibernate.dialect", CustomMysqlDialect.class.getName());
    		props.put("hibernate.format_sql", "true");
    		props.put("hibernate.connection.charSet", "UTF-8");
    		props.put("hibernate.connection.useUnicode", "true");
    		props.put("hibernate.show_sql", "false");
    		props.put("hibernate.connection.characterEncoding", "UTF-8");
    		AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();		
    		sessionFactory.setPackagesToScan(new String[] { "de.jawb.testproject.db.*" });
    		sessionFactory.setHibernateProperties(props);
    		sessionFactory.setDataSource(myDataSource());
    		sessionFactory.setSchemaUpdate(true);
    		return sessionFactory;
    	}	
    	@Bean
    	public HibernateTransactionManager transactionManager() {
    		return new HibernateTransactionManager(sessionFactory().getObject());
    	}	
    	@Bean
    	public ReloadableResourceBundleMessageSource messageSource() {
    		ReloadableResourceBundleMessageSource msrc = new ReloadableResourceBundleMessageSource();
    		msrc.setBasename("/WEB-INF/lang/lang");
    		msrc.setCacheSeconds(0);
    		return msrc;
    	}	
    	@Override
    	public void addInterceptors(InterceptorRegistry registry) {
    		OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
    		interceptor.setSessionFactory(transactionManager().getSessionFactory());
    		registry.addWebRequestInterceptor(interceptor);
    	}	
    	@Override
    	public void addResourceHandlers(ResourceHandlerRegistry registry) {
    		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    	}
    }
    AppInitializer

    Code:
    public class AppInitializer implements WebApplicationInitializer {
    
    	public void onStartup(ServletContext servletContext) throws ServletException {
    		AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
    		mvcContext.register(AppConfiguration.class);
    		
    		// Manage the lifecycle of the root appcontext
    		servletContext.addListener(new ContextLoaderListener(mvcContext));
    		servletContext.setInitParameter("defaultHtmlEscape", "true");
    
    		ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext));
    		dispatcher.setLoadOnStartup(1);
    		dispatcher.addMapping("/src/*");
    	}
    }
    Spring!! New Version -> New Tutorial

  2. #2

    Default

    seems to be a bug, perhaps it will be solved in the next 20 years.
    Spring!! New Version -> New Tutorial

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •