我以前很喜欢用 proxy class. 因为我认为纯 OO 的话,类型本身应该有业务方法,而不是像把 pojo 当作 c 语言那样的结构体,再传给 singleton 的 service 去处理,那不成了 c 语言的结构化编程了么。
举个例子,系统里面有个 User 类型,我喜欢定义:
class User {
@Transaction
void sendMessage(User other) {...}
@Transaction
void buy(Good good) {...}
}
因为 @Transaction 这个注解的存在,spring 得帮我生成 proxy class. 如果 User 有字段,在 proxy class 里面那些字段的内容就是空的。
【 在 oldwatch 的大作中提到: 】
: Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.
: Spring AOP can also use CGLIB proxies. This is necessary to proxy classes rather than interfaces. By default, CGLIB is used if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes, business
: classes normally implement one or more business interfaces. It is possible to force the use of CGLIB, in those (hopefully rare) cases where you need to advise a method that is not declared on an interface or where you need to pass a proxied object to a m
: ...................
--
修改:hgoldfish FROM 120.33.11.*
FROM 120.33.11.*