0

I have service controller which functionality I would like to reuse in another controller. Here is my service controller

@Controller
@Service
@Scope("session")
public class Controller1{
...
}

Here is my second controller

 @Controller
 public class Controller2 {
     @Autowired
     private Controller1 adminController;
     ...
 }

But I'm getting exception which says:

Error creating bean with name 'adminController': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton;

I think that this is because Controler1 is session-scoped bean and Controller2 is application. How can I reuse then Controller1 functionality inside Controller2? Thanks.

4

3 回答 3

1

这两个注释都@Controller作为@Service的特化@Component,允许通过类路径扫描自动检测实现类。并且@Controller通常与带注释的处理程序方法结合使用来处理 http 请求。所以你不必在同一个类上使用@Controller和。@Service您可以安全地删除@Service.

现在,如果你想将一个 HTTP 会话作用域 bean 注入另一个 bean,你必须注入一个 AOPproxy来代替作用域 bean。

也就是说,您需要注入一个代理对象,该对象公开与作用域对象相同的公共接口,但也可以从相关作用域(在本场景中为 HTTP 会话)检索真实的目标对象,并将方法调用委托给真实的目的。因此,为了使其工作,将@Scope注释更改为Controller1

@Controller
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Controller1{
...
}
于 2013-10-30T11:55:15.480 回答
1

您可以在控制器 1 的 xml 配置文件中使用 aop:scoped-proxy

 <bean id="controller1" class="...Controller1" scope="session">
    <aop:scoped-proxy />
 </bean>

看看spring 范围代理 bean

于 2013-10-30T11:21:53.707 回答
0

It depends of what you mean by functionnality but if you want to share a method in both controller, why not defining an abstract parent class defining this method and extends both controllers from this parent ?

于 2013-10-30T11:14:39.527 回答