spring高级49讲
Spring高级49讲
Spring
是整个Java体系最核心的框架,没有之一- 面试必备
- 技术、思想提升
一.容器和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<String, Object> map = (Map<String, Object>) 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<String, Object> map = (Map<String, Object>) 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<String, Object> map = (Map<String, Object>) 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<String, Object> map = (Map<String, Object>) 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);
}
}
}
-
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); @EventListener public void aaa(UserReigisteredEvent event) { log.debug("{}", event); } }
注:
任意的类都可以去监听
文章评论