[英]How can I provide a default bean implementation in Spring MVC with xml configuration?
I'm working on some old code base and it's using xml configuration. 我正在使用一些旧的代码库,并且正在使用xml配置。
Basically it has a basic controller named BaseController
and all other controllers inherit that. 基本上,它有一个名为
BaseController
的基本控制器,所有其他控制器都继承该控制器。 Now I need to add an extra service bean which all current controllers need to use. 现在,我需要添加一个额外的服务bean,所有当前控制器都需要使用该bean。 The bean definition is like this:
Bean定义如下:
<bean id="myService" class="com.myweb.MyService" scope="singleton"/>
The base controller will also have a field named MyService myService
, with a null value now. 基本控制器还将具有一个名为
MyService myService
的字段,现在其值为空。
Instead of setting the property name in the xml file under each existing controller bean mapping (there are too many), how can I set the singleton MyService instance to all controllers at runtime (like a default value instead of null)? 与其在每个现有的控制器bean映射下(存在太多)下的xml文件中设置属性名称,不如在运行时如何将单例MyService实例设置给所有控制器(例如默认值而不是null)?
You can autowire the bean. 您可以自动装配bean。
Annotate your BaseController
MyService
field with 使用以下注释您的
BaseController
MyService
字段
@Autowired
private MyService myService;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.