• 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. }
  • 相关阅读:
    《红蓝攻防对抗实战》二.内网探测协议出网之TCP/UDP协议探测出网
    Codeforces Round #835 (Div. 4) F. Quests
    物联网浏览器(IoTBrowser)-Java快速对接施耐德网络IO网关
    setoolkit克隆网站并抓取账号密码
    P2251 质量检测
    【附源码】Python计算机毕业设计日租房管理系统
    map的使用方法
    redis 哨兵集群搭建
    一位3年经验的测试工程师水平能差到什么程度?面试后,感叹都是人才呀...
    Sound Siphon for Mac:音频处理与录制工具
  • 原文地址:https://blog.csdn.net/m0_61961937/article/details/125398260