publicJBossLoadTimeWeaver(ClassLoader classLoader){ privatefinal JBossClassLoaderAdapter adapter; Assert.notNull(classLoader, "ClassLoader must not be null"); if (classLoader.getClass().getName().startsWith("org.jboss.modules")) { // JBoss AS 7 or WildFly 8 this.adapter = new JBossModulesAdapter(classLoader); } else { // JBoss AS 6 this.adapter = new JBossMCAdapter(classLoader); } }
privatefinal Cache targetCache; /** * Create a new TransactionAwareCache for the given target Cache. * @param targetCache the target Cache to decorate */ publicTransactionAwareCacheDecorator(Cache targetCache){ Assert.notNull(targetCache, "Target Cache must not be null"); this.targetCache = targetCache; }
/** * Event published as early as conceivably possible as soon as a {@link SpringApplication} * has been started - before the {@link Environment} or {@link ApplicationContext} is * available, but after the {@link ApplicationListener}s have been registered. The source * of the event is the {@link SpringApplication} itself, but beware of using its internal * state too much at this early stage since it might be modified later in the lifecycle. * * @author Dave Syer */ @SuppressWarnings("serial") publicclassApplicationStartedEventextendsSpringApplicationEvent{
/** * Create a new {@link ApplicationStartedEvent} instance. * @param application the current application * @param args the arguments the application is running with */ publicApplicationStartedEvent(SpringApplication application, String[] args){ super(application, args); }
/** * Listener for the {@link SpringApplication} {@code run} method. * {@link SpringApplicationRunListener}s are loaded via the {@link SpringFactoriesLoader} * and should declare a public constructor that accepts a {@link SpringApplication} * instance and a {@code String[]} of arguments. A new * {@link SpringApplicationRunListener} instance will be created for each run. * * @author Phillip Webb * @author Dave Syer */ publicinterfaceSpringApplicationRunListener{
/** * Called immediately when the run method has first started. Can be used for very * early initialization. */ voidstarted();
/** * Called once the environment has been prepared, but before the * {@link ApplicationContext} has been created. * @param environment the environment */ voidenvironmentPrepared(ConfigurableEnvironment environment);
/** * Called once the {@link ApplicationContext} has been created and prepared, but * before sources have been loaded. * @param context the application context */ voidcontextPrepared(ConfigurableApplicationContext context);
/** * Called once the application context has been loaded but before it has been * refreshed. * @param context the application context */ voidcontextLoaded(ConfigurableApplicationContext context);
/** * Called immediately before the run method finishes. * @param context the application context or null if a failure occurred before the * context was created * @param exception any run exception or null if run completed successfully. */ voidfinished(ConfigurableApplicationContext context, Throwable exception);
}
看类注释我们可以知道,需要实现此接口内所定义的这几个方法,ok,来看个实现类:
/** * {@link SpringApplicationRunListener} to publish {@link SpringApplicationEvent}s. * <p> * Uses an internal {@link ApplicationEventMulticaster} for the events that are fired * before the context is actually refreshed. * * @author Phillip Webb * @author Stephane Nicoll */ publicclassEventPublishingRunListenerimplementsSpringApplicationRunListener, Ordered{
@Override publicvoidcontextLoaded(ConfigurableApplicationContext context){ for (ApplicationListener<?> listener : this.application.getListeners()) { if (listener instanceof ApplicationContextAware) { ((ApplicationContextAware) listener).setApplicationContext(context); } context.addApplicationListener(listener); } this.initialMulticaster.multicastEvent( new ApplicationPreparedEvent(this.application, this.args, context)); }
@Override publicvoidfinished(ConfigurableApplicationContext context, Throwable exception){ // Listeners have been registered to the application context so we should // use it at this point context.publishEvent(getFinishedEvent(context, exception)); }
publicclassSingletonSpringTest{ @Test publicvoidtest(){ // retreive two different contexts ApplicationContext firstContext = new FileSystemXmlApplicationContext("applicationContext-test.xml"); ApplicationContext secondContext = new FileSystemXmlApplicationContext("applicationContext-test.xml"); // prove that both contexts are loaded by the same class loader assertTrue("Class loaders for both contexts should be the same", firstContext.getClassLoader() == secondContext.getClassLoader()); // compare the objects from different contexts ShoppingCart firstShoppingCart = (ShoppingCart) firstContext.getBean("shoppingCart"); ShoppingCart secondShoppingCart = (ShoppingCart) secondContext.getBean("shoppingCart"); assertFalse("ShoppingCart instances got from different application context shouldn't be the same", firstShoppingCart == secondShoppingCart); // compare the objects from the same context ShoppingCart firstShoppingCartBis = (ShoppingCart) firstContext.getBean("shoppingCart"); assertTrue("ShoppingCart instances got from the same application context should be the same", firstShoppingCart == firstShoppingCartBis); } }