码迷,mamicode.com
首页 > 编程语言 > 详细

SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space

时间:2016-03-02 10:55:30      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

一、注入简单属性

 1 package soundsystem.properties;
 2 import org.springframework.beans.factory.annotation.Autowired;
 3 
 4 import soundsystem.CompactDisc;
 5 import soundsystem.MediaPlayer;
 6 
 7 public class CDPlayer implements MediaPlayer {
 8   private CompactDisc compactDisc;
 9 
10   @Autowired
11   public void setCompactDisc(CompactDisc compactDisc) {
12     this.compactDisc = compactDisc;
13   }
14 
15   public void play() {
16     compactDisc.play();
17   }
18 
19 }

 

1.<property>

<bean id="cdPlayer"
class="soundsystem.CDPlayer">
<property name="compactDisc" ref="compactDisc" />
</bean>

 

2.p-namespace

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xmlns:p="http://www.springframework.org/schema/p"
 5   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7   <bean id="compactDisc" class="soundsystem.BlankDisc">
 8     <constructor-arg value="Sgt. Pepper‘s Lonely Hearts Club Band" />
 9     <constructor-arg value="The Beatles" />
10   </bean>
11         
12   <bean id="cdPlayer" class="soundsystem.properties.CDPlayer"
13         p:compactDisc-ref="compactDisc" />
14 
15 </beans>

技术分享

二、注入集合属性

 1 package soundsystem.properties;
 2 
 3 import java.util.List;
 4 
 5 import soundsystem.CompactDisc;
 6 
 7 public class BlankDisc implements CompactDisc {
 8 
 9   private String title;
10   private String artist;
11   private List<String> tracks;
12 
13   public void setTitle(String title) {
14     this.title = title;
15   }
16 
17   public void setArtist(String artist) {
18     this.artist = artist;
19   }
20 
21   public void setTracks(List<String> tracks) {
22     this.tracks = tracks;
23   }
24 
25   public void play() {
26     System.out.println("Playing " + title + " by " + artist);
27     for (String track : tracks) {
28       System.out.println("-Track: " + track);
29     }
30   }
31 
32 }

 

1.<property>

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7   <bean id="compactDisc"
 8         class="soundsystem.properties.BlankDisc">
 9     <property name="title" value="Sgt. Pepper‘s Lonely Hearts Club Band" />
10     <property name="artist" value="The Beatles" />
11     <property name="tracks">
12       <list>
13         <value>Sgt. Pepper‘s Lonely Hearts Club Band</value>
14         <value>With a Little Help from My Friends</value>
15         <value>Lucy in the Sky with Diamonds</value>
16         <value>Getting Better</value>
17         <value>Fixing a Hole</value>
18         <value>She‘s Leaving Home</value>
19         <value>Being for the Benefit of Mr. Kite!</value>
20         <value>Within You Without You</value>
21         <value>When I‘m Sixty-Four</value>
22         <value>Lovely Rita</value>
23         <value>Good Morning Good Morning</value>
24         <value>Sgt. Pepper‘s Lonely Hearts Club Band (Reprise)</value>
25         <value>A Day in the Life</value>
26       </list>
27     </property>
28   </bean>
29         
30   <bean id="cdPlayer"
31         class="soundsystem.properties.CDPlayer"
32         p:compactDisc-ref="compactDisc" />
33 
34 </beans>

 

2.p-namespace

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xmlns:p="http://www.springframework.org/schema/p"
 5   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7   <bean id="compactDisc"
 8         class="soundsystem.properties.BlankDisc"
 9         p:title="Sgt. Pepper‘s Lonely Hearts Club Band"
10         p:artist="The Beatles">
11     <property name="tracks">
12       <list>
13         <value>Sgt. Pepper‘s Lonely Hearts Club Band</value>
14         <value>With a Little Help from My Friends</value>
15         <value>Lucy in the Sky with Diamonds</value>
16         <value>Getting Better</value>
17         <value>Fixing a Hole</value>
18         <value>She‘s Leaving Home</value>
19         <value>Being for the Benefit of Mr. Kite!</value>
20         <value>Within You Without You</value>
21         <value>When I‘m Sixty-Four</value>
22         <value>Lovely Rita</value>
23         <value>Good Morning Good Morning</value>
24         <value>Sgt. Pepper‘s Lonely Hearts Club Band (Reprise)</value>
25         <value>A Day in the Life</value>
26       </list>
27     </property>
28   </bean>
29         
30   <bean id="cdPlayer"
31         class="soundsystem.properties.CDPlayer"
32         p:compactDisc-ref="compactDisc" />
33 
34 </beans>

 

3.util-namepsace

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xmlns:p="http://www.springframework.org/schema/p"
 5   xmlns:util="http://www.springframework.org/schema/util"
 6   xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7     http://www.springframework.org/schema/beans/spring-beans.xsd
 8     http://www.springframework.org/schema/util 
 9     http://www.springframework.org/schema/util/spring-util.xsd">
10 
11   <bean id="compactDisc"
12         class="soundsystem.properties.BlankDisc"
13         p:title="Sgt. Pepper‘s Lonely Hearts Club Band"
14         p:artist="The Beatles"
15         p:tracks-ref="trackList" />
16 
17   <util:list id="trackList">  
18     <value>Sgt. Pepper‘s Lonely Hearts Club Band</value>
19     <value>With a Little Help from My Friends</value>
20     <value>Lucy in the Sky with Diamonds</value>
21     <value>Getting Better</value>
22     <value>Fixing a Hole</value>
23     <value>She‘s Leaving Home</value>
24     <value>Being for the Benefit of Mr. Kite!</value>
25     <value>Within You Without You</value>
26     <value>When I‘m Sixty-Four</value>
27     <value>Lovely Rita</value>
28     <value>Good Morning Good Morning</value>
29     <value>Sgt. Pepper‘s Lonely Hearts Club Band (Reprise)</value>
30     <value>A Day in the Life</value>
31   </util:list>
32 
33   <bean id="cdPlayer"
34         class="soundsystem.properties.CDPlayer"
35         p:compactDisc-ref="compactDisc" />
36 
37 </beans>

技术分享

SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5233902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!