下面列出了org.springframework.context.annotation4.FactoryMethodComponent#org.springframework.aop.scope.ScopedObject 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void interceptorWithPlainObject() throws Exception {
final Object realObject = new Object();
ScopedObject scoped = new ScopedObject() {
@Override
public Object getTargetObject() {
return realObject;
}
@Override
public void removeFromScope() {
// do nothing
}
};
// default contract is to return null for default behavior
assertNull(interceptor.getEntityName(realObject));
assertEquals(realObject.getClass().getName(), interceptor.getEntityName(scoped));
}
@Test
public void interceptorWithCglibProxy() throws Exception {
final Object realObject = new Object();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(realObject);
proxyFactory.setProxyTargetClass(true);
final Object proxy = proxyFactory.getProxy();
ScopedObject scoped = new ScopedObject() {
@Override
public Object getTargetObject() {
return proxy;
}
@Override
public void removeFromScope() {
// do nothing
}
};
assertEquals(realObject.getClass().getName(), interceptor.getEntityName(scoped));
}
@Test
public void testThreadScope() throws Exception {
// verify that set/get works in normal case
threadedService.setValue(VALUE_FIRST);
Assert.assertEquals(VALUE_FIRST, threadedService.getValue());
// assure bean is not treated as prototype
ThreadedService ts = applicationContext.getBean(BEAN_NAME, ThreadedService.class);
Assert.assertEquals(VALUE_FIRST, ts.getValue());
Thread thread = new Thread(new Runnable() {
public void run() {
// we are in the thread, now create the autowired class and test:
testInOtherThread();
}
});
thread.start();
thread.join();
// now verify that we can clear the thread data
Assert.assertEquals(VALUE_FIRST, threadedService.getValue());
((ScopedObject)threadedService).removeFromScope();
Assert.assertNull(threadedService.getValue());
}
@Test
public void scopedProxyTargetMarkedAsNonAutowireCandidate() {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(beanFactory);
beanFactory.addBeanPostProcessor(bpp);
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ScopedProxyConfigurationClass.class));
beanFactory.registerBeanDefinition("consumer", new RootBeanDefinition(ScopedProxyConsumer.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory);
ITestBean injected = beanFactory.getBean("consumer", ScopedProxyConsumer.class).testBean;
assertTrue(injected instanceof ScopedObject);
assertSame(beanFactory.getBean("scopedClass"), injected);
assertSame(beanFactory.getBean(ITestBean.class), injected);
}
@Test
public void testRawScopes() throws Exception {
String beanName = "scopedProxyInterface";
// get hidden bean
Object bean = ctx.getBean("scopedTarget." + beanName);
assertFalse(bean instanceof ScopedObject);
}
public static Object unwrapIfNecessary(Object resource) {
if (resource instanceof ScopedObject) {
return ((ScopedObject) resource).getTargetObject();
}
else {
return resource;
}
}
public static Object unwrapIfNecessary(Object resource) {
if (resource instanceof ScopedObject) {
return ((ScopedObject) resource).getTargetObject();
}
else {
return resource;
}
}
@Test
public void scopedProxyTargetMarkedAsNonAutowireCandidate() {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(beanFactory);
beanFactory.addBeanPostProcessor(bpp);
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ScopedProxyConfigurationClass.class));
beanFactory.registerBeanDefinition("consumer", new RootBeanDefinition(ScopedProxyConsumer.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory);
ITestBean injected = beanFactory.getBean("consumer", ScopedProxyConsumer.class).testBean;
assertTrue(injected instanceof ScopedObject);
assertSame(beanFactory.getBean("scopedClass"), injected);
assertSame(beanFactory.getBean(ITestBean.class), injected);
}
@Test
public void testRawScopes() throws Exception {
String beanName = "scopedProxyInterface";
// get hidden bean
Object bean = ctx.getBean("scopedTarget." + beanName);
assertFalse(bean instanceof ScopedObject);
}
public static Object unwrapIfNecessary(Object resource) {
if (resource instanceof ScopedObject) {
return ((ScopedObject) resource).getTargetObject();
}
else {
return resource;
}
}
public static Object unwrapIfNecessary(Object resource) {
if (resource instanceof ScopedObject) {
return ((ScopedObject) resource).getTargetObject();
}
else {
return resource;
}
}
@Override
public String getEntityName(Object entity) {
if (entity instanceof ScopedObject) {
// Determine underlying object's type.
Object targetObject = ((ScopedObject) entity).getTargetObject();
return AopUtils.getTargetClass(targetObject).getName();
}
// Any other object: delegate to the default implementation.
return super.getEntityName(entity);
}
@Test
public void scopedProxyTargetMarkedAsNonAutowireCandidate() {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(beanFactory);
beanFactory.addBeanPostProcessor(bpp);
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ScopedProxyConfigurationClass.class));
beanFactory.registerBeanDefinition("consumer", new RootBeanDefinition(ScopedProxyConsumer.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory);
ITestBean injected = beanFactory.getBean("consumer", ScopedProxyConsumer.class).testBean;
assertTrue(injected instanceof ScopedObject);
assertSame(beanFactory.getBean("scopedClass"), injected);
assertSame(beanFactory.getBean(ITestBean.class), injected);
}
@Test
public void testRawScopes() throws Exception {
String beanName = "scopedProxyInterface";
// get hidden bean
Object bean = ctx.getBean("scopedTarget." + beanName);
assertFalse(bean instanceof ScopedObject);
}
@Override
public String getEntityName(Object entity) {
if (entity instanceof ScopedObject) {
// Determine underlying object's type.
Object targetObject = ((ScopedObject) entity).getTargetObject();
return AopUtils.getTargetClass(targetObject).getName();
}
// Any other object: delegate to the default implementation.
return super.getEntityName(entity);
}
public static Object unwrapIfNecessary(Object resource) {
if (resource instanceof ScopedObject) {
return ((ScopedObject) resource).getTargetObject();
}
else {
return resource;
}
}
private boolean isScopedObjectGetTargetObject(Method method) {
return method.getDeclaringClass().equals(ScopedObject.class)
&& method.getName().equals("getTargetObject")
&& method.getParameterTypes().length == 0;
}