郑州营销型网站公司电话,各行各业网站建设,创建门户网站,新乡网站建设-中国互联在实际开发中#xff0c;SpringMVC 负责扫描和加载 Controller 层的 Bean 对象#xff0c;而业务层和数据层等其他模块的 Bean 则由 Spring 框架负责扫描和加载。那么#xff0c;如何控制 Spring 仅加载除了 Controller 层之外的其他 Bean 呢#xff1f;为了解决这个问题SpringMVC 负责扫描和加载 Controller 层的 Bean 对象而业务层和数据层等其他模块的 Bean 则由 Spring 框架负责扫描和加载。那么如何控制 Spring 仅加载除了 Controller 层之外的其他 Bean 呢为了解决这个问题通常可以采取以下两种方案来避免 Spring 错误地加载 SpringMVC 的 Bean
直接指定在 Spring 的配置中精确设定扫描范围只加载特定的包例如 service 包和 dao 包。过滤排除将 Spring 的扫描范围设定为 com.it假设 groupId 为 com.it并排除 controller 包内的 Bean。
当然在实际开发中有时也会选择不区分 Spring 和 SpringMVC 的环境将它们加载到同一个环境中。这里创建一个入门版的 SpringMVC 项目进行演示创建过程可以参考 SpringMVC 快速入门。假设现在的 SpringMVC 配置类和启动配置类如下
Configuration
ComponentScan(com.it.controller)
public class SpringMvcConfig {
}public class InitConfig extends AbstractDispatcherServletInitializer {// 用于加载 SpringMVC 容器配置Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}// 设置哪些请求归属 SpringMVC 处理Overrideprotected String[] getServletMappings() {// 设置所有的请求归属 SpringMVC 处理return new String[]{/};}// 加载 Spring 容器配置Overrideprotected WebApplicationContext createRootApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringConfig.class);return ctx;}
}方式 1直接指定
在 Spring 配置类中通过 ComponentScan 直接指定多个待扫描模块
Configuration
ComponentScan({com.it.service, com.it.dao})
public class SpringConfig {
}方式 2过滤排除
在 Spring 配置类中通过 ComponentScan 排除不扫描模块
Configuration
ComponentScan(value com.it,excludeFilters ComponentScan.Filter(type FilterType.ANNOTATION,classes Controller.class)
)
public class SpringConfig {
}这里按照注解类型排除不需要扫描的模块说明一下ComponentScan 存在两个重要的属性
excludeFilters排除扫描路径中加载的 Bean需要指定类别type与具体项classesincludeFilters加载指定的 Bean需要指定类别type与具体项classes
测试
在 com.it 下创建一个 App 类观察运行后是否能获取到 Controller 层 Bean
public class App {public static void main(String[] args) {AnnotationConfigApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);System.out.println(ctx.getBean(UserController.class));}
}测试前需要将 SpringMvcConfig 类上的 Configuration 注解先注释掉因为 SpringConfig 在设置扫描 com.it 包时如果包中某个类添加了 Configuration 注解那么这个类被扫描的同时该类 ComponentScan 指定的 com.it.controller 也会被扫描这样会影响 Bean 加载控制的测试效果。
优化
在实际开发中可以优化 InitConfig 类中加载 Spring 和 SpringMVC 容器配置的过程。通过继承 AbstractDispatcherServletInitializer 类的子类使得配置过程更加简洁
public class InitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return new Class[]{SpringConfig.class};}Overrideprotected Class?[] getServletConfigClasses() {return new Class[]{SpringMvcConfig.class};}Overrideprotected String[] getServletMappings() {return new String[]{/};}
}