为什么组件扫描没有拿起我的@Configuration?

时间:2012-06-08 07:54:42

标签: java spring configuration annotations

我有一个像

这样的配置文件
package com.mypackage.referencedata.config;

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

如果我有一个弹簧xml

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

它没有加载。

如果我使用

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

它有效。

是什么给出的?我希望第一个也可以工作。

3 个答案:

答案 0 :(得分:4)

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

将扫描com.mypackage.referencedata.config内的包,因为它是包。

com.mypackage.referencedata.config

工作正常。

答案 1 :(得分:2)

您不需要在SpringFramework中扫描组件扫描中的@Configuration类。但是您需要在Web应用程序的Application Initializer类中注册它,该类定义了web.xml文件中所需的配置。您需要在那里实现WebApplicationInitializer接口并定义onStartup方法。

在该onStartup方法中,您需要将@Configuration类注册到Web应用程序的rootContext。请查看以下代码段。

<强> 1。作为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。 Web应用程序的@Configuration类包含bean和其他设置。

@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();
    }
}

希望这会对你有所帮助。欢呼声。

答案 2 :(得分:-1)

如果您使用的是maven,请检查您是否具有正确的依赖项