Search in sources :

Example 1 with SingletonTargetSource

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;
}
Also used : SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource)

Example 2 with SingletonTargetSource

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());
}
Also used : SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 3 with SingletonTargetSource

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 &mdash;
	 * 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;
}
Also used : SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) TargetSource(org.springframework.aop.TargetSource) TargetClassAware(org.springframework.aop.TargetClassAware)

Example 4 with SingletonTargetSource

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());
}
Also used : SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 5 with SingletonTargetSource

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());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) Test(org.junit.Test)

Aggregations

SingletonTargetSource (org.springframework.aop.target.SingletonTargetSource)5 Test (org.junit.Test)3 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 EntityManager (javax.persistence.EntityManager)1 TargetClassAware (org.springframework.aop.TargetClassAware)1 TargetSource (org.springframework.aop.TargetSource)1 HotSwappableTargetSource (org.springframework.aop.target.HotSwappableTargetSource)1 MethodParameter (org.springframework.core.MethodParameter)1 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)1 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)1 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)1 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)1 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)1 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)1 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1