简体   繁体   中英

Why doesn't component scan pick up my @Configuration?

I have a config file like

package com.mypackage.referencedata.config;

@Configuration
@ComponentScan ("com.mypackage.referencedata.*")
public class ReferenceDataConfig {

In a spring xml if I have

<context:component-scan base-package="com.mypackage.referencedata.config.*" />

it does not get loaded.

If I use

<context:component-scan base-package="com.mypackage.referencedata.*" />

it works.

What gives? I'd expect the 1st to work as well.

<context:component-scan base-package="com.mypackage.referencedata.config.*" />

Will scan packages inside com.mypackage.referencedata.config as it is package.

com.mypackage.referencedata.config

Will work fine.

You don't need to scan the @Configuration class in component scan in SpringFramework. But you need to register it in the Application Initializer class of your web application that defines the configuration required as in web.xml file. You need to implement WebApplicationInitializer interface there and define onStartup method.

In that onStartup method you need to register your @Configuration class to the rootContext of your web application. Please take a look at the following code snippet.

1. The class that works as web.xml

public class ApplicationInitializer implements WebApplicationInitializer {

    //Called first when the application starts loading.
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        System.out.println("Inside application initializer...");

        //Registering the class that incorporates the annotated DispatcherServlet configuration of spring
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(DispatcherConfig.class);

        //Adding the listener for the rootContext
        servletContext.addListener(new ContextLoaderListener(rootContext));

        //Registering the dispatcher servlet mappings.
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}

2. The @Configuration class of web application holds the beans and other setups.

@EnableWebMvc
@Configuration
@ComponentScan(basePackages={"com.abcprocure.servicerepo.controller", "com.abcprocure.servicerepo.model", "com.abcprocure.servicerepo.service"})
public class DispatcherConfig extends WebMvcConfigurerAdapter {

    //Registers the url paths for resources to skip from spring. Eg. JS, CSS and images.
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // TODO Auto-generated method stub
        registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
        registry.addResourceHandler("/html/**").addResourceLocations("/html/**");
    }

    //Defines the ViewResolver that Spring will use to render the views.
    @Bean
    public ViewResolver viewResolver() {
        System.out.println("Inside View Resolver...");
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

    //Defines the DataSource to use in the application.
    @Bean
    public DataSource dataSource() {
        System.out.println("Inside DataSource bean creation....");
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSource.setUrl("jdbc:sqlserver://192.168.100.131;databaseName=test");
        dataSource.setUsername("egptender");
        dataSource.setPassword("egp#123");
        return dataSource;
    }

    //Defines the Hibernate's SessionFactory.
    @Bean
    public SessionFactory sessionFactory() {
        LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource()).addAnnotatedClasses(Services.class, Operations.class, OperationParameters.class, ServiceModels.class, Businesslogic.class,TblFormMaster.class,TblFormBuilder.class);
        builder.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
        builder.setProperty("hibernate.show_sql", "true");
        return builder.buildSessionFactory();
    }
}

Hope this helps you. Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM