1、说明

constructor-arg:通过构造函数注入。 
   property:通过setter对应的方法注入。

2、constructor-arg的使用示例

(1)、Model代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Student {
    private Integer id;
    private String name;
    private List<String> dream;
    private Map<String, Integer> score;
    private boolean graduation;
                                                                                                                                                                           
    public Student() {
                                                                                                                                                                           
    }
                                                                                                                                                                           
    public Student(Integer id, String name, List<String> dream,
            Map<String, Integer> score, boolean graduation) {
        this.id = id;
        this.name = name;
        this.dream = dream;
        this.score = score;
        this.graduation = graduation;
    }
                                                                                                                                                                           
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", dream=" + dream
                ", score=" + score + ", graduation=" + graduation + "]";
    }
                                                                                                                                                                           
}

(2)、xml配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<bean id="student" class="com.rc.sp.Student">
    <constructor-arg name="id" value="1"/>
    <constructor-arg name="name" value="student"/>
    <constructor-arg name="dream">
        <list>
            <value>soldier</value>
            <value>scientist</value>
            <value>pilot</value>
        </list>
    </constructor-arg>
    <constructor-arg name="score">
        <map>
            <entry key="math" value="90"/>
            <entry key="english" value="85"/>
        </map>
    </constructor-arg>
    <constructor-arg name="graduation" value="false"/>
</bean>

说明:<constructor-arg name="id" value="1"/>也可以改成<constructor-arg index="0" value="1"/>方式;boolean的值既可以用0/1填充,也可以用true/false填充。

3、property的使用示例

(1)、Model代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Teacher {
    private Integer id;
    private String name;
                                                         
    public Integer getId() {
        return id;
    }
                                                         
    public void setId(Integer id) {
        this.id = id;
    }
                                                         
    public String getName() {
        return name;
    }
                                                         
    public void setName(String name) {
        this.name = name;
    }
                                                         
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + "]";
    }
                                                         
}

(2)、xml配置:

1
2
3
4
<bean id="teacher" class="com.rc.sp.Teacher">
    <property name="id" value="1"></property>
    <property name="name" value="teacher"></property>
</bean>

4、Test

(1)、测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
public class Run {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
                                  
        Student student = (Student) context.getBean("student");
        System.out.println(student);
                                  
        Teacher teacher = (Teacher) context.getBean("teacher");
        System.out.println(teacher);
    }
}

(2)、输出结果:

Student [id=1, name=student, dream=[soldier, scientist, pilot],

score={math=90, english=85}, graduation=false]

Teacher [id=1, name=teacher]

Spring - constructor-arg和property的更多相关文章

  1. Spring 中出现Element : property Bean definitions can have zero or more properties. Property elements correspond to JavaBean setter methods exposed by the bean classes. Spring supports primitives, refer

    在这个ApplicationContext.xml文件中出现 如下报错 Element : property Bean definitions can have zero or more proper ...

  2. spring 的配置 bean>>property>>name属性

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. Spring整合Mybatis解决 Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    在Spring4和Mybatis3整合的时候,dao层注入'sqlSessionFactory'或'sqlSessionTemplate'会报错解决办法如下: package com.alibaba. ...

  4. 配置Spring Security 错误:Property or field 'ROLE_USER' cannot be found

    在学习http://www.mkyong.com/spring-security/spring-security-hello-world-example/时,出现以下错误: Property or f ...

  5. spring的xml的property和constructor-arg的解析

    参考文档: http://zzy7182.iteye.com/blog/1153473

  6. 100个高频Spring面试题

    译   原文:https://www.javacodegeeks.com/2014/05/spring-interview-questions-and-answers.html A:Spring概览 ...

  7. spring cuowu

    spring常见错误总结 在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with nam ...

  8. spring入门常见的问题及解决办法

    在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...

  9. spring错误汇总

    在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结.希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...

  10. 【转】spring 装配Bean中构造参数的注入

    转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...

随机推荐

  1. Mysql的视图、存储过程、函数、索引全解析

    视图是查询命令结果构成的一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集合,并可以当作表来查询使用. 1创建视图 --格式:C ...

  2. HBase 常用Shell命令

    两个月前使用过hbase,现在最基本的命令都淡忘了,留一个备查~ 进入hbase shell console$HBASE_HOME/bin/hbase shell如果有kerberos认证,需要事先使 ...

  3. Java创建WebService服务及客户端实现(转)

    简介 WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto. ...

  4. Java 判断文件夹、文件是否存在、否则创建文件夹

    1.判断文件是否存在,不存在创建文件 File file=new File("C:\\Users\\QPING\\Desktop\\JavaScript\\2.htm"); if( ...

  5. The project: project which is referenced by the classpath, does not exist.

    有个java工程运行时报The project: project which is referenced by the classpath, does not exist.错误 上网查解决方案,说是将 ...

  6. python 2day

    一 优化 username='alex' password=‘alex123’ 可以写成 username,password =‘alex’,'alex123' 二.再次优化 for i in ran ...

  7. jQuery-表单流程导航

    前言:以前做过一个项目,里面牵涉到流程相关的业务,需要用到流程页面导航的效果,完整的工作流相关的项目以前也做过,需要用到第三方插件,当然这里记录的业务需求没这个麻烦,仅仅需要能有一个页面导航的标题栏, ...

  8. (Hibernate进阶)Hibernate映射——一对一双向关联映射(六)

    上一篇博客我们介绍了一对一的单向关联映射,单向是指只能从人(Person)这端加载身份证端(IdCard),但是反过来,不能从身份证端加载人得信息.如图所示: 关键原因在于对象模型具有方向性: 单向: ...

  9. winFrom 常用控件属性及方法介绍

    目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...

  10. ruby 2.2

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ...