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. Linux上分析java程序的问题

    通过日志看不出来的问题,可以通过看java的堆栈信息(dump文件)来看出一些端倪. 1. 找java的进程id.jdk/bin的目录 ps -ef | grep java 2. cd 到bin的目录 ...

  2. git bash下对文件的操作

    window下的e盘中webpack文件夹操作 新建文件夹:mkdir wpdemo: 新建文件:touch index.html; 删除文件夹:rm -r wpdemo; 删除文件:rm index ...

  3. VS2015 使用Razor编写MVC视图时,Razor智能提示消失,报各种红线解决方案。

    打开文件夹 Users\<CurrentUser>\AppData\Local\Microsoft\VisualStudio\<version> 删除文件夹 Component ...

  4. windows下CMake使用图文手册 Part 2

    例子2:有目录的项目 我现在有个文件夹ProjectDate,有如下文件结构 E:. │  CMakeLists.txt │ ├─include │      Date.h │ └─src       ...

  5. [python实现设计模式]-4.观察者模式-吃食啦!

    观察者模式是一个非常重要的设计模式. 我们先从一个故事引入. 工作日的每天5点左右,大燕同学都会给大家订饭. 然后7点左右,饭来了. 于是燕哥大吼一声,“饭来啦!”,5点钟定过饭的同学就会纷纷涌入餐厅 ...

  6. org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/pcisv7]]

    重新配jar包

  7. 利用Flex组件birdeye绘制拓扑关系图

    birdeye绘制拓扑关系图 1.flex简单介绍 Flex 是一个高效.免费的开源框架,可用于构建具有表现力的 Web应用程序,这些应用程序利用Adobe Flash Player和Adobe AI ...

  8. 批量插入数据(基于Mybatis的实现-Oracle)

    前言:做一个数据同步项目,要求:同步数据不丢失的情况下,提高插入性能. 项目DB框架:Mybatis.DataBase:Oracle. -------------------------------- ...

  9. iOS CoreAnimate

    iOS CoreAnimate 东西比较多,这篇笔记是入门用的,主要讲述的是静态的图形绘画处理问题.(当然动画也只是一小部分)理解相关的概念问题:   参考资料 http://segmentfault ...

  10. Android IOS WebRTC 音视频开发总结(八十六)-- WebRTC中RTP/RTCP协议实现分析

    本文主要介绍WebRTC中的RTP/RTCP协议,作者:weizhenwei ,文章最早发表在编风网,微信ID:befoio 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID ...