3

我有一个配置文件

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.*" />

有用。

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

4

3 回答 3

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

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

com.mypackage.referencedata.config

会正常工作。

于 2012-06-08T07:59:43.770 回答
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();
    }
}

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

于 2012-06-08T08:32:05.643 回答
-1

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

于 2019-10-02T08:09:00.587 回答