我正在寻找一种存储我的对象的方法,似乎最好的方法是使用代理.我在互联网上找到了2个注释,我应该使用哪个注释:
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
Run Code Online (Sandbox Code Playgroud)
要么
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS )
Run Code Online (Sandbox Code Playgroud)
此外,代理是否是使用@Component
@Scope("session")或使用的最佳方式@SessionAttributes?
让我们考虑以下 bean:
@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.INTERFACES)
public class MyBeanB implements MyBeanBInterface {
private static final AtomicLong COUNTER = new AtomicLong(0);
private Long index;
public MyBeanB() {
index = COUNTER.getAndIncrement();
System.out.println("constructor invocation:" + index);
}
@Transactional
@Override
public long getCounter() {
return index;
}
}
Run Code Online (Sandbox Code Playgroud)
并考虑两种不同的用法:
@Service
public class MyBeanA {
@Autowired
private MyBeanB myBeanB;
....
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,应用程序无法启动并打印:
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'myBeanB' could not be injected as a 'my.pack.MyBeanB' because …Run Code Online (Sandbox Code Playgroud)