我偶然发现了一个案例,其中使用@Cacheable创建的AOP代理在Spring 3.1.1中打破了依赖注入.这是我的场景:
我有一个接口和一个在实现的方法中使用@Cacheable实现此接口的类.
示例界面:
public interface ImgService {
public byte[] getImage(String name);
}
Run Code Online (Sandbox Code Playgroud)
示例实现:
public class ImgServiceImpl implements ImgService {
@Cacheable(cacheName = "someCache")
public byte[] getImage(String name){//TODO};
protected String someOtherMethod(){//};
}
Run Code Online (Sandbox Code Playgroud)
我还需要JUnit测试类 - 一个注入接口,另一个注入实现:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:META-INF/spring.xml" })
public class ImgServiceTest {
@Inject
private ImgService;
}
Run Code Online (Sandbox Code Playgroud)
和
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:META-INF/spring.xml" })
public class ImgServiceImplTest {
@Inject
private ImgServiceImpl;
}
Run Code Online (Sandbox Code Playgroud)
接口的依赖注入工作正常.但是,当我在第二个测试类中注入实现时,我得到"注入自动连接的依赖项失败".我能够调试它,看起来ClassUtils.isAssignableValue()错误地将所需类型与代理类进行比较.它由DefaultListableBeanFactory调用.更奇怪的是,如果我从已实现的方法中删除@Cacheable注释并将其添加到其他一些受保护/私有方法,依赖注入再次正常工作.这是一个错误,处理这种情况的正确方法是什么?
我有一个在JVM中运行时生成的代理(生成为JDKProxy或CGLIB).我想知道是否有办法将这个类的内容(看起来像com.sun.proxy $ Proxy123.class)写入一个文件,以便我可以使用像反编译器这样的jd-eclipse来查看代码的类型产生.由于该类存在于JVM中,我想知道是否有一种方法可以让ClassLoader向实际类提供一个InputStream/URL,然后可以用来将内容写入磁盘 - 而这个文件位于磁盘上可以使用jd-eclipse或javap读取.我知道这不是一个生产用例,但我很想看到这个动态生成的类的内容.
谢谢!
的std::iterator_traits类模板定义5种嵌套类型:iterator_category,value_type,difference_type,pointer和reference.浏览的源极<algorithm>两者的libc ++和libstdc ++的头,一个可以看到的许多用途value_type,difference_type和iterator_category,但只有一个reference(内部std::iter_swap),并没有对pointer.
我的应用程序使用手工构建的代理迭代器/代理引用对.我想转换到使用Boost iterator_facade,它允许我将引用类型从默认配置为T&任意类型,但对于T*默认情况下的指针类型则不是这样.我想避免被嵌套pointer类型的一些深度隐藏使用所困扰.
注意:迭代器是没有嵌套成员的内置类型的代理,因此我不需要兼容operator->(返回类型将是pointer).
问:有什么用例是有标准库中的嵌套类型pointer里面iterator_traits?
我正在尝试在原生Firefox(并使用Babel)中代理承诺.
var prom = new Promise(function(resolve, reject){resolve(42)});
var promProxy = new Proxy(prom, {});
promProxy.then(function(response){console.log(response)});Run Code Online (Sandbox Code Playgroud)
这不起作用,我得到'TypeError:'然后'调用一个没有实现接口Promise的对象.'
我正在构建一个从第三方检索并提交给第三方的服务.第三方提供了一个复杂的数据模型,其中包括三种不同的状态类型,所有这些类型的整数都有一个模糊的数字序列.下面提供了一个示例(键:值):
HandlerStatus CustomerStatus ArchiveStatus --- --- --- activeUnallocated:40 draftOpen:0 openOpen:0 activeAllocated:26 submitted:2 closed:1 activePromoted:102 approved:100 Declined:2 declined:1 ... ...
只有有限数量的组合,每个组合都有一个术语(提交,委托给另一个用户,等待确认等).例如:
Combination called "Submitted": HandlerStatus has to be "activeUnallocated" (40) CustomerStatus has to be "submitted" (2) ArchiveStatus has to be "openOpen" (0)
我们可以期待添加其他值,并且用户友好名称可能会随时间而变化.
此外,还有一些操作,例如提交和委托给不需要用户选择新值的其他用户,这意味着服务需要知道在这些操作发生时必须设置的一组组合.
我想到了三种不同的解决方案,各有利弊.
优点:
缺点:
优点:
缺点:
优点:
缺点:
我已经尝试研究最佳实践和模式,以确定哪种解决方案最适合,可维护性和良好的代码设计,但我没有得到任何地方(同时我在开发过程中严重依赖魔术字符串和数字) ).
为了更好的调试,我经常希望:
Exception
at com.example.blah.Something.method()
at com.example.blah.Xyz.otherMethod()
at com.example.hello.World.foo()
at com.example.debug.version_3_8_0.debug_info_something.Hah.method() // synthetic method
at com.example.x.A.wrappingMethod()
Run Code Online (Sandbox Code Playgroud)
如上所示的调试堆栈帧将动态生成,就像a一样java.lang.reflect.Proxy,除了我想完全控制最终在代理上的完整限定方法名称.
在通话网站上,我会做一些愚蠢而简单的事情:
public void wrappingMethod() {
run("com.example.debug.version_3_8_0.debug_info_something.Hah.method()", () -> {
World.foo();
});
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这wrappingMethod()是一个真正的方法,最终在堆栈跟踪上,Hah.method()是一个动态生成的方法,而又World.foo()是一个真正的方法.
是的,我知道这会污染已经很深的堆栈痕迹.别担心.我有我的理由.
是否有(简单)方法来执行此操作或与上述类似的方法?
具有以下目的:
let obj = { id: 0 };
Run Code Online (Sandbox Code Playgroud)
和以下内容Proxy:
let objProxy = new Proxy(obj, {
get: (target, name) => {
if (name == "id")
return "id from proxy";
}});
Run Code Online (Sandbox Code Playgroud)
是否可以Proxy在Object.assign()(或对象传播算子,其afaik只是其语法糖Object.assign())之后“保留” ?
let objProxyNew = Object.assign({}, objProxy); // i.e. {...objProxy};
Run Code Online (Sandbox Code Playgroud)
这样objProxyNew.id返回"id from proxy"?
例如,赋值运算符std::slice_array:
void operator=(const valarray<T>&) const; //#1
void operator=(const T&) const; //#2
const slice_array& operator=(const slice_array&) const; //#3
Run Code Online (Sandbox Code Playgroud)
#1并#2返回void,但#3返回const slice_array&。
它禁止某些代码,例如:
std::valarray<int> va{1, 2, 3, 4, 5, 6};
va[std::slice(3, 2, 2)] = va[std::slice(0, 2, 2)] = va[0];
Run Code Online (Sandbox Code Playgroud)
为什么?
我试图通过getClass().getSimpleName()under 获取一些类名Spring,它返回类似的东西
MyClass$$EnhancerBySpringCGLIB$$SOMEHEX
Run Code Online (Sandbox Code Playgroud)
这可能是因为Spring将类包装到代理中.
有没有便携的方法来获得原始的类名?
我正在浏览hibernate的文档并找到了这些行
无参数构造函数是所有持久化类的要求; Hibernate必须使用Java Reflection为您创建对象.构造函数可以是私有的,但是运行时代理生成和无需字节码检测的高效数据检索需要包或公共可见性
任何人都可以解释运行时代理生成和有效的数据检索,无需字节码指示
proxy-classes ×10
java ×5
c++ ×2
cglib ×2
javascript ×2
spring ×2
annotations ×1
bytecode ×1
c# ×1
ecmascript-6 ×1
enums ×1
es6-promise ×1
hibernate ×1
iterator ×1
reflection ×1
traits ×1
valarray ×1