Exception+

导航

 

Spring中ApplicationContext加载机制。
       加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet。
       这两者在功能上完全等同,只是一个是基于Servlet2.3版本中新引入的Listener接口实现,而另一个基于Servlet接口实现。开发中可根据目标Web容器的实际情况进行选择。

spring为ApplicationContext提供有三种实现(举例)

spring为ApplicationContext提供的3种实现分别为:ClassPathXmlApplicationContext,FileSystemXmlApplicationContext和XmlWebApplicationContext,其中XmlWebApplicationContext是专为Web工程定制的。使用举例如下:

1. FileSystemXmlApplicationContext

1 ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件

1 String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};  
2 ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //加载多个配置文件
3 
4 ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");//根据具体路径加载文件 

 DozerBeanMapper对象之间相同属性名赋值

在业务逻辑很复杂的时候,一般都是牵涉到很广,这最直白的表现就是牵连诸多表,然后数据却不是一个对象中的属性或字段,这个时候我们为了方便数据传输一般都会将多个对象中的属性封装到一个VO中,使用Hibernate的人应该知道,查询出来一般都是一个或多个对象,如果依次通过setter方法来赋值的话,效率太低!这时候我们的DozerBeanMapper就体现出来了价值,所以我便将其封装了起来!

/*

* 基于Spring的工具类接口,依赖于容器启动时候的Bean配置<br>
 * 实现该接口的工具类,在Spring容器已经完全启动后才能使用<br>
 * 一般不能是抽象类,使用<code>@Component</code>配置
 * 在启动的时候可能无法取到Bean配置<br>
 * 在容器关闭的时候销毁

*/

在spring容器初始化bean和销毁bean的以前的操作有很多种,
  目前我知道的有:在xml中定义的时候用init-method和destory-method,还有一种就是定义bean的时候实现DisposableBean和InitializingBean 这两个接口

DisposableBean就是在一个bean被销毁的时候,spring容器会帮你自动执行这个方法,对于一些使用完之后需要释放资源的bean,我们都会实现这个接口,或者是配置destory-method方法,只是把afterPropertiesSet改为destroy。

public interface SpringBasedUtil extends DisposableBean {
}
@Component
public class DozerHelper implements SpringBasedUtil {

    /**
     * DozerMapper
     */
    private static Mapper mapper;

    @Autowired(required = false)
    public void injectMapper(Mapper mapper) {
        setMapper(mapper);
    }

    /**
     * 获取Mapper
     * 
     * @return the mapper
     */
    public static Mapper getMapper() {
        if (mapper == null) {
                mapper = (Mapper) SpringContextUtil.getBean("dozerBeanMapper");
            if (mapper == null) {
                mapper = new DozerBeanMapper();
            }
        }
        Assert.notNull(mapper,
                "DozerMapper isn't inited, DozerHelper is not available");
        return mapper;
    }

    /**
     * 使用source映射给target,复制属性
     * 
     * @param source
     * @param target
     * @return
     */
    public static void map(Object source, Object target) {
        getMapper().map(source, target);
    }

    /**
     * 使用source映射给target,复制属性
     * 
     * @param source
     * @param target
     * @return
     */
    public static void map(Object source, Object target, String mapId) {
        getMapper().map(source, target, mapId);
    }

    /**
     * 使用source映射给targetClass,自动生成对象并复制属性
     * 
     * @param source
     * @param targetClass
     * @return
     */
    public static <T> T map(Object source, Class<T> targetClass) {
        return getMapper().map(source, targetClass);
    }
    
    /**
     * 把sourceLst映射成targetClass类型的ArrayList返回
     *
     * @param sourceLst
     * @param targetClass
     * @return
     */
    public static <T> List<T> mapList(List sourceLst, Class<T> targetClass) {
        if (sourceLst == null)
            return null;
        List<T> returnLst = new ArrayList<T>();
        for (Object source : sourceLst) {
            returnLst.add(getMapper().map(source, targetClass));
        }
        return returnLst;
    }
    
    

    /**
     * 使用source映射给targetClass,自动生成对象并复制属性
     * 
     * @param source
     * @param targetClass
     * @param mapId
     * @return
     */
    public static <T> T map(Object source, Class<T> targetClass, String mapId) {
        return getMapper().map(source, targetClass, mapId);
    }

    /**
     * @param mpr
     */
    public static void setMapper(Mapper mpr) {
        mapper = mpr;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.beans.factory.DisposableBean#destroy()
     */
    public void destroy() throws Exception {
        setMapper(null);
    }

}

@Component
public class SpringContextUtil implements ApplicationContextAware
{

    public static ApplicationContext applicationContext; // Spring应用上下文环境

    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     *
     * @param applicationContext
     * @throws BeansException
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
    
    public static void setApplicationContext2(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return SpringContextUtil.applicationContext;
    }

    /**
     * 按照类型获取Bean
     * 
     * @param requiredType
     * @return
     * @throws BeansException
     * @see {@link ApplicationContext#getBean(Class)}
     */
    public static <T> T getBean(Class<T> requiredType) throws BeansException {
        return applicationContext.getBean(requiredType);
    }
    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws BeansException
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
     *
     * @param name         bean注册名
     * @param requiredType 返回对象类型
     * @return Object 返回requiredType类型对象
     * @throws BeansException
     */
    public static Object getBean(String name, Class requiredType) throws BeansException {
        return applicationContext.getBean(name, requiredType);
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
     * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws NoSuchBeanDefinitionException
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws NoSuchBeanDefinitionException
     */
    public static Class getType(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws NoSuchBeanDefinitionException
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getAliases(name);
    }

    /**
     * 发散事件
     * @param event
     */
    public static void publishEvent(ApplicationEvent event){
        getApplicationContext().publishEvent(event);
    }
}

ApplicationContextAware
  其实我们看到---Aware就知道是干嘛用的了,就是属性注入的,但是这个ApplicationContextAware的不同地方在于,实现了这个接口的bean,当spring容器初始化的时候,会自动的将ApplicationContext注入进来

posted on 2017-07-25 17:59  Exception+  阅读(441)  评论(0)    收藏  举报