网站改版对排名的影响,高端网站开发方案,wordpress 博客类模板,网页设计与制作投稿文章目录 前言1、在初始化时保存ApplicationContext对象2、通过Spring提供的工具类获取ApplicationContext对象3、实现接口ApplicationContextAware#xff08;推荐#xff09;4、继承自抽象类ApplicationObjectSupport5、继承自抽象类WebApplicationObjectSupport6、使用Bea… 文章目录 前言1、在初始化时保存ApplicationContext对象2、通过Spring提供的工具类获取ApplicationContext对象3、实现接口ApplicationContextAware推荐4、继承自抽象类ApplicationObjectSupport5、继承自抽象类WebApplicationObjectSupport6、使用BeanFactory直接获取不推荐7、使用ContextLoader提供的getCurrentWebApplicationContext方法8、实现接口BeanFactoryPostProcessor扩展BeanFactoryApplicationContext 前言 1、在初始化时保存ApplicationContext对象
适用于Spring框架的独立应用程序须要程序通过配置文件初始化Spring。 applicationContext.xml配置
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
bean idtest classcom.sxtx.bean.Test
/bean/beans代码
Test
public void test() {ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);//ApplicationContext applicationContext new FileSystemXmlApplicationContext(applicationContext.xml); Test test (Test) applicationContext.getBean(test);System.out.println(test);
}2、通过Spring提供的工具类获取ApplicationContext对象
适合于Spring框架的B/S系统通过ServletContext对象获取ApplicationContext对象。然后在通过它获取须要的类实例。以下两个工具方式的差别是前者在获取失败时抛出异常。后者返回null。
ApplicationContext ac1 WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean(beanId);
ac2.getBean(beanId);3、实现接口ApplicationContextAware推荐
实现该接口的setApplicationContext(ApplicationContext context)方法并保存ApplicationContext 对象。Spring初始化时扫描到该类就会通过该方法将ApplicationContext对象注入。然后在代码中就可以获取spring容器bean了。 例如 User bean SpringUtils.getBean(“user”); Component
public class SpringUtils implements ApplicationContextAware {private static ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringUtils.applicationContext applicationContext;}public static T T getBean(String beanName) {if(applicationContext.containsBean(beanName)){return (T) applicationContext.getBean(beanName);}else{return null;}}public static T MapString, T getBeansOfType(ClassT baseType){return applicationContext.getBeansOfType(baseType);}
}4、继承自抽象类ApplicationObjectSupport
调用父类的getApplicationContext()方法获取Spring容器对象。
Service
public class SpringContextHelper extends ApplicationObjectSupport {public Object getBean(String beanName) {return getApplicationContext().getBean(beanName);}
}5、继承自抽象类WebApplicationObjectSupport
调用getWebApplicationContext()获取WebApplicationContext
Service
public class SpringContextHelper extends WebApplicationObjectSupport {public Object getBean(String beanName) {return getApplicationContext().getBean(beanName);}
}6、使用BeanFactory直接获取不推荐
使用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);
}7、使用ContextLoader提供的getCurrentWebApplicationContext方法
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);
}8、实现接口BeanFactoryPostProcessor
spring工具类 方便在非spring管理环境中获取bean
Component
public final class SpringUtilsS implements BeanFactoryPostProcessor
{/** Spring应用上下文环境 */private static ConfigurableListableBeanFactory beanFactory;Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
{SpringUtilsS.beanFactory beanFactory;}/*** 获取对象** param name* return Object 一个以所给名字注册的bean的实例* throws BeansException**/SuppressWarnings(unchecked)public static T T getBean(String name) throws BeansException
{return (T) beanFactory.getBean(name);}/*** 获取类型为requiredType的对象** param clz* return* throws BeansException**/public static T T getBean(ClassT clz) throws BeansException
{T result (T) beanFactory.getBean(clz);return result;}/*** 如果BeanFactory包含一个与所给名称匹配的bean定义则返回true** param name* return boolean*/public static boolean containsBean(String name)
{return beanFactory.containsBean(name);}/*** 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到将会抛出一个异常NoSuchBeanDefinitionException** param name* return boolean* throws NoSuchBeanDefinitionException**/public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
{return beanFactory.isSingleton(name);}/*** param name* return Class 注册对象的类型* throws NoSuchBeanDefinitionException**/public static Class? getType(String name) throws NoSuchBeanDefinitionException{return beanFactory.getType(name);}/*** 如果给定的bean名字在bean定义中有别名则返回这些别名** param name* return* throws NoSuchBeanDefinitionException**/public static String[] getAliases(String name) throws NoSuchBeanDefinitionException{return beanFactory.getAliases(name);}/*** 获取aop代理对象* * param invoker* return*/SuppressWarnings(unchecked)public static T T getAopProxy(T invoker)
{return (T) AopContext.currentProxy();}
}扩展
BeanFactory和ApplicationContext是Spring的两大核心接口都可以当做Spring的容器。其中ApplicationContext是BeanFactory的子接口。
BeanFactory
1、是Spring里面最底层的接口最原始的接口包含了各种Bean的定义读取bean配置文档管理bean的加载、实例化控制bean的生命周期维护bean之间的依赖关系。
2、采用的是延迟加载形式来注入Bean的即只有在使用到某个Bean时(调用getBean())才对该Bean进行加载实例化。这样我们就不能发现一些存在的Spring的配置问题。如果Bean的某一个属性没有注入BeanFacotry加载后直至第一次使用调用getBean方法才会抛出异常。
3BeanFactory通常以编程的方式被创建。
4BeanFactory和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。