use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class ControlFlowPointcutTests method testSelectiveApplication.
/**
* Check that we can use a cflow pointcut only in conjunction with
* a static pointcut: e.g. all setter methods that are invoked under
* a particular class. This greatly reduces the number of calls
* to the cflow pointcut, meaning that it's not so prohibitively
* expensive.
*/
@Test
public void testSelectiveApplication() {
TestBean target = new TestBean();
target.setAge(27);
NopInterceptor nop = new NopInterceptor();
ControlFlowPointcut cflow = new ControlFlowPointcut(One.class);
Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow);
ProxyFactory pf = new ProxyFactory(target);
ITestBean proxied = (ITestBean) pf.getProxy();
pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));
// Not advised, not under One
target.setAge(16);
assertEquals(0, nop.getCount());
// Not advised; under One but not a setter
assertEquals(16, new One().getAge(proxied));
assertEquals(0, nop.getCount());
// Won't be advised
new One().set(proxied);
assertEquals(1, nop.getCount());
// We saved most evaluations
assertEquals(1, cflow.getEvaluations());
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInDelegate.
@Test
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
final long t = 1001L;
class Tester implements TimeStamped, ITester {
@Override
public void foo() throws Exception {
}
@Override
public long getTimeStamp() {
return t;
}
}
DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
//assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
TimeStamped ts = (TimeStamped) pf.getProxy();
assertTrue(ts.getTimeStamp() == t);
((ITester) ts).foo();
((ITestBean) ts).getAge();
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithSuperInterface.
@Test
public void testIntroductionInterceptorWithSuperInterface() throws Exception {
TestBean raw = new TestBean();
assertTrue(!(raw instanceof TimeStamped));
ProxyFactory factory = new ProxyFactory(raw);
TimeStamped ts = mock(SubTimeStamped.class);
long timestamp = 111L;
given(ts.getTimeStamp()).willReturn(timestamp);
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class));
TimeStamped tsp = (TimeStamped) factory.getProxy();
assertTrue(!(tsp instanceof SubTimeStamped));
assertTrue(tsp.getTimeStamp() == timestamp);
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithInterfaceHierarchy.
@Test
public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception {
TestBean raw = new TestBean();
assertTrue(!(raw instanceof SubTimeStamped));
ProxyFactory factory = new ProxyFactory(raw);
TimeStamped ts = mock(SubTimeStamped.class);
long timestamp = 111L;
given(ts.getTimeStamp()).willReturn(timestamp);
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class));
SubTimeStamped tsp = (SubTimeStamped) factory.getProxy();
assertTrue(tsp.getTimeStamp() == timestamp);
}
use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.
the class MBeanExporter method registerLazyInit.
/**
* Registers beans that are configured for lazy initialization with the
* {@code MBeanServer} indirectly through a proxy.
* @param beanName the name of the bean in the {@code BeanFactory}
* @param beanKey the key associated with this bean in the beans map
* @return the {@code ObjectName} under which the bean was registered
* with the {@code MBeanServer}
*/
private ObjectName registerLazyInit(String beanName, String beanKey) throws JMException {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setProxyTargetClass(true);
proxyFactory.setFrozen(true);
if (isMBean(this.beanFactory.getType(beanName))) {
// A straight MBean... Let's create a simple lazy-init CGLIB proxy for it.
LazyInitTargetSource targetSource = new LazyInitTargetSource();
targetSource.setTargetBeanName(beanName);
targetSource.setBeanFactory(this.beanFactory);
proxyFactory.setTargetSource(targetSource);
Object proxy = proxyFactory.getProxy(this.beanClassLoader);
ObjectName objectName = getObjectName(proxy, beanKey);
if (logger.isDebugEnabled()) {
logger.debug("Located MBean '" + beanKey + "': registering with JMX server as lazy-init MBean [" + objectName + "]");
}
doRegister(proxy, objectName);
return objectName;
} else {
// A simple bean... Let's create a lazy-init ModelMBean proxy with notification support.
NotificationPublisherAwareLazyTargetSource targetSource = new NotificationPublisherAwareLazyTargetSource();
targetSource.setTargetBeanName(beanName);
targetSource.setBeanFactory(this.beanFactory);
proxyFactory.setTargetSource(targetSource);
Object proxy = proxyFactory.getProxy(this.beanClassLoader);
ObjectName objectName = getObjectName(proxy, beanKey);
if (logger.isDebugEnabled()) {
logger.debug("Located simple bean '" + beanKey + "': registering with JMX server as lazy-init MBean [" + objectName + "]");
}
ModelMBean mbean = createAndConfigureMBean(proxy, beanKey);
targetSource.setModelMBean(mbean);
targetSource.setObjectName(objectName);
doRegister(mbean, objectName);
return objectName;
}
}
Aggregations