<Bean id = "compactDisc" class = "soundsystem.CompactDisc" /> // declare a simple bean
CDPlay compactDisc a bean which is incorporated (incorporated constructor bean) by the ID
<bean id="cdPlayer" class="soundsystem.CDPlay" >
<constructor-arg ref="compactDisc">
</bean>
C- namespace may be used in the Spring, c- Spring3.0 namespace is introduced. He is a more concise description of the configuration parameters in the XML way. To use it must be declared at the top of the XML
Spring provides a namespace for c <constructor-arg> element as an alternative, to simplify deployment
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
...
</beans>
<bean id = "cdPlayer" calss = "soundsystem.CDPlay" c:cd-ref = "compactDisc">
c namespace using templates: c: constructor parameter name -ref = "bean to be injected of Id"
Notably, the configuration parameter name directly coupled c
namespace prefix as bean
a defined attribute, instead of corresponding constructor-arg
only to debug
compile manner class
effective, since the debug compile-time, to the value stored in the class code (i.e., we debug debugging value to be seen), for non debug
compile the class
file Spring
will not be able to get the name of the constructor parameter corresponds, this situation may not work correctly. In this case we can use the index directly constructor parameter underlined “_”
prefix parameter name to replace the corresponding index is zero-based, such as when an index above example would be to replace the following way.
<bean id = "cdPlayer" calss = "soundsystem.CDPlay" c:_0-ref = "compactDisc">
If only one configuration parameter, you do not need to identify with the index, then:
<bean id = "cdPlayer" calss = "soundsystem.CDPlay" c:_-ref = "compactDisc">
The literal values directly injected into the constructor
<bean id = "cdPlayer" calss = "soundsystem.CDPlay">
<constructor-arg value="This is a param value">
</bean>
Using namespace as follows:
<bean id = "cdPlayer" calss = "soundsystem.CDPlay"
c:_title = "This is a param value"
c:_artist = " this a one param value" />
Can be seen, the fitting assembly literal references are removed in the attribute name "-ref" suffix Similarly, indexes can be assembled by
<bean id = "cdPlayer" calss = "soundsystem.CDPlay"
c:_0 = "This is a param value"
c:_1 = " this a one param value" />
If a parameter:
<bean id = "cdPlayer" calss = "soundsystem.CDPlay"
c:_ = "This is a param value" />
Use <constructor-arg> time and c namespace implement constructor injection, there are some subtle differences, in terms of assembling the collection, <constructor-arg> advantage now use c namespace attribute can not be achieved assembling a collection of functions