Java Code Examples for org.springframework.aop.framework.ProxyFactory#addAdvice()
The following examples show how to use
org.springframework.aop.framework.ProxyFactory#addAdvice() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EncryptablePropertySourceConverter.java From jasypt-spring-boot with MIT License | 6 votes |
@SuppressWarnings("unchecked") private <T> PropertySource<T> proxyPropertySource(PropertySource<T> propertySource) { //Silly Chris Beams for making CommandLinePropertySource getProperty and containsProperty methods final. Those methods //can't be proxied with CGLib because of it. So fallback to wrapper for Command Line Arguments only. if (CommandLinePropertySource.class.isAssignableFrom(propertySource.getClass()) // Other PropertySource classes like org.springframework.boot.env.OriginTrackedMapPropertySource // are final classes as well || Modifier.isFinal(propertySource.getClass().getModifiers())) { return instantiatePropertySource(propertySource); } ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetClass(propertySource.getClass()); proxyFactory.setProxyTargetClass(true); proxyFactory.addInterface(EncryptablePropertySource.class); proxyFactory.setTarget(propertySource); proxyFactory.addAdvice(new EncryptablePropertySourceMethodInterceptor<>(propertySource, propertyResolver, propertyFilter)); return (PropertySource<T>) proxyFactory.getProxy(); }
Example 2
Source File: LocalSlsbInvokerInterceptorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testInvokesMethodOnEjbInstance() throws Exception { Object retVal = new Object(); LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class); given(ejb.targetMethod()).willReturn(retVal); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class }); pf.addAdvice(si); BusinessMethods target = (BusinessMethods) pf.getProxy(); assertTrue(target.targetMethod() == retVal); verify(mockContext).close(); verify(ejb).remove(); }
Example 3
Source File: GridifySpringEnhancer.java From ignite with Apache License 2.0 | 6 votes |
/** * Enhances the object on load. * * @param <T> Type of the object to enhance. * @param obj Object to augment/enhance. * @return Enhanced object. */ @SuppressWarnings({"unchecked"}) public static <T> T enhance(T obj) { ProxyFactory proxyFac = new ProxyFactory(obj); proxyFac.addAdvice(dfltAsp); proxyFac.addAdvice(setToValAsp); proxyFac.addAdvice(setToSetAsp); while (proxyFac.getAdvisors().length > 0) proxyFac.removeAdvisor(0); proxyFac.addAdvisor(new DefaultPointcutAdvisor( new GridifySpringPointcut(GridifySpringPointcut.GridifySpringPointcutType.DFLT), dfltAsp)); proxyFac.addAdvisor(new DefaultPointcutAdvisor( new GridifySpringPointcut(GridifySpringPointcut.GridifySpringPointcutType.SET_TO_VALUE), setToValAsp)); proxyFac.addAdvisor(new DefaultPointcutAdvisor( new GridifySpringPointcut(GridifySpringPointcut.GridifySpringPointcutType.SET_TO_SET), setToSetAsp)); return (T)proxyFac.getProxy(); }
Example 4
Source File: AnnotationTransactionAttributeSourceTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void serializable() throws Exception { TestBean1 tb = new TestBean1(); CallCountingTransactionManager ptm = new CallCountingTransactionManager(); AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource(); TransactionInterceptor ti = new TransactionInterceptor(ptm, tas); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setInterfaces(ITestBean1.class); proxyFactory.addAdvice(ti); proxyFactory.setTarget(tb); ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy(); proxy.getAge(); assertEquals(1, ptm.commits); ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy); serializedProxy.getAge(); Advised advised = (Advised) serializedProxy; TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice(); CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager(); assertEquals(2, serializedPtm.commits); }
Example 5
Source File: AnnotationTransactionAttributeSourceTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void serializable() throws Exception { TestBean1 tb = new TestBean1(); CallCountingTransactionManager ptm = new CallCountingTransactionManager(); AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource(); TransactionInterceptor ti = new TransactionInterceptor(ptm, tas); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setInterfaces(ITestBean1.class); proxyFactory.addAdvice(ti); proxyFactory.setTarget(tb); ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy(); proxy.getAge(); assertEquals(1, ptm.commits); ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy); serializedProxy.getAge(); Advised advised = (Advised) serializedProxy; TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice(); CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager(); assertEquals(2, serializedPtm.commits); }
Example 6
Source File: LocalSlsbInvokerInterceptorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception { Object retVal = new Object(); LocalInterface ejb = mock(LocalInterface.class); given(ejb.targetMethod()).willReturn(retVal); String jndiName= "foobar"; Context mockContext = mockContext(jndiName, ejb); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName); ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class } ); pf.addAdvice(si); BusinessMethods target = (BusinessMethods) pf.getProxy(); assertTrue(target.targetMethod() == retVal); verify(mockContext).close(); verify(ejb).remove(); }
Example 7
Source File: RemoteExporter.java From java-technology-stack with MIT License | 6 votes |
/** * Get a proxy for the given service object, implementing the specified * service interface. * <p>Used to export a proxy that does not expose any internals but just * a specific interface intended for remote access. Furthermore, a * {@link RemoteInvocationTraceInterceptor} will be registered (by default). * @return the proxy * @see #setServiceInterface * @see #setRegisterTraceInterceptor * @see RemoteInvocationTraceInterceptor */ protected Object getProxyForService() { checkService(); checkServiceInterface(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addInterface(getServiceInterface()); if (this.registerTraceInterceptor != null ? this.registerTraceInterceptor : this.interceptors == null) { proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName())); } if (this.interceptors != null) { AdvisorAdapterRegistry adapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); for (Object interceptor : this.interceptors) { proxyFactory.addAdvisor(adapterRegistry.wrap(interceptor)); } } proxyFactory.setTarget(getService()); proxyFactory.setOpaque(true); return proxyFactory.getProxy(getBeanClassLoader()); }
Example 8
Source File: MethodInvocationProceedingJoinPointTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testCanGetStaticPartFromJoinPoint() { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart(); assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart()); assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind()); assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature()); assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation()); } }); ITestBean itb = (ITestBean) pf.getProxy(); // Any call will do itb.getAge(); }
Example 9
Source File: AnnotationTransactionInterceptorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void withInterface() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new TestWithInterfaceImpl()); proxyFactory.addInterface(TestWithInterface.class); proxyFactory.addAdvice(this.ti); TestWithInterface proxy = (TestWithInterface) proxyFactory.getProxy(); proxy.doSomething(); assertGetTransactionAndCommitCount(1); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(2); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(3); proxy.doSomething(); assertGetTransactionAndCommitCount(4); }
Example 10
Source File: MethodInvocationProceedingJoinPointTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void toShortAndLongStringFormedCorrectly() throws Exception { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { // makeEncSJP, although meant for computing the enclosing join point, // it serves our purpose here JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method); JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString()); assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString()); assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString()); assertEquals(aspectJVersionJp.toLongString(), jp.toLongString()); assertEquals(aspectJVersionJp.toShortString(), jp.toShortString()); assertEquals(aspectJVersionJp.toString(), jp.toString()); } }); ITestBean itb = (ITestBean) pf.getProxy(); itb.getAge(); itb.setName("foo"); itb.getDoctor(); itb.getStringArray(); itb.getSpouse(); itb.setSpouse(new TestBean()); try { itb.unreliableFileOperation(); } catch (IOException ex) { // we don't really care... } }
Example 11
Source File: OAuth2SsoCustomConfiguration.java From spring-security-oauth2-boot with Apache License 2.0 | 5 votes |
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (this.configType.isAssignableFrom(bean.getClass()) && bean instanceof WebSecurityConfigurerAdapter) { ProxyFactory factory = new ProxyFactory(); factory.setTarget(bean); factory.addAdvice(new SsoSecurityAdapter(this.applicationContext)); bean = factory.getProxy(); } return bean; }
Example 12
Source File: JaxWsPortProxyFactoryBean.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); // Build a proxy that also exposes the JAX-WS BindingProvider interface. ProxyFactory pf = new ProxyFactory(); pf.addInterface(getServiceInterface()); pf.addInterface(BindingProvider.class); pf.addAdvice(this); this.serviceProxy = pf.getProxy(getBeanClassLoader()); }
Example 13
Source File: JndiObjectFactoryBean.java From java-technology-stack with MIT License | 5 votes |
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException { // Create a JndiObjectTargetSource that mirrors the JndiObjectFactoryBean's configuration. JndiObjectTargetSource targetSource = new JndiObjectTargetSource(); targetSource.setJndiTemplate(jof.getJndiTemplate()); String jndiName = jof.getJndiName(); Assert.state(jndiName != null, "No JNDI name specified"); targetSource.setJndiName(jndiName); targetSource.setExpectedType(jof.getExpectedType()); targetSource.setResourceRef(jof.isResourceRef()); targetSource.setLookupOnStartup(jof.lookupOnStartup); targetSource.setCache(jof.cache); targetSource.afterPropertiesSet(); // Create a proxy with JndiObjectFactoryBean's proxy interface and the JndiObjectTargetSource. ProxyFactory proxyFactory = new ProxyFactory(); if (jof.proxyInterfaces != null) { proxyFactory.setInterfaces(jof.proxyInterfaces); } else { Class<?> targetClass = targetSource.getTargetClass(); if (targetClass == null) { throw new IllegalStateException( "Cannot deactivate 'lookupOnStartup' without specifying a 'proxyInterface' or 'expectedType'"); } Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(targetClass, jof.beanClassLoader); for (Class<?> ifc : ifcs) { if (Modifier.isPublic(ifc.getModifiers())) { proxyFactory.addInterface(ifc); } } } if (jof.exposeAccessContext) { proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate())); } proxyFactory.setTargetSource(targetSource); return proxyFactory.getProxy(jof.beanClassLoader); }
Example 14
Source File: MethodValidationTests.java From spring-analysis-note with MIT License | 5 votes |
@Test @SuppressWarnings("unchecked") public void testMethodValidationInterceptor() { MyValidBean bean = new MyValidBean(); ProxyFactory proxyFactory = new ProxyFactory(bean); proxyFactory.addAdvice(new MethodValidationInterceptor()); proxyFactory.addAdvisor(new AsyncAnnotationAdvisor()); doTestProxyValidation((MyValidInterface<String>) proxyFactory.getProxy()); }
Example 15
Source File: GenericMessageEndpointFactory.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Wrap each concrete endpoint instance with an AOP proxy, * exposing the message listener's interfaces as well as the * endpoint SPI through an AOP introduction. */ @Override public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { GenericMessageEndpoint endpoint = (GenericMessageEndpoint) super.createEndpoint(xaResource); ProxyFactory proxyFactory = new ProxyFactory(this.messageListener); DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(endpoint); introduction.suppressInterface(MethodInterceptor.class); proxyFactory.addAdvice(introduction); return (MessageEndpoint) proxyFactory.getProxy(); }
Example 16
Source File: EventPublicationInterceptorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testExpectedBehavior() throws Exception { TestBean target = new TestBean(); final TestListener listener = new TestListener(); class TestContext extends StaticApplicationContext { @Override protected void onRefresh() throws BeansException { addApplicationListener(listener); } } StaticApplicationContext ctx = new TestContext(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("applicationEventClass", TestEvent.class.getName()); // should automatically receive applicationEventPublisher reference ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs); ctx.registerSingleton("otherListener", FactoryBeanTestListener.class); ctx.refresh(); EventPublicationInterceptor interceptor = (EventPublicationInterceptor) ctx.getBean("publisher"); ProxyFactory factory = new ProxyFactory(target); factory.addAdvice(0, interceptor); ITestBean testBean = (ITestBean) factory.getProxy(); // invoke any method on the advised proxy to see if the interceptor has been invoked testBean.getAge(); // two events: ContextRefreshedEvent and TestEvent assertTrue("Interceptor must have published 2 events", listener.getEventCount() == 2); TestListener otherListener = (TestListener) ctx.getBean("&otherListener"); assertTrue("Interceptor must have published 2 events", otherListener.getEventCount() == 2); }
Example 17
Source File: AnnotationTransactionInterceptorTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void withVavrTryCheckedException() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new TestWithVavrTry()); proxyFactory.addAdvice(this.ti); TestWithVavrTry proxy = (TestWithVavrTry) proxyFactory.getProxy(); proxy.doSomethingErroneousWithCheckedException(); assertGetTransactionAndCommitCount(1); }
Example 18
Source File: TransactionInterceptorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override protected Object advised(Object target, PlatformTransactionManager ptm, TransactionAttributeSource[] tas) throws Exception { TransactionInterceptor ti = new TransactionInterceptor(); ti.setTransactionManager(ptm); ti.setTransactionAttributeSources(tas); ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(0, ti); return pf.getProxy(); }
Example 19
Source File: DatasourceProxyBeanPostProcessor.java From tutorials with MIT License | 5 votes |
@Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { if (bean instanceof DataSource) { ProxyFactory factory = new ProxyFactory(bean); factory.setProxyTargetClass(true); factory.addAdvice(new ProxyDataSourceInterceptor((DataSource) bean)); return factory.getProxy(); } return bean; }
Example 20
Source File: AbstractMetadataAssemblerTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testWithCglibProxy() throws Exception { IJmxTestBean tb = createJmxTestBean(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(tb); pf.addAdvice(new NopInterceptor()); Object proxy = pf.getProxy(); MetadataMBeanInfoAssembler assembler = (MetadataMBeanInfoAssembler) getAssembler(); MBeanExporter exporter = new MBeanExporter(); exporter.setBeanFactory(getContext()); exporter.setAssembler(assembler); String objectName = "spring:bean=test,proxy=true"; Map<String, Object> beans = new HashMap<>(); beans.put(objectName, proxy); exporter.setBeans(beans); start(exporter); MBeanInfo inf = getServer().getMBeanInfo(ObjectNameManager.getInstance(objectName)); assertEquals("Incorrect number of operations", getExpectedOperationCount(), inf.getOperations().length); assertEquals("Incorrect number of attributes", getExpectedAttributeCount(), inf.getAttributes().length); assertTrue("Not included in autodetection", assembler.includeBean(proxy.getClass(), "some bean name")); }