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);
}
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();
}
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();
}
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;
}
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;
}
Aggregations