本文整理汇总了Java中org.springframework.aop.framework.ProxyFactory.addAdvice方法的典型用法代码示例。如果您正苦于以下问题:Java ProxyFactory.addAdvice方法的具体用法?Java ProxyFactory.addAdvice怎么用?Java ProxyFactory.addAdvice使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.aop.framework.ProxyFactory
的用法示例。
在下文中一共展示了ProxyFactory.addAdvice方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDisposalLockProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
/**
* Apply a lock (preferably a read lock allowing multiple concurrent access) to the bean. Callers should replace the
* bean input with the output.
*
* @param bean the bean to lock
* @param lock the lock to apply
* @return a proxy that locks while its methods are executed
*/
private Object getDisposalLockProxy(Object bean, final Lock lock) {
ProxyFactory factory = new ProxyFactory(bean);
factory.setProxyTargetClass(proxyTargetClass);
factory.addAdvice(new MethodInterceptor() {
public Object invoke(MethodInvocation invocation) throws Throwable {
lock.lock();
try {
return invocation.proceed();
} finally {
lock.unlock();
}
}
});
return factory.getProxy();
}
示例2: createRefreshableProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
/**
* Create a refreshable proxy for the given AOP TargetSource.
* @param ts the refreshable TargetSource
* @param interfaces the proxy interfaces (may be {@code null} to
* indicate proxying of all interfaces implemented by the target class)
* @return the generated proxy
* @see RefreshableScriptTargetSource
*/
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass) {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTargetSource(ts);
ClassLoader classLoader = this.beanClassLoader;
if (interfaces == null) {
interfaces = ClassUtils.getAllInterfacesForClass(ts.getTargetClass(), this.beanClassLoader);
}
proxyFactory.setInterfaces(interfaces);
if (proxyTargetClass) {
classLoader = null; // force use of Class.getClassLoader()
proxyFactory.setProxyTargetClass(proxyTargetClass);
}
DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
introduction.suppressInterface(TargetSource.class);
proxyFactory.addAdvice(introduction);
return proxyFactory.getProxy(classLoader);
}
示例3: getProxyForService
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
/**
* 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.booleanValue() : this.interceptors == null) {
proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName()));
}
if (this.interceptors != null) {
AdvisorAdapterRegistry adapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();
for (int i = 0; i < this.interceptors.length; i++) {
proxyFactory.addAdvisor(adapterRegistry.wrap(this.interceptors[i]));
}
}
proxyFactory.setTarget(getService());
proxyFactory.setOpaque(true);
return proxyFactory.getProxy(getBeanClassLoader());
}
示例4: createProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) {
ProxyFactory pf = new ProxyFactory(target);
if (interfaces.length > 1 || interfaces[0].isInterface()) {
pf.setInterfaces(interfaces);
}
else {
pf.setProxyTargetClass(true);
}
// Required everywhere we use AspectJ proxies
pf.addAdvice(ExposeInvocationInterceptor.INSTANCE);
for (Object a : advisors) {
pf.addAdvisor((Advisor) a);
}
pf.setExposeProxy(true);
return pf.getProxy();
}
示例5: serializable
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@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(new Class[] {ITestBean.class});
proxyFactory.addAdvice(ti);
proxyFactory.setTarget(tb);
ITestBean proxy = (ITestBean) proxyFactory.getProxy();
proxy.getAge();
assertEquals(1, ptm.commits);
ITestBean serializedProxy = (ITestBean) 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);
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:24,代码来源:AnnotationTransactionAttributeSourceTests.java
示例6: testDelegateReturnsThisIsMassagedToReturnProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Test
public void testDelegateReturnsThisIsMassagedToReturnProxy() {
NestedTestBean target = new NestedTestBean();
String company = "Interface21";
target.setCompany(company);
TestBean delegate = new TestBean() {
@Override
public ITestBean getSpouse() {
return this;
}
};
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new DelegatingIntroductionInterceptor(delegate));
INestedTestBean proxy = (INestedTestBean) pf.getProxy();
assertEquals(company, proxy.getCompany());
ITestBean introduction = (ITestBean) proxy;
assertSame("Introduced method returning delegate returns proxy", introduction, introduction.getSpouse());
assertTrue("Introduced method returning delegate returns proxy", AopUtils.isAopProxy(introduction.getSpouse()));
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:DelegatingIntroductionInterceptorTests.java
示例7: testOpenSessionInterceptor
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Test
public void testOpenSessionInterceptor() throws Exception {
final SessionFactory sf = mock(SessionFactory.class);
final Session session = mock(Session.class);
OpenSessionInterceptor interceptor = new OpenSessionInterceptor();
interceptor.setSessionFactory(sf);
Runnable tb = new Runnable() {
@Override
public void run() {
assertTrue(TransactionSynchronizationManager.hasResource(sf));
assertEquals(session, SessionFactoryUtils.getSession(sf, false));
}
};
ProxyFactory pf = new ProxyFactory(tb);
pf.addAdvice(interceptor);
Runnable tbProxy = (Runnable) pf.getProxy();
given(sf.openSession()).willReturn(session);
given(session.isOpen()).willReturn(true);
tbProxy.run();
verify(session).setFlushMode(FlushMode.MANUAL);
verify(session).close();
}
示例8: createRefreshableProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
/**
* Create a refreshable proxy for the given AOP TargetSource.
* @param ts the refreshable TargetSource
* @param interfaces the proxy interfaces (may be {@code null} to
* indicate proxying of all interfaces implemented by the target class)
* @return the generated proxy
* @see RefreshableScriptTargetSource
*/
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass) {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTargetSource(ts);
ClassLoader classLoader = this.beanClassLoader;
if (interfaces == null) {
interfaces = ClassUtils.getAllInterfacesForClass(ts.getTargetClass(), this.beanClassLoader);
}
proxyFactory.setInterfaces(interfaces);
if (proxyTargetClass) {
classLoader = null; // force use of Class.getClassLoader()
proxyFactory.setProxyTargetClass(true);
}
DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
introduction.suppressInterface(TargetSource.class);
proxyFactory.addAdvice(introduction);
return proxyFactory.getProxy(classLoader);
}
示例9: withInterface
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@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);
}
示例10: testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@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();
}
示例11: postProcessAfterInitialization
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@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;
}
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:13,代码来源:OAuth2SsoCustomConfiguration.java
示例12: postProcessAfterInitialization
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DataSource && !getDataSourceDecoratorProperties().getExcludeBeans().contains(beanName)) {
DataSource dataSource = (DataSource) bean;
DataSource decoratedDataSource = dataSource;
Map<String, DataSourceDecorator> decorators = new LinkedHashMap<>();
applicationContext.getBeansOfType(DataSourceDecorator.class)
.entrySet()
.stream()
.sorted(Entry.comparingByValue(AnnotationAwareOrderComparator.INSTANCE))
.forEach(entry -> decorators.put(entry.getKey(), entry.getValue()));
List<DataSourceDecorationStage> decoratedDataSourceChainEntries = new ArrayList<>();
for (Entry<String, DataSourceDecorator> decoratorEntry : decorators.entrySet()) {
String decoratorBeanName = decoratorEntry.getKey();
DataSourceDecorator decorator = decoratorEntry.getValue();
DataSource dataSourceBeforeDecorating = decoratedDataSource;
decoratedDataSource = Objects.requireNonNull(decorator.decorate(beanName, decoratedDataSource),
"DataSourceDecorator (" + decoratorBeanName + ", " + decorator + ") should not return null");
if (dataSourceBeforeDecorating != decoratedDataSource) {
decoratedDataSourceChainEntries.add(0, new DataSourceDecorationStage(decoratorBeanName, decorator, decoratedDataSource));
}
}
if (dataSource != decoratedDataSource) {
ProxyFactory factory = new ProxyFactory(bean);
factory.setProxyTargetClass(true);
factory.addInterface(DecoratedDataSource.class);
factory.addAdvice(new DataSourceDecoratorInterceptor(beanName, dataSource, decoratedDataSource, decoratedDataSourceChainEntries));
return factory.getProxy();
}
}
return bean;
}
开发者ID:gavlyukovskiy,项目名称:spring-boot-data-source-decorator,代码行数:35,代码来源:DataSourceDecoratorBeanPostProcessor.java
示例13: createProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
private Object createProxy(final Class<?> clazz, Advice cardinalityInterceptor) {
ProxyFactory factory = new ProxyFactory();
factory.setProxyTargetClass(true);
factory.setOptimize(true);
factory.setTargetClass(clazz);
factory.addAdvice(cardinalityInterceptor);
factory.setFrozen(true);
return factory.getProxy(ProxyFactory.class.getClassLoader());
}
示例14: createProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
public static Object createProxy(Class<?>[] classes, Object target, final ClassLoader classLoader,
BundleContext bundleContext, Advice[] advices) {
final ProxyFactory factory = new ProxyFactory();
ClassUtils.configureFactoryForClass(factory, classes);
for (int i = 0; i < advices.length; i++) {
factory.addAdvice(advices[i]);
}
if (target != null)
factory.setTarget(target);
// no need to add optimize since it means implicit usage of CGLib always
// which is determined automatically anyway
// factory.setOptimize(true);
factory.setFrozen(true);
factory.setOpaque(true);
boolean isSecurityOn = (System.getSecurityManager() != null);
try {
if (isSecurityOn) {
return AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return factory.getProxy(classLoader);
}
});
} else {
return factory.getProxy(classLoader);
}
} catch (NoClassDefFoundError ncdfe) {
DebugUtils.debugClassLoadingThrowable(ncdfe, bundleContext.getBundle(), classes);
throw ncdfe;
}
}
示例15: createProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
private Object createProxy(Object target, Class<?> intf, Advice[] advices) {
ProxyFactory factory = new ProxyFactory();
factory.addInterface(intf);
if (advices != null)
for (int i = 0; i < advices.length; i++) {
factory.addAdvice(advices[0]);
}
factory.setTarget(target);
return factory.getProxy();
}