一.当构造函数有集合时,只能用<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. PHP 概述 特点 基础语法

    PHP是什么 http://php.net/manual/zh/intro-whatis.php#intro-whatis  PHP(Hypertext Preprocessor,超文本预处理器)是一 ...

  2. Oracle错误代码案例总结及解决方案

    引自:http://blog.sina.com.cn/s/blog_9daa54ec0100yr7v.html 常见错误: ORA-00001:违反唯一约束条件(主键错误) ORA-00028:无法连 ...

  3. jquery知识 内部 外部插入元素

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. C++Primer笔记二

    真是一本好书,就这么点,就感觉学到很多了,当然也是我水平太差. 用shell或者bash的时候有一个文件重定向,就是每次程序运行的时候,我们都需要手动输入内容,然后程序输出内容,这时可以用文件来代替. ...

  5. Java 十六进制转十进制

    public static int hexToDecimal(String hex) { int decimalValue = 0; for (int i = 0; i < hex.length ...

  6. BAT变量中的百分号学习

    在BlogJava上看到如下的批处理文件,并将其转记在此: 1 2 3 4 5 6 7 8 @echo off rem bat 获取系统时间,并去掉时间小时前面的空格 rem  2012-12-12 ...

  7. Unity中使用RequireComponent,没有添加上组件

    using UnityEngine; using System.Collections; [RequireComponent(typeof(MeshFilter), typeof(MeshRender ...

  8. bzoj1485:[HNOI2009]有趣的数列

    思路:首先限制数很多,逐步来考虑,限制一很容易满足,考虑限制二,也就是让奇数位和偶数位上的数递增,限制三就是让奇数位上的数小于奇数位加一对应的偶数位上的数,那么我们可以把形成序列的过程看成加数的过程, ...

  9. bzoj2653:middle

    思路:首先容易想到二分答案,但如何去check呢,对于一段区间[l,r],把所有小于答案的都赋值为-1,大于等于它的都赋值为1,然后求左端点在[a,b],右端点在[c,d]的最大子串和即可(也就是区间 ...

  10. LINQ to Entities 查询注意事项

    1> 排序信息丢失 如果在排序操作之后执行了任何其他操作,则不能保证这些附加操作中会保留排序结果.这些操作包括 Select 和 Where 等.另外,采用表达式作为输入参数的 First 和 ...