kzht.gm.springframework.extension.context.PrototypeBeanInitializeListener.java Source code

Java tutorial

Introduction

Here is the source code for kzht.gm.springframework.extension.context.PrototypeBeanInitializeListener.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package kzht.gm.springframework.extension.context;

import kzht.gm.springframework.extension.context.XmlWebApplicationContext.LazyBeanDocumentReader;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * AspectJ 1.6.8 ???????<br />
 * <p>
 * ? URL https://jira.springsource.org/browse/SPR-8070 <br />
 * ? org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#
 * wrapIfNecessary(Object, String, Object)
 *
 * {@link ApplicationContext} ??? Bean ????Prototype ? Bean
 * ???????????????????????<br />
 * ???? MT Safe ????????????????? ({@link NullPointerException})
 * ??
 * <p>
 * ?????????? {@link ApplicationContext} ???????? Advice ??????
 * Prototype ? Bean ????
 * {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean(Class)}
 * ?????Advice ?????????(?? javax.validation.Validator
 * ??????????)<br />
 * ????????? Bean ?(?????? Bean ?????????)<br />
 * ???????? //bean/@id ? //bean/@class ????????????????<br />
 * ???????????????? Bean ??? Singleton ????????????????
 * <p />
 * ??? ApplicationContext ???????Servlet (?) ? ApplicationContext
 * ???????Web ? ApplicationContext () ???????????
 * ApplicationContext ?????????? 2 ???????????????
 * () ??????????????????????????????
 * ??????????
 *
 * <p />
 * ? AspectJ 1.6.12 ??????????AspectJ ?????????
 *
 * <pre>
 * Usage :
 *  application.xml, presentation.xml ...
 *    <b>&lt;import resource="classpath:kzht/gm/springframework/extension/context/PrototypeBeanInitializeListener.xml" /&gt;</b>
 *
 * @author kazuhito
 * @see org.springframework.context.support.AbstractApplicationContext#publishEvent(org.springframework.context.ApplicationEvent)
 */
public class PrototypeBeanInitializeListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        initializeAspectJWorld(event.getApplicationContext());
    }

    private void initializeAspectJWorld(ApplicationContext ctx) {
        if (ctx == null) {
            return;
        }

        if (LazyBeanDocumentReader.loadLazyInitAttr()) {
            return;
        }

        for (String name : ctx.getBeanDefinitionNames()) {
            if (ctx.isPrototype(name)) {
                try {
                    // AspectJExpression ??? AspectJ ????
                    ctx.getBean(name);
                } catch (BeansException e) {
                    // ApplicationContext ????????????????
                    // Prototype() ? Prototype() ??????????
                    // ? ApplicationContext ??????????????????
                    // ??????????
                }
            }
        }
    }
}