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));

    }
}
相关文章
|
JavaScript Windows 容器
Flutter Web:根据浏览器窗口改变布局大小
之前我们通过flutter开发web应用,然后用electron打包成可执行文件在pc端使用,因为electron可以设置最小宽高,所以布局不会越界,但是如果直接在浏览器中打开,因为浏览器的大小无法控制,如果用户缩小浏览器会导致布局越界。根据大部分网站的经验来看,当窗口缩小到一定程度后,布局就不会再改变,反而是增加了滚动,这样就保证了布局的正确性,所以我们也打算这么做。
1684 0
|
监控 网络协议
Win系统 - 如何设置电脑的固定IP地址?
Win系统 - 如何设置电脑的固定IP地址?
1120 0
Win系统 - 如何设置电脑的固定IP地址?
|
机器学习/深度学习 数据采集 存储
AI 音辨世界:艺术小白的我,靠这个AI模型,速识音乐流派选择音乐 ⛵
音乐领域,借助于歌曲相关信息,模型可以根据歌曲的音频和歌词特征,将歌曲精准进行流派分类。本文讲解如何基于机器学习完成对音乐的识别分类。
2978 2
AI 音辨世界:艺术小白的我,靠这个AI模型,速识音乐流派选择音乐 ⛵
|
存储 云安全 运维
如何安全存放数据到对象存储 OSS 及数据湖的13问
数据作为企业的血液和命脉,需要妥善的保存。对象存储作为云计算的数据存储底座,并且还在支持数据湖存储能力,它是企业存储数据的理想之地。但是如何安全的存放数据到存储,特别是公共云对象存储,相信不少的的数据负责人都会有很多疑问,本文从账户认证能力、网络安全配置、访问授权方法、数据加密功能、访问日志审计、数据安全机制等纬度总结了13个安全问题,帮助回答企业上云存放数据的安全顾虑。一、企业上云存放数据的安全
1157 1
如何安全存放数据到对象存储 OSS 及数据湖的13问
|
消息中间件 人工智能 Cloud Native
开放下载 | 飞天技术峰会-云原生加速应用构建分论坛资料开放下载
阿里云飞天技术峰会于 8 月 11 日在深圳举行,峰会以“聚焦核心技术,激活企业内生动力”为主题,邀请众多高新技术领军企业,围绕“企业如何在新一代云计算体系架构之上激活业务、技术、产品的内生动力”这一核心话题展开探讨,共商后疫情时代企业增长的破局之道。
开放下载 | 飞天技术峰会-云原生加速应用构建分论坛资料开放下载
|
前端开发 Java Serverless
玩转《天猫精灵技能平台》,搞一个诗词问答小游戏
看了中国诗词大会,必须要自己搞一个诗词问答小游戏。 文章最后还有一个思路,非常简单的思路,大家可以试着去实现一下。
14256 5
玩转《天猫精灵技能平台》,搞一个诗词问答小游戏
|
弹性计算 Linux 数据安全/隐私保护
ESC初试体验(新手)搭建属于自己的网站
此次体验部分主要包括ESC创建、管理和扩容,总体来说阿里云配置简单,易上手,对于学生党来说简直不要太友好
1037 1
ESC初试体验(新手)搭建属于自己的网站
|
机器学习/深度学习 人工智能 缓存
|
存储 消息中间件 监控
探讨,关于秒合约时间盘交易系统开发逻辑以及前后端代码方案
秒合约交易规则比较简捷,简单来说,首先必须选择要交易的数字货币。交易时间区间短为1min、3min、5min,长为60min;然后风险控制,在我们可以控制的风险范围内设定交易金额,设定盈余止损,最重要的是进行货币方向走势的技术分析。也就是说,在我们设置的交易区间内的涨跌方向,根据分析下单。
3969 0
探讨,关于秒合约时间盘交易系统开发逻辑以及前后端代码方案