Spring - constructor-arg和property
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的更多相关文章
- 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 ...
- spring 的配置 bean>>property>>name属性
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring整合Mybatis解决 Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在Spring4和Mybatis3整合的时候,dao层注入'sqlSessionFactory'或'sqlSessionTemplate'会报错解决办法如下: package com.alibaba. ...
- 配置Spring Security 错误:Property or field 'ROLE_USER' cannot be found
在学习http://www.mkyong.com/spring-security/spring-security-hello-world-example/时,出现以下错误: Property or f ...
- spring的xml的property和constructor-arg的解析
参考文档: http://zzy7182.iteye.com/blog/1153473
- 100个高频Spring面试题
译 原文:https://www.javacodegeeks.com/2014/05/spring-interview-questions-and-answers.html A:Spring概览 ...
- spring cuowu
spring常见错误总结 在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with nam ...
- spring入门常见的问题及解决办法
在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...
- spring错误汇总
在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结.希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...
- 【转】spring 装配Bean中构造参数的注入
转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...
随机推荐
- android的四层体系结构,基于mvc三层结构浅析
从多方面理解Android体系结构 1.以分层的方式来看Android 安卓体系结构分为四层. 首先看一下官方关于Android体系结构的图: 1).Linux Kernel:负责硬件的驱动程序.网络 ...
- NGUI的localPosition和Position之间的关系
假设有子节点为child, 父节点为parent, 且都是Transform类型. 则: child.localPosition = (child.position - parent.position ...
- 字典树的C++实现
此次代码使用了诸多新鲜玩意,比如自动类型推断,模板类等等,感觉真的超级好玩啊. 自己做了一个简易的测试,程序的健壮性什么的还是比较差的,此程序留待以后进行功能补全. #pragma once #inc ...
- OpenGL 像素在内存中的排列方式
在OpenGL中所有和图像像素有关的API(包括glTexImage2D, glReadPixels等)第一个像素从左下角开始,从左到又一次排列,满了从下到上排列. 这个和Windows 下惯用的左上 ...
- 关于计算机的ID和用户ID之间的关系
关于计算机的ID和用户ID之间的关系 计算机安装完系统后就会生成计算机ID,然后系统会以计算机ID为前缀附加数字创建Administrator(500)和Guest(501)用户ID,其他用户的ID将 ...
- 安装了多个Oracle11g的客户端,哪个客户端的tnsnames.ora会起作用?
如果我们由于需要安装了多个Oracle的client,哪个客户端的tnsnames.ora会起作用呢? 答案是: 在安装好clinent端后,安装程序会把client的bin目录放到path里面,pa ...
- Access denied for user 'Administrator'@'localhost' (using password: YES)
Access denied for user 'Administrator'@'localhost' (using password: YES) 配置文件中明明是用的root <entry ke ...
- mysql 连接空闲超8小时自动断开连接问题(linux)
在mysql配置文件里添加wait_timeout和interactive_timeout两个值 [mysqld] wait_timeout= interactive_timeout= 超时时间,10 ...
- Could not parse mapping document from input stream hibernate配置异常
十二月 , :: 下午 org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context ...
- MongoDB(五)mongo语法和mysql语法对比学习
我们总是在对比中看到自己的优点和缺点,对于mongodb来说也是一样,对比学习让我们尽快的掌握关于mongodb的基础知识. mongodb与MySQL命令对比 关系型数据库一般是由数据库(datab ...