Spring
是整个Java体系最核心的框架,没有之一- 面试必备
- 技术、思想提升
BeanFactory和ApplicationContext
核心容器
,ApplicationContext 实现和【组合】了它的功能注:ctrl alt B 进入方法的实现类 ctrl F12 列出类中所有方法 类图中f4查看方法源码
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);
});
}
}
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());
});
}
}
- 表面上只有getBean()
- 实际上
控制反转 依赖注入 直至 Bean的生命周期的各种功能
,都由它的实现类提供
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* 可以从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);
}
}
注: 任意的类都可以去监听