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全球软件技术峰会 ...
随机推荐
- 02、微信小程序的数据绑定
02.微信小程序的数据绑定 目录结构: 模板内容: 使用bindtap绑定事件 <!--index.wxml--> <view class="container" ...
- word2007的配置进度怎么产生的?如何解决?
那么要怎么解决这个问题呢?既然是安装的,那么我们便道安装控制器文件夹下面去找原因.在WIN8操作系统下,文件夹位于:C:\Program Files (x86)\Common Files\Micros ...
- IIS设置文件 App_Offline.htm 网站维护
在ASP.NET 2.0 站点根目录下,只要存在 App_Offline.htm 文件,那么所有对.aspx的请求都将转向App_Offline.htm .而且浏览器的地址栏显示的是所请求的.aspx ...
- 我的Android进阶之旅------>解决Android Studio编译后安装apk报错:The APK file does not exist on disk
1.错误描述 今天用Android Studio编译应用后安装APK的时候,报错了,错误如下所示: The APK file build\outputs\apk\OYP_2.3.4_I2Base_64 ...
- hibernate detached分离查询 与 抓取策略注意事项
1.detached在抓取策略为 jion显式左外连接查询情况下 会产生笛卡儿积现象 DetachedCriteria dc = DetachedCriteria.forClass(Topic.cla ...
- 什么是EJB
学习EJB可以加深对J2EE平台的认识. 百科定义EJB: 被称为java企业bean,服务器端组件,核心应用是部署分布式应用程序.用它部署的系统不限定平台.实际上ejb是一种产品,描述了应用组件要解 ...
- struts 文件下载 annotation 注解版
[本文简介] 本文将简单介绍使用 struts2 ,通过零配置和 annotation 实现文件下载功能. [文件夹结构] [web.xml有关struts的配置] <filter> &l ...
- 004-shiro简介
一.什么是shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和 ...
- python中unicode和str的组合
python中unicode对象和str对象拼接在一起,会自动将str对象转换成unicode对象 即:a="aa" b=u"bb" c=a+b type(c) ...
- HDU1069:Monkey and Banana(最长上升子序列的应用)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 这题挺简单的,给定一个箱子的长宽高,要求啰箱子,但必须保证下面箱子的长和宽必须大于上面的箱子. 一个 ...