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. JZOJ.5257【NOIP2017模拟8.11】小X的佛光

    Description

  2. Java版接口自动化--初稿

    一.接口参数的获取: 1.参数通过Excel读取,并将结果写入Excel中 package org.fanqi.operateExcel; import java.io.FileInputStream ...

  3. C#自动给文章关键字加链接实现代码

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  4. 解决Windows 7 IIS7.5 用户 'IIS APPPOOL\{站点名} AppPool'登录失败

    今天调试程序的时候,使用VS调试没有任何问题,但是发布到IIS就发生错误了,网上搜索了一下,问题具体上就出在IIS的应用程序池的设置上.我使用的是Windows7 IIS7.5. 错误为:用户 'II ...

  5. 如果"一切是IO"“一切是file”是成立的,那么上述的想法也一定可以实现吧 awk对apache日志分析 ---

    定时执行 自动化处理 直接入库 再去读取这个file入库: root@VM---ubuntu:/var/log/apache2# awk '{print $1 "\t" $7}' ...

  6. maven安装,maven命令行使用

    1 下载maven,解压(无需安装),配置环境变量,命令行mvn -v测试. 2 maven常用命令:https://www.cnblogs.com/wkrbky/p/6352188.html 3 注 ...

  7. LeetCode232:Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) – Push element x to the back of ...

  8. DBCC SHRINKFILE收缩日志/收缩数据库/收缩文件

    DBCC SHRINKFILE 收缩相关数据库的指定数据文件或日志文件大小. 语法 DBCC SHRINKFILE    ( { file_name | file_id }        { [ ,t ...

  9. corethink功能模块探索开发(十六)后台搜索功能

    效果图: 代码很简单,就是添加搜索框,搜索字段,在初始化页面查询的时候添加查询条件. 1.添加搜索框 添加到删除按钮后边. ->setSearch('请输入设备名称/MAC/宿舍号', U('i ...

  10. CoreThink开发(十三)增加页面加载动画

    效果: 加载动画是由jquery和fakeloader这个js库实现的. 其实这个也可以做成一个插件,用数据库记录是否开启,选择动画的样式,那样扩展性会更好. 源码资源已经上传在我的csdn下载中. ...