您不需要在 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();
}
}
希望这对您有所帮助。干杯。