property和constructor-arg的使用

(标注依稀)由于好久没去看spring的东西,以前的知道差不多都忘记了,今天重新学习一下如何使用,以后在慢慢的一个一个知识点的深究。我发现学过的东西还是要记录下来,否则以后又要重新找资料。纯属个人学习笔记。

一:依赖注入的方式

constructor-arg:通过构造函数注入。

property:通过setxx方法注入。

二:constructor-arg的简单使用

java代码

    public class Man {

	private String name ;
	private int age;
	private List hobby;
	private Map  friends;
	private Set  set;
	private boolean ifMarried;

	public Man() {

	}

    public Man(String name, int age,List hobby,Map friends,Set    set,boolean ifMarried){
    	this.name = name;
    	this.age = age;
    	this.hobby = hobby;
    	this.friends = friends;
    	this.set = set;
    	this.ifMarried = ifMarried;
    }

    public String getInfo(){

    	String info = "姓名:"+this.name+"\n年龄:"+this.age+"\n爱好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;
        return info;
    }

}

xml配置文件

	<bean id="man" class="com.spring.test.man.Man">
	   <constructor-arg value="zzy" index="0" >
	   </constructor-arg>

	   <constructor-arg value="10" index="1">
	   </constructor-arg>

	   <constructor-arg>
	     <list>
	        <value>movie</value>
	        <value>music</value>
	     </list>
	   </constructor-arg>

	   <constructor-arg>
	      <set>
	         <value>Lady is GaGa</value>
	         <value>GaGa is Lady</value>
	      </set>
	   </constructor-arg>

	   <constructor-arg>
	       <map>
	          <entry key="liuhua" value="man"></entry>
	          <entry key="xujinglei" value="female"></entry>
	       </map>
	   </constructor-arg>

	   <constructor-arg index="5" value="0">
	   </constructor-arg>
	</bean>

最后一个参数ifMarried是一个boolean类型的参数,在配置的时候可以是true/false或者0/1;

二:property的简单使用

java代码:

public class Doctor {

	private String name;
	private String sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public void init(){
		System.out.println("88888888888");
	}
	public void init(String name,String sex){
		this.name = name;
		this.sex = sex;
	}
}

xml配置文件:

  <bean id="doctor" class="com.spring.test.man.Doctor" init-method="init">
	  <property name="name" value="doctor"></property>
	  <property name="sex" value="i don't know"></property>
	</bean>

在这里我配置了一个init-method="init"表示在容易实例化这个doctor的时候,调用一个Doctor类的init方法,本来还以为可以通过这个init方法来注入要注入的信息,但是尝试过后才知道这个init方法是不能带参数的。

时间: 2024-07-30 05:11:52

property和constructor-arg的使用的相关文章

Spring约束

时间:2017-1-29 02:01 Appendix D. XML Schema-based configuration Prev Part VII. Appendices Next Appendix D. XML Schema-based configuration D.1 Introduction This appendix details the XML Schema-based configuration introduced in Spring 2.0 and enhanced an

深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap[转]

上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select.resultMap的用法.select无疑是我们最常用,也是最复杂的,mybatis通过resultMap能帮助我们很好地进行高级映射.下面就开始看看select 以及 resultMap的用法: 先看select的配置吧: <select <!-- 1. id (必须配置) id是命名空间中的

深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good

上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select.resultMap的用法.select无疑是我们最常用,也是最复杂的,mybatis通过resultMap能帮助我们很好地进行高级映射.下面就开始看看select 以及 resultMap的用法: 先看select的配置吧: <select <!-- 1. id (必须配置) id是命名空间中的

mapper映射文件配置之select、resultMap(转载)

原文地址:http://www.cnblogs.com/dongying/p/4073259.html 先看select的配置吧: <select         <!-- 1. id (必须配置)         id是命名空间中的唯一标识符,可被用来代表这条语句.         一个命名空间(namespace) 对应一个dao接口,         这个id也应该对应dao里面的某个方法(相当于方法的实现),因此id 应该与方法名一致 -->        id="se

MyBatis关联查询

首先在数据库bookstore中建立三张表,分别是BSuser,author,reader CREATE TABLE `author` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `realName` varchar(20) COLLATE utf8_bin DEFAULT NULL,   `userID` int(11) DEFAULT NULL,   `IDCard` varchar(20) COLLATE utf8_bin DEFAULT NUL

Attribute操作的性能优化方式

Attribute是.NET平台上提供的一种元编程能力,可以通过标记的方式来修饰各种成员.无论是组件设计,语言之间互通,还是最普通的框架使用,现在已经都离不开Attribute了.迫于Attribute的功能的重要性(Kent Beck认为NUnit比早期JUnit设计的好,一个主要方面便是利用了Attribute),Java语言也在5.0版本中引入了与Attribute类似的Annotation概念.不过Attribute说到底也是一种反射操作,平时正常使用不会带来问题,但是密集的调用还是对性

深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap

上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select.resultMap的用法.select无疑是我们最常用,也是最复杂的,mybatis通过resultMap能帮助我们很好地进行高级映射.下面就开始看看select 以及 resultMap的用法: 先看select的配置吧: <select <!-- 1. id (必须配置) id是命名空间中的

Google JavaScript Style Guide

转自:http://google.github.io/styleguide/javascriptguide.xml Google JavaScript Style Guide Revision 2.93 Aaron Whyte Bob Jervis Dan Pupius Erik Arvidsson Fritz Schneider Robby Walker Each style point has a summary for which additional information is ava

spring beans源码解读之--bean definiton解析器

spring提供了有两种方式的bean definition解析器:PropertiesBeanDefinitionReader和XmLBeanDefinitionReader即属性文件格式的bean definition解析器和xml文件格式的bean definition解析器. 我们先从简单的PropertiesBeanDefinitionReader开始深入挖掘. 1. PropertiesBeanDefinitionReader 属性文件bean definition解析器 1.1  

译:Spring框架参考文档之IoC容器(未完成)

6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, o