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. cocos2d-X学习之主要类介绍:动作:CCAction

    引用自:http://www.cnblogs.com/lhming/archive/2012/07/01/2572238.html 类继承图: 主要函数: virtual CCObject *  co ...

  2. CodeForces 667A Pouring Rain

    A. Pouring Rain time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. StackExchange.Redis性能调优

    大家经常出现同步调用Redis超时的问题,但改成异步之后发现错误非常少了,但却可能通过前后记日志之类的发现Redis命令非常慢. PS: 以后代码都在Windows bash中运行,StackExch ...

  4. 记录--关于Jquery uploadify 不能动态传值的问题(java)

    动态传值纠结多时后无效, 后得下面一番代码,依旧无效~~ 纳了几个闷,心灰意冷下   清理了 tomcat 一次 再出运行   可以了 真心纠结很久很久   无奈之下还是得  清理清理tomcat: ...

  5. submit按钮修改宽高的坑

    近些天对h5非常感兴趣,边工作边学习,虽然比较累,但过得很踏实.每天都要学习一点东西,这样才能对得起自己.好了,废话不多说,进入今天的主题. 今天遇到了一个非常有趣的东西,就是在修改submit按钮的 ...

  6. vue中的项目目录assets和staitc的区别

    vue中的项目目录assets和staitc的区别 在进行发行正式版时,即为npm run build编译后, assets下的文件如(js.css)都会在dist文件夹下面的项目目录分别合并到一个文 ...

  7. java final 关键词

    package day9; /** * Created by admin on 2018/11/17. * final可以修饰类,方法,变量 特点: final可以修饰类,该类不能被继承. final ...

  8. JS的深拷贝

    var obj = { name: "wuyongyu", age: 18 } 第一种方式: function deepClone(obj){ // 判断传入的数据类型 - 数组或 ...

  9. rem根元素的定义规则

    <script> !function(a) { function b() { var b = g.getBoundingClientRect().width; var winpix=1; ...

  10. (转载)处理SQL解析失败导致share pool 的争用

    通过关联x$kglcursorx$kglcursor_child_sqlid视图: 通过使用Oracle10035Event事件可以找到解析失败的SQL: 通过oraclesystemdump也可以找 ...