org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving#org.springframework.instrument.classloading.LoadTimeWeaver源码实例Demo

下面列出了org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving#org.springframework.instrument.classloading.LoadTimeWeaver 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: lams   文件: DefaultContextLoadTimeWeaver.java
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
	LoadTimeWeaver serverSpecificLoadTimeWeaver = createServerSpecificLoadTimeWeaver(classLoader);
	if (serverSpecificLoadTimeWeaver != null) {
		if (logger.isInfoEnabled()) {
			logger.info("Determined server-specific load-time weaver: " +
					serverSpecificLoadTimeWeaver.getClass().getName());
		}
		this.loadTimeWeaver = serverSpecificLoadTimeWeaver;
	}
	else if (InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) {
		logger.info("Found Spring's JVM agent for instrumentation");
		this.loadTimeWeaver = new InstrumentationLoadTimeWeaver(classLoader);
	}
	else {
		try {
			this.loadTimeWeaver = new ReflectiveLoadTimeWeaver(classLoader);
			logger.info("Using a reflective load-time weaver for class loader: " +
					this.loadTimeWeaver.getInstrumentableClassLoader().getClass().getName());
		}
		catch (IllegalStateException ex) {
			throw new IllegalStateException(ex.getMessage() + " Specify a custom LoadTimeWeaver or start your " +
					"Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar");
		}
	}
}
 
源代码2 项目: lams   文件: DefaultContextLoadTimeWeaver.java
protected LoadTimeWeaver createServerSpecificLoadTimeWeaver(ClassLoader classLoader) {
	String name = classLoader.getClass().getName();
	try {
		if (name.startsWith("weblogic")) {
			return new WebLogicLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.glassfish")) {
			return new GlassFishLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.apache.catalina")) {
			return new TomcatLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.jboss")) {
			return new JBossLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("com.ibm")) {
			return new WebSphereLoadTimeWeaver(classLoader);
		}
	}
	catch (IllegalStateException ex) {
		logger.info("Could not obtain server-specific LoadTimeWeaver: " + ex.getMessage());
	}
	return null;
}
 
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
	LoadTimeWeaver serverSpecificLoadTimeWeaver = createServerSpecificLoadTimeWeaver(classLoader);
	if (serverSpecificLoadTimeWeaver != null) {
		if (logger.isInfoEnabled()) {
			logger.info("Determined server-specific load-time weaver: " +
					serverSpecificLoadTimeWeaver.getClass().getName());
		}
		this.loadTimeWeaver = serverSpecificLoadTimeWeaver;
	}
	else if (InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) {
		logger.info("Found Spring's JVM agent for instrumentation");
		this.loadTimeWeaver = new InstrumentationLoadTimeWeaver(classLoader);
	}
	else {
		try {
			this.loadTimeWeaver = new ReflectiveLoadTimeWeaver(classLoader);
			logger.info("Using a reflective load-time weaver for class loader: " +
					this.loadTimeWeaver.getInstrumentableClassLoader().getClass().getName());
		}
		catch (IllegalStateException ex) {
			throw new IllegalStateException(ex.getMessage() + " Specify a custom LoadTimeWeaver or start your " +
					"Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar");
		}
	}
}
 
protected LoadTimeWeaver createServerSpecificLoadTimeWeaver(ClassLoader classLoader) {
	String name = classLoader.getClass().getName();
	try {
		if (name.startsWith("weblogic")) {
			return new WebLogicLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.glassfish")) {
			return new GlassFishLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.apache.catalina")) {
			return new TomcatLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.jboss")) {
			return new JBossLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("com.ibm")) {
			return new WebSphereLoadTimeWeaver(classLoader);
		}
	}
	catch (IllegalStateException ex) {
		logger.info("Could not obtain server-specific LoadTimeWeaver: " + ex.getMessage());
	}
	return null;
}
 
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
	LoadTimeWeaver serverSpecificLoadTimeWeaver = createServerSpecificLoadTimeWeaver(classLoader);
	if (serverSpecificLoadTimeWeaver != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Determined server-specific load-time weaver: " +
					serverSpecificLoadTimeWeaver.getClass().getName());
		}
		this.loadTimeWeaver = serverSpecificLoadTimeWeaver;
	}
	else if (InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) {
		logger.debug("Found Spring's JVM agent for instrumentation");
		this.loadTimeWeaver = new InstrumentationLoadTimeWeaver(classLoader);
	}
	else {
		try {
			this.loadTimeWeaver = new ReflectiveLoadTimeWeaver(classLoader);
			if (logger.isDebugEnabled()) {
				logger.debug("Using reflective load-time weaver for class loader: " +
						this.loadTimeWeaver.getInstrumentableClassLoader().getClass().getName());
			}
		}
		catch (IllegalStateException ex) {
			throw new IllegalStateException(ex.getMessage() + " Specify a custom LoadTimeWeaver or start your " +
					"Java virtual machine with Spring's agent: -javaagent:spring-instrument-{version}.jar");
		}
	}
}
 
@Nullable
protected LoadTimeWeaver createServerSpecificLoadTimeWeaver(ClassLoader classLoader) {
	String name = classLoader.getClass().getName();
	try {
		if (name.startsWith("org.apache.catalina")) {
			return new TomcatLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.glassfish")) {
			return new GlassFishLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.jboss.modules")) {
			return new JBossLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("com.ibm.ws.classloader")) {
			return new WebSphereLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("weblogic")) {
			return new WebLogicLoadTimeWeaver(classLoader);
		}
	}
	catch (Exception ex) {
		if (logger.isInfoEnabled()) {
			logger.info("Could not obtain server-specific LoadTimeWeaver: " + ex.getMessage());
		}
	}
	return null;
}
 
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	if (bean instanceof LoadTimeWeaverAware) {
		LoadTimeWeaver ltw = this.loadTimeWeaver;
		if (ltw == null) {
			Assert.state(this.beanFactory != null,
					"BeanFactory required if no LoadTimeWeaver explicitly specified");
			ltw = this.beanFactory.getBean(
					ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, LoadTimeWeaver.class);
		}
		((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw);
	}
	return bean;
}
 
@Test
public void enableLTW_withAjWeavingDisabled() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingDisabled.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	verifyZeroInteractions(loadTimeWeaver);
}
 
@Test
public void enableLTW_withAjWeavingAutodetect() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingAutodetect.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	// no expectations -> a class file transformer should NOT be added
	// because no META-INF/aop.xml is present on the classpath
	verifyZeroInteractions(loadTimeWeaver);
}
 
@Test
public void enableLTW_withAjWeavingEnabled() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingEnabled.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	verify(loadTimeWeaver).addTransformer(isA(ClassFileTransformer.class));
}
 
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
	LoadTimeWeaver serverSpecificLoadTimeWeaver = createServerSpecificLoadTimeWeaver(classLoader);
	if (serverSpecificLoadTimeWeaver != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Determined server-specific load-time weaver: " +
					serverSpecificLoadTimeWeaver.getClass().getName());
		}
		this.loadTimeWeaver = serverSpecificLoadTimeWeaver;
	}
	else if (InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) {
		logger.debug("Found Spring's JVM agent for instrumentation");
		this.loadTimeWeaver = new InstrumentationLoadTimeWeaver(classLoader);
	}
	else {
		try {
			this.loadTimeWeaver = new ReflectiveLoadTimeWeaver(classLoader);
			if (logger.isDebugEnabled()) {
				logger.debug("Using reflective load-time weaver for class loader: " +
						this.loadTimeWeaver.getInstrumentableClassLoader().getClass().getName());
			}
		}
		catch (IllegalStateException ex) {
			throw new IllegalStateException(ex.getMessage() + " Specify a custom LoadTimeWeaver or start your " +
					"Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar");
		}
	}
}
 
@Nullable
protected LoadTimeWeaver createServerSpecificLoadTimeWeaver(ClassLoader classLoader) {
	String name = classLoader.getClass().getName();
	try {
		if (name.startsWith("org.apache.catalina")) {
			return new TomcatLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.glassfish")) {
			return new GlassFishLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("org.jboss.modules")) {
			return new JBossLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("com.ibm.ws.classloader")) {
			return new WebSphereLoadTimeWeaver(classLoader);
		}
		else if (name.startsWith("weblogic")) {
			return new WebLogicLoadTimeWeaver(classLoader);
		}
	}
	catch (Exception ex) {
		if (logger.isInfoEnabled()) {
			logger.info("Could not obtain server-specific LoadTimeWeaver: " + ex.getMessage());
		}
	}
	return null;
}
 
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	if (bean instanceof LoadTimeWeaverAware) {
		LoadTimeWeaver ltw = this.loadTimeWeaver;
		if (ltw == null) {
			Assert.state(this.beanFactory != null,
					"BeanFactory required if no LoadTimeWeaver explicitly specified");
			ltw = this.beanFactory.getBean(
					ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, LoadTimeWeaver.class);
		}
		((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw);
	}
	return bean;
}
 
@Test
public void enableLTW_withAjWeavingDisabled() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingDisabled.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	verifyZeroInteractions(loadTimeWeaver);
}
 
@Test
public void enableLTW_withAjWeavingAutodetect() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingAutodetect.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	// no expectations -> a class file transformer should NOT be added
	// because no META-INF/aop.xml is present on the classpath
	verifyZeroInteractions(loadTimeWeaver);
}
 
@Test
public void enableLTW_withAjWeavingEnabled() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingEnabled.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	verify(loadTimeWeaver).addTransformer(isA(ClassFileTransformer.class));
}
 
源代码17 项目: lams   文件: LoadTimeWeaverAwareProcessor.java
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	if (bean instanceof LoadTimeWeaverAware) {
		LoadTimeWeaver ltw = this.loadTimeWeaver;
		if (ltw == null) {
			Assert.state(this.beanFactory != null,
					"BeanFactory required if no LoadTimeWeaver explicitly specified");
			ltw = this.beanFactory.getBean(
					ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, LoadTimeWeaver.class);
		}
		((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw);
	}
	return bean;
}
 
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	if (bean instanceof LoadTimeWeaverAware) {
		LoadTimeWeaver ltw = this.loadTimeWeaver;
		if (ltw == null) {
			Assert.state(this.beanFactory != null,
					"BeanFactory required if no LoadTimeWeaver explicitly specified");
			ltw = this.beanFactory.getBean(
					ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, LoadTimeWeaver.class);
		}
		((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw);
	}
	return bean;
}
 
@Test
public void enableLTW_withAjWeavingDisabled() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingDisabled.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	verifyZeroInteractions(loadTimeWeaver);
}
 
@Test
public void enableLTW_withAjWeavingAutodetect() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingAutodetect.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	// no expectations -> a class file transformer should NOT be added
	// because no META-INF/aop.xml is present on the classpath
	verifyZeroInteractions(loadTimeWeaver);
}
 
@Test
public void enableLTW_withAjWeavingEnabled() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EnableLTWConfig_withAjWeavingEnabled.class);
	ctx.refresh();
	LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
	verify(loadTimeWeaver).addTransformer(isA(ClassFileTransformer.class));
}
 
@Override
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
	this.loadTimeWeaver = loadTimeWeaver;
}
 
@Test
public void control() {
	GenericXmlApplicationContext ctx =
		new GenericXmlApplicationContext(getClass(), "EnableLoadTimeWeavingTests-context.xml");
	ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
}
 
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
	return mock(LoadTimeWeaver.class);
}
 
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
	return mock(LoadTimeWeaver.class);
}
 
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
	return mock(LoadTimeWeaver.class);
}
 
/**
 * Return the Spring LoadTimeWeaver to use for class instrumentation according
 * to the JPA class transformer contract.
 */
@Nullable
public LoadTimeWeaver getLoadTimeWeaver() {
	return this.loadTimeWeaver;
}
 
/**
 * Initialize this PersistenceUnitInfo with the LoadTimeWeaver SPI interface
 * used by Spring to add instrumentation to the current class loader.
 */
public void init(LoadTimeWeaver loadTimeWeaver) {
	Assert.notNull(loadTimeWeaver, "LoadTimeWeaver must not be null");
	this.loadTimeWeaver = loadTimeWeaver;
	this.classLoader = loadTimeWeaver.getInstrumentableClassLoader();
}
 
@Override
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
	this.loadTimeWeaver = loadTimeWeaver;
}
 
@Test
public void control() {
	GenericXmlApplicationContext ctx =
		new GenericXmlApplicationContext(getClass(), "EnableLoadTimeWeavingTests-context.xml");
	ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
}