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参数注入的几个方法的更多相关文章

  1. struts2 参数注入 方法拦截器

    web.xml: <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=" ...

  2. 【原创】内核ShellCode注入的一种方法

    标 题: [原创]内核ShellCode注入的一种方法 作 者: organic 时 间: 2013-05-04,04:34:08 链 接: http://bbs.pediy.com/showthre ...

  3. spring mvc 复杂参数注入

    过了这么久,又重新把博客拾起来了 来上海工作也已经有将近两周的时间了, 今天在整理项目的时候,遇到了一个关于参数注入的问题 背景: 我的开发前台用的是extjs4,在对后台spring mvc提交表单 ...

  4. FineReport父子格实现动态参数注入

    "深入学习FineReport后发现其功能及其强大,之前使用存储过程实现的报表完全可以使用FineReport本身的功能实现. 当你需要的表名,查询条件等均未知的时候,使用"动态参 ...

  5. java学习笔记38(sql注入攻击及解决方法)

    上一篇我们写了jdbc工具类:JDBCUtils ,在这里我们使用该工具类来连接数据库, 在之前我们使用 Statement接口下的executeQuery(sql)方法来执行搜索语句,但是这个接口并 ...

  6. spring的ioc依赖注入的三种方法(xml方式)

    常见的依赖注入方法有三种:构造函数注入.set方法注入.使用P名称空间注入数据.另外说明下注入集合属性 先来说下最常用的那个注入方法吧. 一.set方法注入 顾名思义,就是在类中提供需要注入成员的 s ...

  7. Spring 参数注入

    一个(类)Bean可能包含多种属性,这些属性怎么配置???  见下: 用People  Dog Cat Tiger  Panda五个类来学习一些常用的=_= 重点在XML <!--基本类型注入- ...

  8. Spring3 MVC请求参数获取的几种方法

    Spring3 MVC请求参数获取的几种方法 一.      通过@PathVariabl获取路径中的参数 @RequestMapping(value="user/{id}/{name}&q ...

  9. PHP函数可变参数列表的具体实现方法介绍

    PHP函数可变参数列表可以通过_get_args().func_num_args().func_get_arg()这三个函数来实现.我们下面就对此做了详细的介绍. AD:2014WOT全球软件技术峰会 ...

随机推荐

  1. db2 相关命令

    db2ilist  ---列出主机所有实例 db2icrt  instanceName   ---创建实例名为instanceName的实例 db2idrop  instanceName   ---删 ...

  2. CentOS 7 64位 安装 PostgreSQL 9.2整理

    centos版本 LSB Version: :core-4.1-amd64:core-4.1-noarchDistributor ID: CentOSDescription: CentOS Linux ...

  3. mac下面安装mysql

    参考http://www.cnblogs.com/lakeslove/p/6280404.html 关于msyql5.7,安装时最大的改变就是有了一个默认密码 我安装的是mysql-5.7.17-ma ...

  4. 存一些可能会用得到的vue的UI框架

    VUX 项目主页:https://vux.li/#/ github地址:https://github.com/airyland/vux element UI(饿了么后台) Element 是由饿了么U ...

  5. 巨蟒python全栈开发-第20天 核能来袭-约束 异常处理 MD5 日志处理

    一.今日主要内容 1.类的约束(对下面人的代码进行限制;项目经理的必备技能,要想走的长远) (1)写一个父类,父类中的某个方法要抛出一个异常 NotImplementedError(重点) (2)抽象 ...

  6. 传说中的MATLAB1.0(1984年DOS界面182K、运行良好)附下载

    传说中的MATLAB1.0(1984年DOS界面182K.运行良好)附下载 你见过 MATLAB 最早的版本吗?下载一份 MATLAB 1.0,(只有182K, 只有20来个函数) 体会一下吧.MAT ...

  7. Pycharm选中代码无法Backspace直接删除

    现象 如图,选中代码后,按习惯来说,直接Backspace就可以删除被选中代码. 但选中后再按“Backspace”,只会改变(扩选)选中范围. 解决 点击“工具栏”里的“Tool”,去掉勾选的Vim ...

  8. Notepad++ 更换主题

    到Textmate Theme Directory下载主题: 用文本编辑器把它打开,复制所有代码,贴到theme converter page里,然后“Download”: 保存: 在Notepad+ ...

  9. centos7 只需两步安装rabbitmq

    # yum -y install rabbitmq-server # systemctl start rabbitmq-server  && systemctl enable rabb ...

  10. 面向对象封装 classmethod和staticmethod方法

    接口类 接口类:是规范子类的一个模板,只要接口类中定义的,就应该在子类中实现接口类不能被实例化,它只能被继承支持多继承接口隔离原则:使用多个专门的接口,而不使用单一的总接口.即客户端不应该依赖那些不需 ...