我正在寻找一种方法来存储我的对象,似乎最好的方法是使用代理。我在互联网上找到了 2 个注释,我应该使用哪个:
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
或
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS )
此外,代理是不是比使用 @Component 最好的使用方式
@Scope("session") 还是使用 @SessionAttributes?
最佳答案
您需要了解每个注释的作用,以便为您自己选择。请参阅 javadoc,here .继续进行更详细的说明。
第一个
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
创造
a JDK dynamic proxy implementing all interfaces exposed by the class of the target object
换句话说,代理将是目标对象类实现的接口(interface)的子类型,但不会是目标对象类本身的子类。
基本上 Spring 做了以下事情
public class Example {
public static void main(String[] args) throws Exception {
Foo target = new Foo();
InvocationHandler proxyHandler = ... // some proxy specific logic, likely referencing the `target`
// works fine
Printable proxy = (Printable) Proxy.newProxyInstance(Example.class.getClassLoader(),
target.getClass().getInterfaces(), proxyHandler);
// not possible, ClassCastException
Foo foo = (Foo) proxy;
}
public static class Foo implements Printable {
@Override
public void print() {
}
}
public interface Printable {
void print();
}
}
返回的代理将不是 Foo 类型,因此您不能将其注入(inject)该类型的任何目标。例如,Spring 将无法将其注入(inject)到类似的字段中
@Autowired
private Foo foo;
但会成功地将代理注入(inject)到类似的字段中
@Autowired
private Printable printable;
对代理的所有调用都将由 InvocationHandler 处理(通常执行一些特定于用例的逻辑,然后委托(delegate)给目标对象)。
第二个注释
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS )
创造
a class-based proxy (uses CGLIB).
除了接口(interface),使用CGLIB Spring 将能够创建一个代理,其类是目标类的子类。本质上,它执行以下操作
Foo target = new Foo();
net.sf.cglib.proxy.Enhancer enhancer = new net.sf.cglib.proxy.Enhancer();
enhancer.setInterfaces(target.getClass().getInterfaces());
enhancer.setSuperclass(target.getClass());
net.sf.cglib.proxy.MethodInterceptor interceptor = ... // some proxy specific logic, likely referencing the `target`
enhancer.setCallback(interceptor);
// works fine
Foo proxy = (Foo) enhancer.create();
CGLIB 创建一个新类,它是 Foo 的子类并实例化它(调用 Foo 的构造函数)。所有对代理的调用都将被提供的回调拦截(通常执行一些特定于用例的逻辑,然后委托(delegate)给目标对象)。
由于代理类扩展了 Foo,Spring 可以像
@Autowired
private Foo injectMe;
如果你是 programming to interfaces,就这么说吧,那么 ScopedProxyMode.INTERFACES 就足够了。如果不是,请使用 ScopedProxyMode.TARGET_CLASS。
至于使用@SessionAttributes ,它不是 session 范围 bean 的替代品。 session 属性只是对象,它们不是 bean。它们不具备 bean 可能具有的完整生命周期、注入(inject)功能、代理行为。
关于spring - 接口(interface)或 TARGET_CLASS : Which proxyMode should I choose?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759684/
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,
转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案
由于匿名block和散列block看起来大致相同。我正在玩它。我做了一些严肃的观察,如下所示:{}.class#=>Hash好的,这很酷。空block被视为Hash。print{}.class#=>NilClassputs{}.class#=>NilClass为什么上面的代码和NilClass一样,下面的代码又显示了Hash?puts({}.class)#Hash#=>nilprint({}.class)#Hash=>nil谁能帮我理解上面发生了什么?我完全不同意@Lindydancer的观点你如何解释下面几行:print{}.class#NilClassprint[].class#A
是否有可能以某种方式访问Class.new范围内的a?a=5Class.new{defb;aend}.new.b#NameError:undefinedlocalvariableormethod`a'for#:0x007fa8b15e9af0>#:in`b' 最佳答案 即使@MarekLipka的回答是正确的——改变变量范围总是有风险的。这是可行的,因为每个block都带有创建它的上下文,因此您的局部变量a突然变得不那么局部了——它变成了一个“隐藏的”全局变量:a=5object=Class.new{define_method(
classFooincludeModule.new{class_eval"deflab;puts'm'end"}deflabsuperputs'c'endendFoo.new.lab#=>mc======================================================================classFooincludeModule.new{instance_eval"deflab;puts'm'end"}deflabsuperputs'c'endend注意这里我把class_eval改成了instance_evalFoo.new.labresc
我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li
玩转Rails和Controller继承。我创建了一个名为AdminController的Controller,其中一个名为admin_user_controller的子类位于/app/controllers/admin/admin_user_controller.rb这是我的routes.rbnamespace:admindoresources:admin_user#Havetheadminmanagethemhere.endapp/controllers/admin/admin_user_controller.rbclassAdminUserController应用程序/Contr
我正在尝试升级到Rails4beta1,但遇到了一些问题。简而言之,这就是我的应用程序Controller的样子。classApplicationControllercaches_action在Rails4中移到了它自己的gem中,因此包含gem应该可以解决问题。gem"actionpack-action_caching",github:"rails/actionpack-action_caching"但是当我运行我的请求规范或在浏览器中访问该应用程序时,我收到此错误。app/controllers/application_controller.rb:3:in`':undefinedm