controller加载控制与业务bean加载控制
SpringMVC相关bean(表现层bean)
Spring控制的bean
- 业务bean(Service)
- 功能bean(DataSource等)
因为功能不同,如何避免Spring错误的加载到SpringMVC的bean?
dart
加载Spring控制的bean的时候排除掉SpringMVC控制的bean
SpringMVC相关bean加载控制
SpringMVC加载的bean对应的包均在com.baidu.controller包内
Spring相关bean加载控制
- 方式一:Spring加载的bean设定扫描范围为
com.baidu,排除掉controller包内的bean - 方式二:Spring加载的bean设定扫描范围为精准范围,例如service包,dao包等(这里用数组)
- 方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中
方式一测试:
SpringConfig

UserController

App


此时并未排除
修改注解


此时已经排除
注意:
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
方式和如下方式一样
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(SpringConfig.class);
如果还存在

排除后是会被这里@ComponentScan("com.baidu.controller")加载的
需要注释@ComponentScan("com.baidu.controller")
bean的加载格式

可以简化
