简体   繁体   English

如何在带有xml配置的Spring MVC中提供默认的bean实现?

[英]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.

相关问题 在春季如何将xml bean配置文件导入到@configuration类? - How can I import a xml bean configuration file to a @configuration class in spring? 如何在Spring中按条件提供默认bean? - How to provide a default bean in Spring by condition? 如何使用Spring XML配置来设置包含特定类型的所有bean的列表的bean属性? - How can I use Spring XML configuration to set a bean property with list of all beans of a certain type? 在没有具体实现的接口的xml配置中创建spring bean - Creating spring bean in xml configuration for an interface without concrete implementation 为什么我不能将用@Service注释的bean注入到spring-security.xml配置文件中声明的bean中? - Why I can't inject this bean annoted with @Service into a bean declared into the spring-security.xml configuration file? 如何在Spring配置中为所有子类提供单个bean定义? - How to provide single bean definition for all child class in spring configuration? 如何使用几个bean配置文件配置Spring BeanFactory? - How can I configure a Spring BeanFactory with several bean configuration files? Spring mvc控制器bean配置 - Spring mvc controller bean configuration 当我使用STS创建spring MVC项目时,如何修改默认XML内容 - When I created the spring MVC project with STS, how can modify the default XML content Spring MVC xml配置 - Spring MVC xml configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM