Spring - constructor-arg和property的使用示例
一、说明
constructor-arg:通过构造函数注入。
property:通过setter对应的方法注入。
二、property使用实例
1、Model代码:
public class Person {
private String name;
private int age;
private String className;
private String grade;
private List<Person> friends = new ArrayList<>();
public List<Person> getFriends() {
return friends;
}
public void setFriends(List<Person> friends) {
this.friends = friends;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String toString(){
return "Person [name=" + name + ", classname=" + className +", age=" + age+", grade=" + grade+", friends=\n" +friends+ "]";
}
}
2、xml配置:
<bean id="person" class="models.Person">
<property name="age" value="22"/> //通过setXXX方法,所以Person对象属性不用全部设置,classname和grade未设置
<property name="name" value="liuzg"/>
<property name="friends"> //通过ref 传入已经初始化的bean wangxin和lirifeng
<list> //liuzg有两个朋友 wangxin和lirifeng
<ref bean="wangxin"/> //local只能在同一个XML中实现id引入,而bean可以跨XML文件引入,同时bean可以根据name属性进行引用,使用范围明显较广,更强大。
<ref bean="lirifeng"/>
</list>
</property>
</bean>
<bean id="wangxin" class="models.Person">
<property name="age" value="22"/>
<property name="name" value="wangxin"/>
</bean>
<bean id="lirifeng" class="models.Person">
<property name="age" value="22"/>
<property name="name" value="lirifeng"/>
</bean>
三、constructor-arg使用实例
1、Model代码:
public class Student {
private String name;
private int age;
private String className;
private String grade;
private List<Person> friends = new ArrayList<>();
public Student() {
}
public Student(String name,int age,String className,String grade,List<Person> friends){
this.friends = friends;
this.name = name;
this.age = age;
this.className = className;
this.grade = grade;
}
public String toString(){
return "student [name=" + name + ", classname=" + className +", age=" + age+", grade=" + grade+", friends=\n" +friends+ "]";
}
}
2.xml配置
<bean id="liangyuqi" class="models.Student">
<constructor-arg index="0" value="liangyuqi"/> //通过构造函数,需根据函数形参 全部设置
<constructor-arg name="age" value="18"/> //可以根据index或者name设置属性,下标从0开始。boolean的值既可以用0/1填充,也可以用true/false填充。
<constructor-arg index="2" value="1401"/>
<constructor-arg index="3" value="95"/>
<constructor-arg index="4" ref="person"/>
</bean>
四、Test
1.测试代码
public class MyTest {
public void testSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("test.xml"); //1.读取spring初始化的配置文件
Object p = context.getBean("liangyuqi"); //2.根据bean获取Student实现类对象 p
System.out.println(p);
}
}
2.输出结果 : liangyuqi的朋友有liuzg,以及显示liuzg的friends set wnagxin,lirifeng

Spring - constructor-arg和property的使用示例的更多相关文章
- 简单理解Spring之IOC和AOP及代码示例
Spring是一个开源框架,主要实现两件事,IOC(控制反转)和AOP(面向切面编程). IOC 控制反转,也可以称为依赖倒置. 所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B, ...
- (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...
- Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个 Demo 来试试它的效果,越 ...
- Spring MVC-表单(Form)处理示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_form_handling.htm 说明:示例基于Spring MVC 4.1.6 ...
- 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 boot 日志介绍 以及 logback配置示例
https://www.cnblogs.com/flying607/p/7827460.html 以下是springboot的一个局部依赖关系: 可以看到,java util logging(jul) ...
- spring security 3 自定义认证,授权示例
1,建一个web project,并导入所有需要的lib. 2,配置web.xml,使用Spring的机制装载: <?xml version="1.0" encoding=& ...
- Spring Session实现分布式session的简单示例
前面有用 tomcat-redis-session-manager来实现分布式session管理,但是它有一定的局限性,主要是跟tomcat绑定太紧了,这里改成用Spring Session来管理分布 ...
随机推荐
- 从文本中读取字符——feof函数问题
feof()函数 函数原型:int feof(FILE *fp): 函数功能:检测流上的文件结束符,如果文件结束,则返回非0值,否则返回0,文件结束符只能被clearerr()函数清除 (函数feof ...
- CentOS防火墙配置
1.查询防火墙状态 service iptables status 2.开启防火墙 service iptables start 3.关闭防火墙 service iptables stop 4.重启防 ...
- 微信小程序如何套用iconfont
前言 如果你在开发微信时,没有图标的话,可以到http://www.iconfont.cn/ 官方下使用图标,那么我们去使用一些吧,到官方网址下点击使用~ 下载代码即可使用,看看下载的文件吧. 如图可 ...
- 《机器学习实战(基于scikit-learn和TensorFlow)》第三章内容的学习心得
本章主要讲关于分类的一些机器学习知识点.我会按照以下关键点来总结自己的学习心得:(本文源码在文末,请自行获取) 什么是MNIST数据集 二分类 二分类的性能评估与权衡 从二元分类到多类别分类 错误分析 ...
- python实现归并排序算法
归并排序(英语:Merge sort,或mergesort),是创建在归并操作上的一种有效的排序算法,效率为O(nlogn). 1945年由约翰·冯·诺伊曼首次提出.该算法是采用分治法(Divide ...
- Spring Boot整合 Thymeleaf 模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
- PyTorch(二)Intermediate
Convolutional Neural Network import torch import torch.nn as nn import torchvision import torchvisio ...
- Python函数——装饰器
前言 给下面的函数加上运行时间 def fib(n): a, b = 0, 1 for i in range(n): print(b) a, b = b, a+b return b a = fib(5 ...
- 【LeetCode】21.合并两个有序链表
题目 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-> ...
- oracle生成AWR报告方法
2018-04-02 19:59:42 在10g 11g中AWR自动的每隔一小时进行一次数据采集并生成快照.下面是生成AWR报告的步骤: 1:使用oracle用户在数据库服务器上执行如下命令 sqlp ...