Search in sources :

Example 66 with Advised

use of org.springframework.aop.framework.Advised in project spring-security by spring-projects.

the class MethodInvocationUtils method create.

/**
 * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on
 * the passed object, using the <code>args</code> to locate the method.
 * @param object the object that will be used to find the relevant <code>Method</code>
 * @param methodName the name of the method to find
 * @param args arguments that are required as part of the method signature (can be
 * empty)
 * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a
 * problem
 */
public static MethodInvocation create(Object object, String methodName, Object... args) {
    Assert.notNull(object, "Object required");
    Class<?>[] classArgs = null;
    if (args != null) {
        classArgs = new Class<?>[args.length];
        for (int i = 0; i < args.length; i++) {
            classArgs[i] = args[i].getClass();
        }
    }
    // Determine the type that declares the requested method,
    // taking into account proxies
    Class<?> target = AopUtils.getTargetClass(object);
    if (object instanceof Advised) {
        Advised a = (Advised) object;
        if (!a.isProxyTargetClass()) {
            Class<?>[] possibleInterfaces = a.getProxiedInterfaces();
            for (Class<?> possibleInterface : possibleInterfaces) {
                try {
                    possibleInterface.getMethod(methodName, classArgs);
                    // to get here means no exception happened
                    target = possibleInterface;
                    break;
                } catch (Exception ex) {
                // try the next one
                }
            }
        }
    }
    return createFromClass(object, target, methodName, classArgs, args);
}
Also used : Advised(org.springframework.aop.framework.Advised)

Example 67 with Advised

use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.

the class ConcurrencyThrottleInterceptorTests method testSerializable.

@Test
public void testSerializable() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    proxy.getAge();
    ITestBean serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    Advised advised = (Advised) serializedProxy;
    ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
    assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit());
    serializedProxy.getAge();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 68 with Advised

use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.

the class CreatesTestBean method withFrozenProxy.

@Test
void withFrozenProxy() {
    ITestBean testBean = (ITestBean) beanFactory.getBean("frozenBean");
    assertThat(((Advised) testBean).isFrozen()).isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advised(org.springframework.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 69 with Advised

use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.

the class SelectivePrototypeTargetSourceCreator method testCustomPrototypeTargetSource.

@Test
public void testCustomPrototypeTargetSource() throws Exception {
    CountingTestBean.count = 0;
    BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
    ITestBean test = (ITestBean) bf.getBean("prototypeTest");
    assertThat(AopUtils.isAopProxy(test)).isTrue();
    Advised advised = (Advised) test;
    boolean condition = advised.getTargetSource() instanceof PrototypeTargetSource;
    assertThat(condition).isTrue();
    assertThat(test.getName()).isEqualTo("Rod");
    // Check that references survived prototype creation
    assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
    assertThat(CountingTestBean.count).as("Only 2 CountingTestBeans instantiated").isEqualTo(2);
    CountingTestBean.count = 0;
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) BeanFactory(org.springframework.beans.factory.BeanFactory) PrototypeTargetSource(org.springframework.aop.target.PrototypeTargetSource) Test(org.junit.jupiter.api.Test)

Example 70 with Advised

use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.

the class SelectivePrototypeTargetSourceCreator method testLazyInitTargetSource.

@Test
public void testLazyInitTargetSource() throws Exception {
    CountingTestBean.count = 0;
    BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
    ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
    assertThat(AopUtils.isAopProxy(test)).isTrue();
    Advised advised = (Advised) test;
    boolean condition = advised.getTargetSource() instanceof LazyInitTargetSource;
    assertThat(condition).isTrue();
    assertThat(CountingTestBean.count).as("No CountingTestBean instantiated yet").isEqualTo(0);
    assertThat(test.getName()).isEqualTo("Rod");
    assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
    assertThat(CountingTestBean.count).as("Only 1 CountingTestBean instantiated").isEqualTo(1);
    CountingTestBean.count = 0;
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) BeanFactory(org.springframework.beans.factory.BeanFactory) LazyInitTargetSource(org.springframework.aop.target.LazyInitTargetSource) Test(org.junit.jupiter.api.Test)

Aggregations

Advised (org.springframework.aop.framework.Advised)78 Advisor (org.springframework.aop.Advisor)21 Test (org.junit.jupiter.api.Test)20 Test (org.junit.Test)18 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)16 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)13 ProxyFactory (org.springframework.aop.framework.ProxyFactory)10 Advice (org.aopalliance.aop.Advice)8 JoinPoint (org.aspectj.lang.JoinPoint)6 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)6 TargetSource (org.springframework.aop.TargetSource)6 BeanCreationException (org.springframework.beans.factory.BeanCreationException)6 MessageChannel (org.springframework.messaging.MessageChannel)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)4 ChannelAccessPolicy (org.springframework.integration.security.channel.ChannelAccessPolicy)4 ChannelSecurityInterceptor (org.springframework.integration.security.channel.ChannelSecurityInterceptor)4 ConfigAttribute (org.springframework.security.access.ConfigAttribute)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 List (java.util.List)3