BeanFactoryPostProcessor
接口实现
BeanFactoryPostProcessor
接口,并重写postProcessBeanFactory
方法,通过ConfigurableListableBeanFactory
对象获取所需要的Bean
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;/*** @ApiNote: Spring工具类* @Author: 陌路* @Date: 2022/11/2 22:49* @Tool: Created by IntelliJ IDEA*/
@Component
public final class SpringUtils implements BeanFactoryPostProcessor {private static final Logger LOGGER = LoggerFactory.getLogger(SpringUtils.class);/** Spring应用上下文环境 */private static ConfigurableListableBeanFactory beanFactory;@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {SpringUtils.beanFactory = beanFactory;}public static ConfigurableListableBeanFactory getBeanFactory() {return beanFactory;}/*** @apiNote: 获取对象* @param: name 对象名* @return: Object 一个以所给名字注册的bean的实例*/@SuppressWarnings("unchecked")public static T getBean(String name) {if (getBeanFactory() == null) {LOGGER.info("没有BeanFactory,忽略错误");return null;}return (T) getBeanFactory().getBean(name);}/*** @apiNote: 获取类型为requiredType的对象* @param: name 类名称* @return: T*/public static T getBean(Class name) {if (getBeanFactory() == null) {LOGGER.info("本地调试Main模式,没有BeanFactory,忽略错误");return null;}return (T) getBeanFactory().getBean(name);}/*** @apiNote: 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true* @param: name* @return: boolean*/public static boolean containsBean(String name) {return getBeanFactory().containsBean(name);}/*** @apiNote: 判断bean是singleton还是prototype。* @Desc: 如果与name对应的bean没被找到,将会抛出(NoSuchBeanDefinitionException)* @param: name 实例对象名称* @return: 是singleton true*/public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {return getBeanFactory().isSingleton(name);}/*** @apiNote: 注册对象的类型* @param: name 对象名称* @return: clazz*/public static Class> getType(String name) throws NoSuchBeanDefinitionException {return getBeanFactory().getType(name);}/*** @apiNote: 如果name在bean定义中有别名,则返回这些别名* @param: name 对象别名* @return: String[]*/public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {return getBeanFactory().getAliases(name);}
}
ApplicationContextAware
接口实现
ApplicationContextAware
接口,并重写setApplicationContext
方法,通过ApplicationContext
对象获取所需要的Bean
对象
import org.springframework.aop.framework.AopContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/*** @ApiNote: spring工具类 方便在非spring管理环境中获取bean* @Author: 陌路* @Date: 2022/11/2 20:40* @Tool: Created by IntelliJ IDEA*/
@Component
public final class ApplicationContextHolder implements ApplicationContextAware {/*实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext对象。Spring初始化时,扫描到该类,就会通过该方法将ApplicationContext对象注入。然后在代码中就可以获取spring容器bean了。*//** Spring应用上下文环境 */private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) {ApplicationContextHolder.applicationContext = applicationContext;}/*** @apiNote: 根据对象名获取对象* @param: name* @return: T*/@SuppressWarnings("unchecked")public static T getBean(String name) {return (T) applicationContext.getBean(name);}/*** @apiNote: 反射获取对象实例* @param: clazz* @return: T*/public static T getBean(Class clz) {T result = (T) applicationContext.getBean(clz);return result;}/*** @apiNote: BeanFactory是否包含指定的bean* @param: name 实例对象名称* @return: 包含true*/public static boolean containsBean(String name) {return applicationContext.containsBean(name);}/*** @apiNote: 判断bean是singleton还是prototype。* @Desc: 如果与name对应的bean没被找到,将会抛出(NoSuchBeanDefinitionException)* @param: name 实例对象名称* @return: 是singleton true*/public static boolean isSingleton(String name) {return applicationContext.isSingleton(name);}/*** @apiNote: 注册对象的类型* @param: name 对象名称* @return: clazz*/public static Class> getType(String name) {return applicationContext.getType(name);}/*** @apiNote: 如果name在bean定义中有别名,则返回这些别名* @param: name 对象别名* @return: String[]*/public static String[] getAliases(String name) {return applicationContext.getAliases(name);}/*** @apiNote: 获取aop代理对象* @param: invoker* @return: T*/@SuppressWarnings("unchecked")public static T getAopProxy(T invoker) {return (T) AopContext.currentProxy();}/*** @apiNote: 获取当前的环境配置,无配置返回null* @return: 当前的环境配置*/public static String[] getActiveProfiles() {return applicationContext.getEnvironment().getActiveProfiles();}/*** @apiNote: 获取当前的环境配置,当有多个环境配置时,只获取第一个* @return: 当前的环境配置*/public static String getActiveProfile() {final String[] activeProfiles = getActiveProfiles();return ObjUtil.arrIsEmpty(activeProfiles) ? null : activeProfiles[0];}
}
适用于Spring框架的独立应用程序,需要程序通过配置文件初始化Spring。
需要applicationContext.xml
配置文件配置
@Test
public void test() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml"); User user = (User) applicationContext.getBean("user");System.out.println(test);
}
适合于Spring框架的B/S系统,通过
ServletContext
对象获取ApplicationContext
对象。然后在通过它获取须要的类实例。以下两个工具方式的差别是,ServletContext
在获取失败时抛出异常。ApplicationContext
返回null。
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
实现该接口的
setApplicationContext(ApplicationContext context)
方法,并保存ApplicationContext
对象。Spring
初始化时,扫描到该类,就会通过该方法将ApplicationContext
对象注入。然后在代码中就可以获取spring
容器bean
了。
见上文实现ApplicationContextAware接口
调用父类的
getApplicationContext()
方法,获取Spring
容器对象。
public class SpringContextHelper extends ApplicationObjectSupport {public Object getBean(String beanName) {return getApplicationContext().getBean(beanName);}
}
调用
getWebApplicationContext()
获取WebApplicationContext
public class SpringContextHelper extends WebApplicationObjectSupport {public Object getBean(String beanName) {return getApplicationContext().getBean(beanName);}
}
使用
BeanFactory
从工厂中直接获取Bean实例,但是XmlBeanFactory
类已经废弃,因此不建议使用。
@Test
public void test() {BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));Test test= (Test) beanFactory.getBean("test");System.out.println(test);
}
@Test
public void test() {MockServletContext sc = new MockServletContext("");sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/applicationContext.xml");ServletContextListener listener = new ContextLoaderListener();ServletContextEvent event = new ServletContextEvent(sc);listener.contextInitialized(event);WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();Test test= (Test) wac.getBean("test");System.out.println(test);
}
spring工具类 方便在非spring管理环境中获取bean
见上文实现BeanFactoryPostProcessor接口
BeanFactory
和ApplicationContext
是Spring
的两大核心接口,都可以当做Spring
的容器。其中ApplicationContext
是BeanFactory
的子接口。
BeanFactory
(1)、是Spring里面最底层的接口(最原始的接口),包含了各种Bean的定义,读取bean配置文档,管理bean的加载、实例化,控制bean的生命周期,维护bean之间的依赖关系。
(2)、采用的是延迟加载形式来注入Bean的,即只有在使用到某个Bean时(调用getBean()),才对该Bean进行加载实例化。这样,我们就不能发现一些存在的Spring的配置问题。如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用调用getBean方法才会抛出异常。
(3)BeanFactory通常以编程的方式被创建。
(4)BeanFactory和ApplicationContext都支持BeanPostProcessor、BeanFactoryPostProcessor的使用,但两者之间的区别是:BeanFactory需要手动注册,而ApplicationContext则是自动注册。
(5) 占用内存小。
ApplicationContext
1、ApplicationContext接口作为BeanFactory的派生,除了提供BeanFactory所具有的功能外,还提供了更完整的框架功能:
继承MessageSource,因此支持国际化。
统一的资源文件访问方式。
提供在监听器中注册bean的事件。
同时加载多个配置文件。
载入多个(有继承关系)上下文 ,使得每一个上下文都专注于一个特定的层次,比如应用的web层。
2、ApplicationContext,它是在容器启动时,一次性创建了所有的Bean。这样,在容器启动时,我们就可以发现Spring中存在的配置错误,这样有利于检查所依赖属性是否注入。ApplicationContext启动后预载入所有的单实例Bean,通过预载入单实例bean ,确保当你需要的时候,你就不用等待,因为它们已经创建好了。
3、ApplicationContext占用内存空间大,当程序的配置bean特别多时,程序启动慢。
4、ApplicationContext能以编程式方式创建,还能能以声明的方式创建,如使用ContextLoader。