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

    }
}
相关文章
|
2月前
|
Java Spring
SpringMVC中bean的加载控制
SpringMVC中bean的加载控制
26 0
|
11月前
|
XML Java 数据库
SpringIOC操作Bean管理--最终章
SpringIOC操作Bean管理--最终章
64 0
|
11月前
|
XML Java 数据格式
延迟初始化Bean会影响依赖注入吗
延迟初始化Bean会影响依赖注入吗
39 0
|
Java Spring
任何 bean 初始化回调前自定义逻辑
任何 bean 初始化回调前自定义逻辑
|
Java
Springboot加载动态Bean的10种方式
Springboot加载动态Bean的10种方式
553 0
|
消息中间件 存储 安全
类是如何加载的?
类是如何加载的?
105 0
|
Java 容器 Spring
Bean的加载方式
Bean的加载方式 1.XML方式声明bean 2.XML+注解方式声明bean 3.注解方式声明配置类 扩展1——FactoryBean 扩展2——配置类中导入原始的配置文件(系统迁移) 扩展3——proxyBeanMethods 4.使用@Import导入要注入的bean 扩展4——使用@Import注解还可以导入配置类 5.使用上下文对象在容器初始化完毕后注入bean 6.导入实现了ImportSelector接口的类,实现对导入源的编程式处理 bean的加载方式(七) bean的加载方式(八)
154 1
|
Java 应用服务中间件 Maven
|
Java Spring
SpringMVC的bean的加载及控制
SpringMVC的bean的加载及控制
95 0