实体类 Student.java

package com.umgsai.spring.entity;
import java.util.Date;
public class Student {
private int id;
private String name;
private String sex;
private Date birthday;
public Student(int id, String name, String sex, Date birthday) {//构造函数
super();
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
//省略其他get、set方法
}

applicationContext.xml

<bean id="student" class="com.umgsai.spring.entity.Student">
<constructor-arg value="1"/>
<constructor-arg value="umgsai"/>
<constructor-arg value="M"/>
<constructor-arg ref="cur"/><!--引用-->
</bean>
<bean id="cur" class="java.util.Date"/>

测试注入

BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)factory.getBean("student");
System.out.println(student.getId()+":"+student.getName());

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1255907

Spring入门_03_构造注入的更多相关文章

  1. Spring入门_02_属性注入

    Spring 的set方法(属性)注入 UserAction类中设置属性和get.set方法.(实际上只需要set方法) private List list = null; private Set s ...

  2. Spring入门_04_注解注入

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  3. Spring 设值注入 构造注入 p命名空间注入

    注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...

  4. Spring入门(4)-注入Bean属性

    Spring入门(4)-注入Bean属性 本文介绍如何注入Bean属性,包括简单属性.引用.内部Bean.注入集合等. 0. 目录 注入简单值 注入引用 注入内部Bean 装配集合 装配空值 使用命名 ...

  5. Spring入门(2)-通过构造器注入Bean

    Spring入门(2)-通过构造器注入Bean 前一篇文章将了最基本的spring例子,这篇文章中,介绍一下带有参数的构造函数和通过构造器注入对象引用. 0. 目录 带有参数的构造函数 通过构造器注入 ...

  6. spring 构造注入 异常 Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments

    你可能在做项目的时候,需要在项目启动时初始化一个自定义的类,这个类中包含着一个有参的构造方法,这个构造方法中需要传入一些参数. spring提供的这个功能叫“构造注入”, applicationCon ...

  7. Spring注入值得2种方式:属性注入和构造注入

    Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...

  8. Spring接口编程_设值注入和构造注入

    说明: UserManagerImp是设值注入,UserManagerImp2是构造注入 接口不注入,也就是在Spring配置文件中没有接口的<bean>,但是定义的时候是用接口 priv ...

  9. 7.28.1 Spring构造注入还是设置注入

    1. 构造方法注入代码如下:public UserManagerImpl(UserDao userDao) {                                              ...

随机推荐

  1. 二叉树遍历Java实现

    [仅贴代码及测试结果] -------------------BinaryTree.java------------------------------ class Tree<E>{ E ...

  2. Trinity min_kmer_cov

    A high min_kmer value was used to reduce noise in the assembly and to identify only transcripts that ...

  3. SQLite3 学习笔记

    1.数据存储方式 Plist(NSArray\NSDictionary) Preference(偏好设置\NSUserDefaults) NSCoding(NSKeyedArchiver\NSkeye ...

  4. 【BZOJ-2599】Race 点分治

    2599: [IOI2011]Race Time Limit: 70 Sec  Memory Limit: 128 MBSubmit: 2590  Solved: 769[Submit][Status ...

  5. 【BZOJ-4435】Juice Junctions 最小割树(分治+最小割)+Hash

    4435: [Cerc2015]Juice Junctions Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 20  Solved: 11[Submi ...

  6. 【bzoj3110】 Zjoi2013—K大数查询

    http://www.lydsy.com/JudgeOnline/problem.php?id=3110 (题目链接) 题意 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在 ...

  7. CodeForces 37E Trial for Chief

    Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Description Having ...

  8. spring容器初始化执行某个方法

    在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. 比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出 ...

  9. C#用正则表达式对IP进行排序

    static void Main(string[] args) { string IPs = " 192.168.1.1 202.47.4.6 1.2.3.3 "; Console ...

  10. POJ 2240 - Arbitrage(bellman_ford & floyd)

    题意: 给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加. 举例说就是1美元经过一些兑换之后,超过1美元.可以输出Yes,否则输出No. 分析: 首先我们要把货币之 ...