一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE

二、

1.

 package soundsystem.collections;

 import java.util.List;

 import soundsystem.CompactDisc;

 public class BlankDisc implements CompactDisc {

   private String title;
private String artist;
private List<String> tracks; public BlankDisc(String title, String artist, List<String> tracks) {
this.title = title;
this.artist = artist;
this.tracks = tracks;
} public void play() {
System.out.println("Playing " + title + " by " + artist);
for (String track : tracks) {
System.out.println("-Track: " + track);
}
} }

(1)可以注入null,但运行会nullpointerException

 <bean id="compactDisc" class="soundsystem.BlankDisc">
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
<constructor-arg><null/></constructor-arg>
</bean>

(2)注入list

<bean id="compactDisc" class="soundsystem.BlankDisc">
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
<constructor-arg>
<list>
<value>Sgt. Pepper's Lonely Hearts Club Band</value>
<value>With a Little Help from My Friends</value>
<value>Lucy in the Sky with Diamonds</value>
<value>Getting Better</value>
<value>Fixing a Hole</value>
<!-- ...other tracks omitted for brevity... -->
</list>
</constructor-arg>

(2)如果构造函数的集合参数的元素是对象,则用<ref>

public Discography(String artist, List<CompactDisc> cds) { ... }
<bean id="beatlesDiscography"
class="soundsystem.Discography">
<constructor-arg value="The Beatles" />
<constructor-arg>
<list>
<ref bean="sgtPeppers" />
<ref bean="whiteAlbum" />
<ref bean="hardDaysNight" />
<ref bean="revolver" />
...
</list>
</constructor-arg>
</bean>

(3)也可以注入set

It makes sense to use <list> when wiring a constructor argument of type
java.util.List . Even so, you could also use the <set> element in the same way:

<bean id="compactDisc" class="soundsystem.BlankDisc">
<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
<constructor-arg>
<set>
<value>Sgt. Pepper's Lonely Hearts Club Band</value>
<value>With a Little Help from My Friends</value>
<value>Lucy in the Sky with Diamonds</value>
<value>Getting Better</value>
<value>Fixing a Hole</value>
<!-- ...other tracks omitted for brevity... -->
</set>
</constructor-arg>
</bean>

There’s little difference between <set> and <list> . The main difference is that when

Spring creates the collection to be wired, it will create it as either a java.util.Set or
a java.util.List . If it’s a Set , then any duplicate values will be discarded and the
ordering may not be honored. But in either case, either a <set> or a <list> can be
wired into a List , a Set , or even an array.

SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource

    1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...

  2. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>

    一.在xml中引入xml,用<import> <?xml version="1.0" encoding="UTF-8"?> <be ...

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

    一.注入简单属性 package soundsystem.properties; import org.springframework.beans.factory.annotation.Autowir ...

  4. SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace

    1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...

  5. SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例

    spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean. It appears that the CompactDisc ...

  6. SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法

    1. package soundsystem; import org.springframework.context.annotation.Bean; import org.springframewo ...

  7. SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法

    一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...

  8. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean

    1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...

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

    一. 1.定义接口 Suppose that you need to authenticate against users in a non-relational database suchas Mo ...

随机推荐

  1. jqery基础知识

    选择器按属性,子元素,表单匹配元素 <!doctype html> <html lang="en"> <head> <meta chars ...

  2. ActiveMQ系列(1) - 使用入门

    没网的日子真的不好过啊 1.背景:                   对于常见业务中,数据并发是一个很头疼的问题,很多时候,会出现资源共享导致线程阻塞的问题,这时候问题就来了,,,老板也尾随来了,来 ...

  3. 【转】数据库SQL优化大总结之 百万级数据库优化方案

    原帖地址:http://www.cnblogs.com/yunfeifei/p/3850440.html#undefined 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 or ...

  4. AndroidListview 滑动过程中图片显示重复错乱解决方案

    主要分析Android中Listview滚动过程造成的图片显示重复.错乱.闪烁的原因及解决方法,顺便跟进Listview的缓存机制. 1.原因分析 Listview item 缓存机制:为了使得性能更 ...

  5. python中关于正则表达式三

    2015年8月14日 11:10 7.2正则表达式操作 正则表达式使用反斜杠字符'\'来暗示一些特殊的形式或者允许特殊的字符使用但是没有调用它们特殊的意思.在字符串常量中的相同目标的字符的python ...

  6. Windows Server 2008 计划任务配置(任务计划程序)每分钟执行BAT

    首先Windows Server 2008不同于其他服务器操作系统和Windows Server 2003有着很大的区别,计划任务的名称是“任务计划程序”不在控制面板里,而是在“管理工具”里. 由于服 ...

  7. Java线程间通信-回调的实现方式

    Java线程间通信-回调的实现方式   Java线程间通信是非常复杂的问题的.线程间通信问题本质上是如何将与线程相关的变量或者对象传递给别的线程,从而实现交互.   比如举一个简单例子,有一个多线程的 ...

  8. PIMP模式的理解

    看了[C++程序设计技巧]Pimpl机制 之后,想了半天才理解    // MyClass.h 2: class MyClassImpl; // forward declaration 3: clas ...

  9. Spring学习之代理

    Spring的核心就是IOC和AOP IOC就是控制反转:   就是用配置文件的方式给javabean 赋值. 正常的在程序里;用new 的方式创建一个对象的时候,他就固定了值, 如果是注入的方式的话 ...

  10. 如何实现.so共享库文件

    .so共享库相当于window中的.DLL文件 两个进程同时调用了.so文件,进程就会加载的.so文件到各自的内存空间,而不能实现进程间通讯. .so文件编译的方法: -so文件不需要main文件,即 ...