【问题标题】:Why doesn't component scan pick up my @Configuration?为什么组件扫描不获取我的@Configuration?
【发布时间】:2012-06-12 06:54:00
【问题描述】:

我有一个类似的配置文件

package com.mypackage.referencedata.config;

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

如果我有的话,在 spring xml 中

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

它没有被加载。

如果我使用

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

它有效。

什么给了?我希望第一个也能正常工作。

【问题讨论】:

    标签: java spring configuration annotations


    【解决方案1】:
    <context:component-scan base-package="com.mypackage.referencedata.config.*" />
    

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

    com.mypackage.referencedata.config
    

    会正常工作。

    【讨论】:

    • 你是对的,是讨厌的 * 破坏了一些东西。
    【解决方案2】:

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

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

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

    希望这对您有所帮助。干杯。

    【讨论】:

    • 我的东西是对现有项目的补充。我不想改变引导过程。
    • 我没有建议你改变你的引导过程。但是我刚刚给出了一个示例,您不需要扫描您的 @Configuration 类,但您需要在根上下文中注册它。
    【解决方案3】:

    如果你使用的是maven,请检查你是否有正确的依赖关系

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2020-12-23
      • 2015-01-19
      • 2017-12-16
      • 2016-10-21
      相关资源
      最近更新 更多