use of org.springframework.aop.target.SingletonTargetSource in project spring-framework by spring-projects.
the class AbstractAutoProxyCreator method wrapIfNecessary.
/**
* Wrap the given bean if necessary, i.e. if it is eligible for being proxied.
* @param bean the raw bean instance
* @param beanName the name of the bean
* @param cacheKey the cache key for metadata access
* @return a proxy wrapping the bean, or the raw bean instance as-is
*/
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (beanName != null && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
use of org.springframework.aop.target.SingletonTargetSource in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithNonGenericConverter.
// SPR-11225
@Test
public void resolveArgumentTypeVariableWithNonGenericConverter() throws Exception {
Method method = MyParameterizedController.class.getMethod("handleDto", Identifiable.class);
HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method);
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
HttpMessageConverter target = new MappingJackson2HttpMessageConverter();
HttpMessageConverter proxy = ProxyFactory.getProxy(HttpMessageConverter.class, new SingletonTargetSource(target));
converters.add(proxy);
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
SimpleBean result = (SimpleBean) processor.resolveArgument(methodParam, container, request, factory);
assertNotNull(result);
assertEquals("Jad", result.getName());
}
use of org.springframework.aop.target.SingletonTargetSource in project spring-framework by spring-projects.
the class AopProxyUtils method ultimateTargetClass.
/**
* Determine the ultimate target class of the given bean instance, traversing
* not only a top-level proxy but any number of nested proxies as well —
* as long as possible without side effects, that is, just for singleton targets.
* @param candidate the instance to check (might be an AOP proxy)
* @return the ultimate target class (or the plain class of the given
* object as fallback; never {@code null})
* @see org.springframework.aop.TargetClassAware#getTargetClass()
* @see Advised#getTargetSource()
*/
public static Class<?> ultimateTargetClass(Object candidate) {
Assert.notNull(candidate, "Candidate object must not be null");
Object current = candidate;
Class<?> result = null;
while (current instanceof TargetClassAware) {
result = ((TargetClassAware) current).getTargetClass();
Object nested = null;
if (current instanceof Advised) {
TargetSource targetSource = ((Advised) current).getTargetSource();
if (targetSource instanceof SingletonTargetSource) {
nested = ((SingletonTargetSource) targetSource).getTarget();
}
}
current = nested;
}
if (result == null) {
result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
}
return result;
}
use of org.springframework.aop.target.SingletonTargetSource in project spring-framework by spring-projects.
the class HibernateEntityManagerFactoryIntegrationTests method testCanUnwrapAopProxy.
@Test
public void testCanUnwrapAopProxy() {
EntityManager em = entityManagerFactory.createEntityManager();
EntityManager proxy = ProxyFactory.getProxy(EntityManager.class, new SingletonTargetSource(em));
assertTrue(em instanceof org.hibernate.jpa.HibernateEntityManager);
assertFalse(proxy instanceof org.hibernate.jpa.HibernateEntityManager);
assertTrue(proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class) != null);
assertSame(em, proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class));
assertSame(em.getDelegate(), proxy.getDelegate());
}
use of org.springframework.aop.target.SingletonTargetSource in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testExistingProxyChangesTarget.
@Test
public void testExistingProxyChangesTarget() throws Throwable {
TestBean tb1 = new TestBean();
tb1.setAge(33);
TestBean tb2 = new TestBean();
tb2.setAge(26);
tb2.setName("Juergen");
TestBean tb3 = new TestBean();
tb3.setAge(37);
ProxyFactory pc = new ProxyFactory(tb1);
NopInterceptor nop = new NopInterceptor();
pc.addAdvice(nop);
ITestBean proxy = (ITestBean) createProxy(pc);
assertEquals(nop.getCount(), 0);
assertEquals(tb1.getAge(), proxy.getAge());
assertEquals(nop.getCount(), 1);
// Change to a new static target
pc.setTarget(tb2);
assertEquals(tb2.getAge(), proxy.getAge());
assertEquals(nop.getCount(), 2);
// Change to a new dynamic target
HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
pc.setTargetSource(hts);
assertEquals(tb3.getAge(), proxy.getAge());
assertEquals(nop.getCount(), 3);
hts.swap(tb1);
assertEquals(tb1.getAge(), proxy.getAge());
tb1.setName("Colin");
assertEquals(tb1.getName(), proxy.getName());
assertEquals(nop.getCount(), 5);
// Change back, relying on casting to Advised
Advised advised = (Advised) proxy;
assertSame(hts, advised.getTargetSource());
SingletonTargetSource sts = new SingletonTargetSource(tb2);
advised.setTargetSource(sts);
assertEquals(tb2.getName(), proxy.getName());
assertSame(sts, advised.getTargetSource());
assertEquals(tb2.getAge(), proxy.getAge());
}
Aggregations