Bean加载控制

简介: Bean加载控制

在这里插入图片描述

🍁博客主页:👉 不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:👉 SpringMVC注解开发
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
🔥欢迎大佬指正,一起 学习!一起加油!

在这里插入图片描述

@TOC


一、SpringMVc简介

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

  • SpringMVc相关bean(表现层bean)
  • Spring控制的bean

    • 业务bean (Service)
    • 功能bean (DataSource等)
    • SpringMVC相关bean加载控制

      • SpringMVC加载的bean对应的包均在com.study.controller包内
      • Spring相关bean加载控制

        • 方式一: Spring加载的bean设定扫描范围为com.study,排除掉controller包内的bean
        • 方式二: Spring加载的 bean设定扫描范围为精准范围,例如service包、dao包等
        • 方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中
  • @ComponentScan

    • 类型:类注解
@Configuration
//@ComponentScan({com.study.service},{com.study.dao})
@ComponentScan(value = "com.study" ,
     excludeFilters =@Componentscan.Filter(
          type = FilterType.ANNOTATION,
         classes = Controller.class
         )
   )
public class SpringConfig {
}
  • 属性
  • excludeFilters:排除扫描路径中加载的bean,需要指定类别(type)与具体项(classes)
  • includeFilters:加载指定的bean,需要指定类别(type)与具体项(classes)
  • Bean的加载格式
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    //加载SpringMVC容器配置
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }
    //设置哪些请求可以归属SpringMVC处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //加载Spring容器配置
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringConfig.class);
        return ctx;
    }
}
  • 简化版
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

二、案例

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>SpringMVC-Demo1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

 <dependencies>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>5.2.22.RELEASE</version>
   </dependency>
   <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
     <version>4.0.1</version>
     <scope>provided</scope>
   </dependency>
 </dependencies>
</project>

2.ServletContainersInitConfig

package com.study.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
/*
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    //加载SpringMVC容器配置
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }
    //设置哪些请求可以归属SpringMVC处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //加载Spring容器配置
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringConfig.class);
        return ctx;
    }
}*/

3.SpringConfig

package com.study.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.study")
public class SpringConfig {
}

4.SpringMvcConfig

package com.study.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.study.controller")
public class SpringMvcConfig {
}

5.UserController

package com.study.controller;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller //定义Bean
public class UserController {
    @RequestMapping("/save")
    @ResponseBody  //设置当前操作的返回值类型
    public String save(){
        System.out.println("user save....");
        return "{'module':'SpringMVC'}";
    }

}

6.Test

package com.study;

import com.study.config.SpringConfig;
import com.study.controller.UserController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
        System.out.println(app.getBean(UserController.class));

    }
}
相关文章
|
JSON 负载均衡 Java
SpringCloud Feign 远程调用(史上最详细讲解)
SpringCloud Feign 远程调用(史上最详细讲解)
15952 0
SpringCloud Feign 远程调用(史上最详细讲解)
|
消息中间件 API 芯片
RT-Thread记录(十六、SFUD组件 — SPI Flash的读写)
从本文开始,测试学习一些 RT-Thread 常用的组件与软件包,先从刚学完的 SPI 设备开始。
2008 0
RT-Thread记录(十六、SFUD组件 — SPI Flash的读写)
|
Java Android开发 Windows
IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
设置自动导包功能、设置显示行号和方法间的分隔符、忽略大小写提示、置取消单行显示 tabs 的操作、设置默认的字体、字体大小、字体行间距、修改当前主题的字体、字体大小、字体行间距、修改当前主题的控制台输出的字体及字体大小、修改代码中注释的字体颜色、设置超过指定 import 个数,改为* 、修改类头的文档注释信息、设置项目文件编码、设置当前源文件的编码、设置自动编译、设置为省电模式、设置代码水平或垂直显示.........
1294 1
IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
|
分布式计算 Hadoop Shell
Hadoop中HDFS的Shell操作(开发重点)、启动Hadoop集群、基本语法、常用命令实操、命令大全、-help、-mkdir、-moveFromLocal、-copyFromLocal
-help:-mkdir:-moveFromLocal:-copyFromLocal:从本地文件系统中拷贝文件到HDFS路径去、-put:等同于copyFromLocal,生产环境更习惯用put、-appendToFile:追加一个文件到已经存在的文件末尾、-copyToLocal:从HDFS拷贝到本地、-get:等同于copyToLocal,-ls: -cat:-chgrp、-chmod、-chown:-mkdir:-cp:-mv:-tail:-rm:-rm -r:-du:-setrep:......
662 1
Hadoop中HDFS的Shell操作(开发重点)、启动Hadoop集群、基本语法、常用命令实操、命令大全、-help、-mkdir、-moveFromLocal、-copyFromLocal
|
Arthas 监控 Java
使用阿里arthas 定位oom问题原因
1,最近我们生产服务器在做营销活动的时候突然发生oom,堆内存溢出。这个很让人头疼, 我们开发排查了好长时间,最终找到了问题的所在。这里我先买一个关子,问题的原因大家看完文章就一清二白了。
使用阿里arthas  定位oom问题原因
|
物联网 大数据
参赛作品15:毫米波智慧监护系统
“智物智造杯-2022物联网创新应用大赛”投票开始啦!
844 2
|
存储 关系型数据库 MySQL
Python图书管理系统(一)
初识python的小乐趣
748 0
Python图书管理系统(一)