injection bean && c xml namespace initialized by constructors

 

<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 cnamespace prefix as beana defined attribute, instead of corresponding constructor-argonly to debugcompile manner classeffective, since the debug compile-time, to the value stored in the class code (i.e., we debug debugging value to be seen), for non debugcompile the classfile Springwill 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

Guess you like

Origin blog.csdn.net/m0_37668842/article/details/82715398