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

一、注入简单属性

 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>

时间: 2024-10-05 23:24:52

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

SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-&lt;constructor-arg&gt;和c-namespace

1. 1 package soundsystem; 2 3 public class SgtPeppers implements CompactDisc { 4 5 private String title = "Sgt. Pepper's Lonely Hearts Club Band"; 6 private String artist = "The Beatles"; 7 8 public void play() { 9 System.out.println(&

SPRING IN ACTION 第4版笔记-第二章[email&#160;protected]、@Autowired的用法

一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPlayerConfig { } 2. @ComponentScan(basePackages={"soundsystem", "video"})//扫描多个包 public class CDPlayerConfig { } 3. @ComponentScan(basePac

SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)

一. 1.定义接口 Suppose that you need to authenticate against users in a non-relational database suchas Mongo or Neo4j. In that case, you’ll need to implement a custom implementationof the UserDetailsService interface. 1 public interface UserDetailsService

SPRING IN ACTION 第4版笔记-第九章Securing web applications-001-SpringSecurity简介(DelegatingFilterProxy、AbstractSecurityWebApplicationInitializer、WebSecurityConfigurerAdapter、@EnableWebSecurity、@EnableWebMvcS)

一.SpringSecurity的模块 At the least, you’ll want to include the Core and Configuration modules in your application’s classpath. Spring Security is often used to secure web applications, and that’s certainly the case with the Spittr application, so you’l

SPRING IN ACTION 第4版笔记-第九章Securing web applications-003-把用户数据存在数据库

一. 1.It’s quite common for user data to be stored in a relational database, accessed via JDBC . To configure Spring Security to authenticate against a JDBC -backed user store,you can use the jdbcAuthentication() method. The minimal configuration requ

SPRING IN ACTION 第4版笔记-第九章Securing web applications-002-把用户数据存在memory里(AuthenticationManagerBuilder、 UserDetailsManagerConfigurer.UserDetailsBuilder)

Spring Security is extremely flexible and is capable of authenticating users against virtually any data store. Several common user store situations—such as in-memory, relational database, and LDAP —are provided out of the box. But you can also create

SPRING IN ACTION 第4版笔记-第九章Securing web applications-004-对密码加密passwordEncoder

一. 1.Focusing on the authentication query, you can see that user passwords are expected to be stored in the database. The only problem with that is that if the passwords are stored in plain text, they’re subject to the prying eyes of a hacker. But if

SPRING IN ACTION 第4版笔记-第九章Securing web applications-011-把敏感信息请求转为https(requiresChannel())

1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() 1 @Override 2 protected void configure(HttpSecurity http) throws Exception { 3 http 4 .authorizeRequests() 5 .antMatchers("/spitter/me").hasRole("S

SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程

一. 1. 2.pizza-flow.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <flow xmlns="http://www.springframework.org/schema/webflow" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h