• SpringMVC中bean的加载控制


    Controller加载控制与业务bean加载控制

    SpringMVC相关bean(表现层bean)

    Spring控制的bean

    业务bean(Service)

    功能bean(DateSource等)

    SpringMVC相关bean加载控制

    SpringMVC加载的bean对应的包均在com.itheima.controller包内

    Spring相关bean加载控制

    方式一:Spring加载的bean设定扫描范围为com.itheima,排除controller包内的bean

    方式二:Spring加载的bean设定扫描范围为精准扫描,例如service包、dao包等

    方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中

    方式一:

    名称:@ComponentScan

    类型:类注解

    范例:

    1. import org.springframework.context.annotation.ComponentScan;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.context.annotation.FilterType;
    4. import org.springframework.stereotype.Controller;
    5. @Configuration
    6. //设置spring配置类加载bean时的过滤规则,当前要求排除掉表现层对应的bean
    7. //excludeFilters属性:设置扫描加载bean时,排除的过滤规则
    8. //type属性:设置排除规则,当前使用按照bean定义时的注解类型进行排除
    9. //classes属性:设置排除的具体注解类,当前设置排除@Controller定义的bean
    10. @ComponentScan(value="com.itheima",
    11. excludeFilters = @ComponentScan.Filter(
    12. type = FilterType.ANNOTATION,
    13. classes = Controller.class
    14. )
    15. )
    16. public class SpringConfig {
    17. }

    属性:

    excludeFilters:排除扫描路径中加载的bean,需要指定类别(type)与具体项(classes)

    includeFilters:加载指定的bean,需要指定类别(type)与具体项(classes)

    方式三:

    bean的加载格式:

    1. public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    2. protected WebApplicationContext createServletApplicationContext() {
    3. AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    4. ctx.register(SpringMvcConfig.class);
    5. return ctx;
    6. }
    7. protected WebApplicationContext createRootApplicationContext() {
    8. AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    9. ctx.register(SpringConfig.class);
    10. return ctx;
    11. }
    12. protected String[] getServletMappings() {
    13. return new String[]{"/"};
    14. }
    15. }

    简化开发:

    1. //web配置类简化开发,仅设置配置类类名即可
    2. public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    3. protected Class<?>[] getRootConfigClasses() {
    4. return new Class[]{SpringConfig.class};
    5. }
    6. protected Class<?>[] getServletConfigClasses() {
    7. return new Class[]{SpringMvcConfig.class};
    8. }
    9. protected String[] getServletMappings() {
    10. return new String[]{"/"};
    11. }
    12. }
  • 相关阅读:
    【转】iOS消息推送机制
    [重磅来袭] 功能强大的开源数据中台系统 DataCap 1.14.0 发布
    个微号的二次开发,api
    卷出头了,终于学完阿里架构师推荐 413 页微服务分布式架构基础与实战笔记
    计算机学院第一周语法组及算法组作业
    vlan虚拟局域网学习笔记
    嵌入式学习笔记(29)轮询方式处理按键
    数据库查询方式
    基于django的食堂外卖系统的设计与实现
    SAP SEGW 事物码里的 Association 建模方式
  • 原文地址:https://blog.csdn.net/m0_61961937/article/details/125398260