Spring bean reference example
来源:互联网 发布:步兵升级数据 编辑:程序博客网 时间:2024/05/19 13:56
In Spring, beans can “access” to each other by specify the bean references in the same or different bean configuration file.
1. Bean in different XML files
If you are referring to a bean in different XML file, you can reference it with a ‘ref
‘ tag, ‘bean
‘ attribute.
<ref bean="someBean"/>
In this example, the bean “OutputHelper
” declared in ‘Spring-Common.xml
‘ can access to other beans in ‘Spring-Output.xml
‘ – “CsvOutputGenerator
” or “JsonOutputGenerator
“, by using a ‘ref
’ attribute in property
tag.
File : Spring-Common.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="OutputHelper" class="com.mkyong.output.OutputHelper"> <property name="outputGenerator" > <ref bean="CsvOutputGenerator"/> </property> </bean></beans>
File : Spring-Output.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" /> <bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" /></beans>
2. Bean in same XML file
If you are referring to a bean in same XML file, you can reference it with ‘ref
‘ tag, ‘local
‘ attribute.
<ref local="someBean"/>
In this example, the bean “OutputHelper
” declared in ‘Spring-Common.xml
‘ can access to each other “CsvOutputGenerator
” or “JsonOutputGenerator
“.
File : Spring-Common.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="OutputHelper" class="com.mkyong.output.OutputHelper"> <property name="outputGenerator" > <ref local="CsvOutputGenerator"/> </property> </bean> <bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" /> <bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" /></beans>
Conclusion
Actually, the ‘ref
’ tag can access to a bean either in same or different XML files, however, for the project readability, you should use the ‘local
’ attribute if you reference to a bean which declared in the same XML file.
- Spring bean reference example
- Spring Bean Scopes Example
- Spring bean scopes example
- Spring(5)-Spring bean scopes example
- Spring Remoting by HTTP Invoker Example--reference
- Spring Bean Scope Example using @Scope Annotation
- Spring Bean Validation Example with JSR-303 Annotations
- Git Reference---Example
- spring example
- bean定义的继承 - Spring Framework reference 2.0.5 参考手册中文版
- java bean,spring bean
- Nuttx buttons example: undefined reference to 'poll’
- Spring Bean
- spring bean
- spring bean
- Spring bean
- spring & bean
- Spring Bean
- iPhone 如何添加横屏代码
- C#自动更换IE代理IP并判断代理是否可连接网络
- Android中ListView使用addHeaderView和addFooterView注意事项
- 你应该掌握的七种回归技术
- 03-树2. List Leaves (25)
- Spring bean reference example
- 个人发展职业定位思考
- POJ 3041 Asteroids(匈牙利算法模板)
- DialogFragment的使用+j接口回调
- HDU 1248:寒冰王座【Floyd & 完全背包】
- remove all the same elements
- 解析XML
- 获取设备唯一标识码
- 使用7zip压解各种文件的常用命令