springBean参数注入的几个方法
1、普通方式注入
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.xiaostudy.service.PersonImple">
<property name="id" value="2018"></property>
<property name="name" value="xiaostudy"></property>
<property name="age" value="23"></property>
</bean>
</beans>
PersonImple.java
package com.xiaostudy.service;
public class PersonImple implements Person {
private Integer id;
private String name;
private Integer age;
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "PersonImple [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
2、通过构造方法的注入,name为参数名称
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.xiaostudy.service.PersonImple">
<constructor-arg name="id" value="2018"></constructor-arg>
<constructor-arg name="name" value="xiaostudy"></constructor-arg>
<constructor-arg name="age" value="23"></constructor-arg>
</bean>
</beans>
PersonImple.java
package com.xiaostudy.service;
public class PersonImple implements Person {
private Integer id;
private String name;
private Integer age;
public PersonImple(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "PersonImple [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
3、也是通过构造方法注入,index为参数索引的位置
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.xiaostudy.service.PersonImple">
<constructor-arg index="0" value="2018"></constructor-arg>
<constructor-arg index="1" value="xiaostudy"></constructor-arg>
<constructor-arg index="2" value="23"></constructor-arg>
</bean>
</beans>
4、也是通过构造方法注入,type是参数的数据类型
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.xiaostudy.service.PersonImple">
<constructor-arg type="java.lang.Integer" value="2018" index="0"></constructor-arg>
<constructor-arg type="java.lang.String" value="xiaostudy" index="1"></constructor-arg>
<constructor-arg type="java.lang.Integer" value="23" index="2"></constructor-arg>
</bean>
</beans>
springBean参数注入的几个方法的更多相关文章
- struts2 参数注入 方法拦截器
web.xml: <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=" ...
- 【原创】内核ShellCode注入的一种方法
标 题: [原创]内核ShellCode注入的一种方法 作 者: organic 时 间: 2013-05-04,04:34:08 链 接: http://bbs.pediy.com/showthre ...
- spring mvc 复杂参数注入
过了这么久,又重新把博客拾起来了 来上海工作也已经有将近两周的时间了, 今天在整理项目的时候,遇到了一个关于参数注入的问题 背景: 我的开发前台用的是extjs4,在对后台spring mvc提交表单 ...
- FineReport父子格实现动态参数注入
"深入学习FineReport后发现其功能及其强大,之前使用存储过程实现的报表完全可以使用FineReport本身的功能实现. 当你需要的表名,查询条件等均未知的时候,使用"动态参 ...
- java学习笔记38(sql注入攻击及解决方法)
上一篇我们写了jdbc工具类:JDBCUtils ,在这里我们使用该工具类来连接数据库, 在之前我们使用 Statement接口下的executeQuery(sql)方法来执行搜索语句,但是这个接口并 ...
- spring的ioc依赖注入的三种方法(xml方式)
常见的依赖注入方法有三种:构造函数注入.set方法注入.使用P名称空间注入数据.另外说明下注入集合属性 先来说下最常用的那个注入方法吧. 一.set方法注入 顾名思义,就是在类中提供需要注入成员的 s ...
- Spring 参数注入
一个(类)Bean可能包含多种属性,这些属性怎么配置??? 见下: 用People Dog Cat Tiger Panda五个类来学习一些常用的=_= 重点在XML <!--基本类型注入- ...
- Spring3 MVC请求参数获取的几种方法
Spring3 MVC请求参数获取的几种方法 一. 通过@PathVariabl获取路径中的参数 @RequestMapping(value="user/{id}/{name}&q ...
- PHP函数可变参数列表的具体实现方法介绍
PHP函数可变参数列表可以通过_get_args().func_num_args().func_get_arg()这三个函数来实现.我们下面就对此做了详细的介绍. AD:2014WOT全球软件技术峰会 ...
随机推荐
- 智力大冲浪(洛谷P1230)
题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的?!接下来主持人宣布了比赛规则: ...
- 【BZOJ5063】旅游 Splay
[BZOJ5063]旅游 Description 小奇成功打开了大科学家的电脑. 大科学家打算前往n处景点旅游,他用一个序列来维护它们之间的顺序.初 始时,序列为1,2,...,n. 接着,大科学家进 ...
- Leetcode-Recover BST
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 用Python xlwt建立excel表格
1.下载xlwt的Python库 (This is a library for developers to use to generate spreadsheet files compatible w ...
- 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262
POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...
- Powershell Exchange Message Per Day Sent and Reveive
Powershell Exchange Message Per Day Sent and Reveive # Initialize some variables used for counting a ...
- php 字母和数字验证码
//验证码 <?php //实现简单的验证码 //session_start session_start(); //画布 $image = imagecreatetruecolor(100, 3 ...
- python系列十六:Python3 面向对象
#!/usr/bin/python #-*-coding:gbk-*- #Python3 面向对象 '''面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了 ...
- 第18章—后端分页(Mybatis)
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...
- XPath 快速入门
XPath 是一门在 xml 文档中查找信息的语言. XPath 使用路径表达式来选取 xml 文档中的节点或者节点集合. 路径表达式由元素和属性组成. 语法介绍 // 示例: // xml 文档 & ...