【spring高级49讲】
创始人
2025-05-31 06:29:35

Spring高级49讲

  1. Spring是整个Java体系最核心的框架,没有之一
  2. 面试必备
  3. 技术、思想提升

一.容器和Bean

第1讲.BeanFactory和ApplicationContext
1.1.BeanFactory
  • BeanFactory是ApplicationContext的父接口
  • BeanFactory才是Spring的核心容器,ApplicationContext 实现和【组合】了它的功能

注:ctrl alt B 进入方法的实现类 ctrl F12 列出类中所有方法 类图中f4查看方法源码

  • (1)获取Springboot中的单例
package com.ttc;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;import java.lang.reflect.Field;
import java.util.Map;@SpringBootApplication
public class Demo1Application {public static void main(String[] args) throws Exception {ConfigurableApplicationContext ctx = SpringApplication.run(Demo1Application.class, args);System.out.println(ctx);Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");singletonObjects.setAccessible(true);ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();Map map = (Map) singletonObjects.get(beanFactory);map.forEach((k, v) -> {System.out.println(k + "=" + v);});}
}
  • (2) 指定componet查询单例 componet可任意指定
package com.ttc;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;import java.lang.reflect.Field;
import java.util.Map;@SpringBootApplication
public class Demo1Application {public static void main(String[] args) throws Exception {ConfigurableApplicationContext ctx = SpringApplication.run(Demo1Application.class, args);System.out.println(ctx);Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");singletonObjects.setAccessible(true);ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();Map map = (Map) singletonObjects.get(beanFactory);map.entrySet().stream().filter(e -> e.getKey().startsWith("componet")).forEach(e -> {System.out.println(e.getKey() + "=" + e.getValue());});}
}
  • (3) BeanFactory 能干啥?
  • 表面上只有getBean()
  • 实际上 控制反转 依赖注入 直至 Bean的生命周期的各种功能,都由它的实现类提供
1.2ApplicationContext
  • getMessage 国际化消息功能
package com.ttc;import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;@SpringBootApplication
public class Demo1Application {public static void main(String[] args) throws Exception {ConfigurableApplicationContext ctx = SpringApplication.run(Demo1Application.class, args);System.out.println(ctx);Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");singletonObjects.setAccessible(true);ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();Map map = (Map) singletonObjects.get(beanFactory);map.entrySet().stream().filter(e -> e.getKey().startsWith("componet")).forEach(e -> {System.out.println(e.getKey() + "=" + e.getValue());});System.out.println(ctx.getMessage("hi", null, Locale.CHINA));System.out.println(ctx.getMessage("hi", null, Locale.ENGLISH));System.out.println(ctx.getMessage("hi", null, Locale.JAPANESE));}
}

在这里插入图片描述

  • getResources

    • classpath 类路径
    • filepath 磁盘路径

    查找文件 classpath* 可以从jar包中寻找

package com.ttc;import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;@SpringBootApplication
public class Demo1Application {public static void main(String[] args) throws Exception {ConfigurableApplicationContext ctx = SpringApplication.run(Demo1Application.class, args);System.out.println(ctx);Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");singletonObjects.setAccessible(true);ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();Map map = (Map) singletonObjects.get(beanFactory);map.entrySet().stream().filter(e -> e.getKey().startsWith("componet")).forEach(e -> {System.out.println(e.getKey() + "=" + e.getValue());});System.out.println(ctx.getMessage("hi", null, Locale.CHINA));System.out.println(ctx.getMessage("hi", null, Locale.ENGLISH));System.out.println(ctx.getMessage("hi", null, Locale.JAPANESE));Resource[] resources = ctx.getResources("classpath*:META-INF/spring.factories");for (Resource resource : resources) {System.out.println(resource);}}
}

qn

cs

  • getEnvironment 获取配置信息

    System.out.println(ctx.getEnvironment().getProperty("java_home"));     // 获取 JAVA_HOME 位置  (  jdk 安装目录)
    System.out.println(ctx.getEnvironment().getProperty("server.port"));   // 获取 tomcat访问端口
    
  • publishEvent

    #  修改日志级别  可以看到 debug 级别
    logging:level:com.ttc: trace
    
           // 发送事件ctx.publishEvent(new UserReigisteredEvent(ctx));
    
    package com.ttc;import org.springframework.context.ApplicationEvent;/*** 设置事件*/
    public class UserReigisteredEvent extends ApplicationEvent {// source 指代事件源public UserReigisteredEvent(Object source) {super(source);}
    }
    package com.ttc;import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;/*** 监听事件*/@Component
    public class Componet2 {private static final Logger log = LoggerFactory.getLogger(Componet2.class);@EventListenerpublic void aaa(UserReigisteredEvent event) {log.debug("{}", event);}
    }

    注: 任意的类都可以去监听

二.AOP

三.Web MVC

四.Spring boot

五.其他

相关内容

热门资讯

世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
吸猫伐是什么意思 吸猫是吸哪里 作为铲屎官,相信对于网络上流行的各种与猫咪相关的梗都是了然于心的,什么吸猫啦,撸猫啦,云养猫啦……等...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...