本文整理汇总了Java中org.springframework.aop.framework.ProxyFactory.addInterface方法的典型用法代码示例。如果您正苦于以下问题:Java ProxyFactory.addInterface方法的具体用法?Java ProxyFactory.addInterface怎么用?Java ProxyFactory.addInterface使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.aop.framework.ProxyFactory
的用法示例。
在下文中一共展示了ProxyFactory.addInterface方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: configureFactoryForClass
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
/**
* Based on the given class, properly instructs the ProxyFactory proxies. For additional sanity checks on the passed
* classes, check the methods below.
*
* @see #containsUnrelatedClasses(Class[])
* @see #removeParents(Class[])
*
* @param factory
* @param classes
*/
public static void configureFactoryForClass(ProxyFactory factory, Class<?>[] classes) {
if (ObjectUtils.isEmpty(classes))
return;
for (int i = 0; i < classes.length; i++) {
Class<?> clazz = classes[i];
if (clazz.isInterface()) {
factory.addInterface(clazz);
} else {
factory.setTargetClass(clazz);
factory.setProxyTargetClass(true);
}
}
}
示例2: 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());
}
示例3: testSerializableDelegatingIntroductionInterceptorSerializable
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Test
public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception {
SerializablePerson serializableTarget = new SerializablePerson();
String name = "Tony";
serializableTarget.setName("Tony");
ProxyFactory factory = new ProxyFactory(serializableTarget);
factory.addInterface(Person.class);
long time = 1000;
TimeStamped ts = new SerializableTimeStamped(time);
factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
factory.addAdvice(new SerializableNopInterceptor());
Person p = (Person) factory.getProxy();
assertEquals(name, p.getName());
assertEquals(time, ((TimeStamped) p).getTimeStamp());
Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
assertEquals(name, p1.getName());
assertEquals(time, ((TimeStamped) p1).getTimeStamp());
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:24,代码来源:DelegatingIntroductionInterceptorTests.java
示例4: 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);
}
示例5: crossClassInterfaceMethodLevelOnJdkProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Test
public void crossClassInterfaceMethodLevelOnJdkProxy() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(new SomeServiceImpl());
proxyFactory.addInterface(SomeService.class);
proxyFactory.addAdvice(this.ti);
SomeService someService = (SomeService) proxyFactory.getProxy();
someService.bar();
assertGetTransactionAndCommitCount(1);
someService.foo();
assertGetTransactionAndCommitCount(2);
someService.fooBar();
assertGetTransactionAndCommitCount(3);
}
示例6: 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
示例7: 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();
}
示例8: createJndiObjectProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
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());
targetSource.setJndiName(jof.getJndiName());
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);
}
示例9: afterPropertiesSet
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@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());
}
示例10: buildLazyResolutionProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final String beanName) {
Assert.state(getBeanFactory() instanceof DefaultListableBeanFactory,
"BeanFactory needs to be a DefaultListableBeanFactory");
final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory();
TargetSource ts = new TargetSource() {
@Override
public Class<?> getTargetClass() {
return descriptor.getDependencyType();
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() {
return beanFactory.doResolveDependency(descriptor, beanName, null, null);
}
@Override
public void releaseTarget(Object target) {
}
};
ProxyFactory pf = new ProxyFactory();
pf.setTargetSource(ts);
Class<?> dependencyType = descriptor.getDependencyType();
if (dependencyType.isInterface()) {
pf.addInterface(dependencyType);
}
return pf.getProxy(beanFactory.getBeanClassLoader());
}
示例11: setBeanFactory
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof ConfigurableBeanFactory)) {
throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
}
ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
this.scopedTargetSource.setBeanFactory(beanFactory);
ProxyFactory pf = new ProxyFactory();
pf.copyFrom(this);
pf.setTargetSource(this.scopedTargetSource);
Class<?> beanType = beanFactory.getType(this.targetBeanName);
if (beanType == null) {
throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName +
"': Target type could not be determined at the time of proxy creation.");
}
if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) {
pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader()));
}
// Add an introduction that implements only the methods on ScopedObject.
ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName());
pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));
// Add the AopInfrastructureBean marker to indicate that the scoped proxy
// itself is not subject to auto-proxying! Only its target bean is.
pf.addInterface(AopInfrastructureBean.class);
this.proxy = pf.getProxy(cbf.getBeanClassLoader());
}
示例12: buildLazyResourceProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
/**
* Obtain a lazily resolving resource proxy for the given name and type,
* delegating to {@link #getResource} on demand once a method call comes in.
* @param element the descriptor for the annotated field/method
* @param requestingBeanName the name of the requesting bean
* @return the resource object (never {@code null})
* @since 4.2
* @see #getResource
* @see Lazy
*/
protected Object buildLazyResourceProxy(final LookupElement element, final String requestingBeanName) {
TargetSource ts = new TargetSource() {
@Override
public Class<?> getTargetClass() {
return element.lookupType;
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() {
return getResource(element, requestingBeanName);
}
@Override
public void releaseTarget(Object target) {
}
};
ProxyFactory pf = new ProxyFactory();
pf.setTargetSource(ts);
if (element.lookupType.isInterface()) {
pf.addInterface(element.lookupType);
}
ClassLoader classLoader = (this.beanFactory instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() : null);
return pf.getProxy(classLoader);
}
示例13: invokeListenerInvalidProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Test
public void invokeListenerInvalidProxy() {
Object target = new InvalidProxyTestBean();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.addInterface(SimpleService.class);
Object bean = proxyFactory.getProxy(getClass().getClassLoader());
Method method = ReflectionUtils.findMethod(InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class);
StaticApplicationListenerMethodAdapter listener =
new StaticApplicationListenerMethodAdapter(method, bean);
thrown.expect(IllegalStateException.class);
thrown.expectMessage("handleIt2");
listener.onApplicationEvent(createGenericTestEvent("test"));
}
示例14: testAopJdkProxy
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Test
public void testAopJdkProxy() throws Exception {
ModelMap map = new ModelMap();
ProxyFactory factory = new ProxyFactory();
Map<?, ?> target = new HashMap<Object, Object>();
factory.setTarget(target);
factory.addInterface(Map.class);
Object proxy = factory.getProxy();
map.addAttribute(proxy);
assertSame(proxy, map.get("map"));
}
示例15: testAopJdkProxyWithMultipleInterfaces
import org.springframework.aop.framework.ProxyFactory; //导入方法依赖的package包/类
@Test
public void testAopJdkProxyWithMultipleInterfaces() throws Exception {
ModelMap map = new ModelMap();
Map<?, ?> target = new HashMap<Object, Object>();
ProxyFactory factory = new ProxyFactory();
factory.setTarget(target);
factory.addInterface(Serializable.class);
factory.addInterface(Cloneable.class);
factory.addInterface(Comparable.class);
factory.addInterface(Map.class);
Object proxy = factory.getProxy();
map.addAttribute(proxy);
assertSame(proxy, map.get("map"));
}