本文整理汇总了Java中org.springframework.cglib.proxy.Enhancer.create方法的典型用法代码示例。如果您正苦于以下问题:Java Enhancer.create方法的具体用法?Java Enhancer.create怎么用?Java Enhancer.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.cglib.proxy.Enhancer
的用法示例。
在下文中一共展示了Enhancer.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: enhanceFactoryBean
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Create a subclass proxy that intercepts calls to getObject(), delegating to the current BeanFactory
* instead of creating a new instance. These proxies are created only when calling a FactoryBean from
* within a Bean method, allowing for proper scoping semantics even when working against the FactoryBean
* instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing,
* it will not be proxied. This too is aligned with the way XML configuration works.
*/
private Object enhanceFactoryBean(Class<?> fbClass, final ConfigurableBeanFactory beanFactory,
final String beanName) throws InstantiationException, IllegalAccessException {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(fbClass);
enhancer.setUseFactory(false);
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (method.getName().equals("getObject") && args.length == 0) {
return beanFactory.getBean(beanName);
}
return proxy.invokeSuper(obj, args);
}
});
return enhancer.create();
}
示例2: wrapWithIfaceImplementation
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
static <T> T wrapWithIfaceImplementation(final Class<T> iface, final Specification<Object> targetSpec) {
Enhancer enhancer = new Enhancer();
enhancer.setInterfaces(new Class[] { iface });
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if ("toString".equals(method.getName())) {
return iface.getSimpleName() + "[" + proxy.invoke(targetSpec, args) + "]";
}
return proxy.invoke(targetSpec, args);
}
});
return (T) enhancer.create();
}
示例3: createStudent
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public Object createStudent(Object obj){
this.object=obj;
//创建一个Enhancer对象,来实例化obj
Enhancer enhancer=new Enhancer();
//将obj设置为Enhancer的父类,则Enhancer实现了obj的功能
enhancer.setSuperclass(obj.getClass());
//调用回调函数intercept
enhancer.setCallback(this);
return enhancer.create();
}
示例4: main
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserServiceImpl.class);
enhancer.setCallback(new MyMethodInterceptor());
UserServiceImpl userService = (UserServiceImpl)enhancer.create();
userService.add();//一个是重写的add方法,一个是CGLIB$add$0方法
userService.delete(2233);
userService.update(2333);//委托类的final方法不能被代理
}
示例5: createProxyService
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
private <T extends EntityService> T createProxyService(T entityService, EntityServiceProxy entityServiceProxy) {
Enhancer enhancer = new Enhancer();
// 设置需要创建子类的类
enhancer.setSuperclass(entityService.getClass());
enhancer.setCallback(entityServiceProxy);
// 通过字节码技术动态创建子类实例
return (T) enhancer.create();
}
示例6: createProxyEntity
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private <T extends IEntity> T createProxyEntity(EntityProxy entityProxy) {
if (entityProxy != null) {
Enhancer enhancer = new Enhancer();
if (enhancer != null) {
// 设置需要创建子类的类
enhancer.setSuperclass(entityProxy.getEntity().getClass());
enhancer.setCallback(entityProxy);
// 通过字节码技术动态创建子类实例
return (T) enhancer.create();
}
}
return null;
}
示例7: createProxyClassAndInstance
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
enhancer.setInterceptDuringConstruction(false);
enhancer.setCallbacks(callbacks);
return (this.constructorArgs != null ?
enhancer.create(this.constructorArgTypes, this.constructorArgs) :
enhancer.create());
}
示例8: wrap
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static NamedParameterJdbcOperations wrap(final NamedParameterJdbcOperations jdbcOperations) {
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(NamedParameterJdbcOperations.class);
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
final Stopwatch sw = Stopwatch.createStarted();
try {
final String methodName = method.getName();
if (methodName.startsWith("query")
|| methodName.startsWith("batchUpdate")
|| methodName.startsWith("update")) {
logSqlQuery(args);
final Object result = method.invoke(jdbcOperations, args);
if (sw.elapsed(TimeUnit.SECONDS) > 5) {
LOG.warn("SQL execution took too long {}", sw);
}
return result;
}
return method.invoke(jdbcOperations, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
});
return (NamedParameterJdbcOperations) enhancer.create();
}
示例9: getIgnite
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public Ignite getIgnite(IgniteResource[] igniteProps) {
if (!initialized) {
initIgnition();
initialized = true;
}
String gridName = igniteProps == null || igniteProps.length == 0
? null
: igniteProps[0].gridName();
IgniteResource gridResource = igniteProps == null || igniteProps.length == 0
? null
: igniteProps[0];
List<IgniteHolder> configs = igniteMap.get(gridName);
Ignite ignite;
if (configs == null) {
IgniteConfiguration defaultIgnite = getDefaultIgniteConfig(gridResource);
ignite = Ignition.start(defaultIgnite);
List<IgniteHolder> holderList = new ArrayList<>();
holderList.add(new IgniteHolder(defaultIgnite, ignite));
igniteMap.put(gridName, holderList);
} else {
IgniteHolder igniteHolder = configs.get(0);
if (igniteHolder.ignite == null) {
igniteHolder.ignite = Ignition.start(igniteHolder.config);
}
ignite = igniteHolder.ignite;
}
if (props.isUseSameServerNames()) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Ignite.class);
enhancer.setCallback(new IgniteHandler(ignite));
ignite = (Ignite) enhancer.create();
}
return ignite;
}
示例10: getObject
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public ShardedJedis getObject() throws Exception {
if (jedis != null) {
return jedis;
}
Enhancer en = new Enhancer();
en.setSuperclass(ShardedJedis.class);
en.setCallbackFilter(finalizeFilter);
en.setCallbacks(new Callback[]{NoOp.INSTANCE, jedisCallback});
jedis = (ShardedJedis) en.create(new Class[]{List.class}, new Object[]{Arrays.asList(new JedisShardInfo("shardedJedisProxy"))});
return jedis;
}
示例11: getObject
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@Override
public Jedis getObject() throws Exception {
if (jedis != null) {
return jedis;
}
Enhancer en = new Enhancer();
en.setSuperclass(Jedis.class);
en.setCallbackFilter(finalizeFilter);
en.setCallbacks(new Callback[]{NoOp.INSTANCE, jedisCallback});
jedis = (Jedis) en.create(new Class[]{String.class}, new Object[]{"JedisProxy"});
return jedis;
}
示例12: createProxyObject
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T createProxyObject(Class<T> clazz, Interceptor handler) {
MethodInterceptor methodInterceptor = new ProxyObjectMethodImpl(handler);
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setCallback(methodInterceptor);
return (T) enhancer.create();
}
示例13: lazyLoad
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Create a lazy loaded class.
*
* @param superclass Class that you want to lazy load
* @param callback Callback to load the target class
* @return Enhanced class
*/
public static Object lazyLoad(Class superclass, Callback callback) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(superclass);
enhancer.setCallback(callback);
return enhancer.create();
}
示例14: instantiate
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
/**
* Create a new instance of a dynamically generated subclasses implementing the
* required lookups.
* @param ctor constructor to use. If this is {@code null}, use the
* no-arg constructor (no parameterization, or Setter Injection)
* @param args arguments to use for the constructor.
* Ignored if the ctor parameter is {@code null}.
* @return new instance of the dynamically generated class
*/
public Object instantiate(Constructor<?> ctor, Object[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.beanDefinition.getBeanClass());
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setCallbackFilter(new CallbackFilterImpl());
enhancer.setCallbacks(new Callback[] {
NoOp.INSTANCE,
new LookupOverrideMethodInterceptor(),
new ReplaceOverrideMethodInterceptor()
});
return (ctor != null ? enhancer.create(ctor.getParameterTypes(), args) : enhancer.create());
}
示例15: proxySock
import org.springframework.cglib.proxy.Enhancer; //导入方法依赖的package包/类
public static <T> T proxySock(String host, int port, Class<T> type, @Nullable String beanName) {
ThreadLocal<Triple<Socket, ObjectInputStream, ObjectOutputStream>> tt =
ThreadLocal.withInitial(() -> Triple.of(null, null, null));
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(type);
enhancer.setCallback(
(InvocationHandler) (o, method, args) -> {
Triple<Socket, ObjectInputStream, ObjectOutputStream> tsoo = tt.get();
Socket socket = tsoo.getLeft();
ObjectOutputStream out = tsoo.getRight();
ObjectInputStream in = tsoo.getMiddle();
if (socket == null || socket.isClosed()) {
socket = new Socket(host, port);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
tt.set(Triple.of(socket, in, out));
}
out.writeObject(
new Invoke(type, beanName,
method.getDeclaringClass(), method.getName(), method.getParameterTypes(),
args));
out.flush();
Result r = (Result) in.readObject();
return r.getResult();
});
return (T) enhancer.create();
}