【跟我学spring 4.0 】之第三节-bean的配置

配置bean的方式:

        -配置形式:基于XML文件的方式,基于注解的方式
    -Bean的配置方式:通过全类名(反射)、通过工厂方法(静态工厂方法&实例工厂方法)、FactoryBean

    -Ioc容器BeanFactory  & ApplicationContext概述

    -依赖注入方式:属性注入;构造器注入

    -注入属性值细节

    -自动装配

    -bean之间的关系:继承、依赖

    -bean的作用域:singleton;prototype;WEB 环境作用域

    -使用外部属性文件

    -spEL

    -IOC容器中Bean的生命周期

    -Spring 4.x 新特性:泛型依赖注入






从Ioc容器中获取Bean

调用ApplicationContext的getBean方法。




构造器方式注入

public class Car {
<div style="text-align: left;"><strong style="text-align: center; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;"></span></strong></div>
	private  String name;
	private String speed;
	private int price;
	public Car(String name, String speed, int price) {
		super();
		this.name = name;
		this.speed = speed;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [name=" + name + ", speed=" + speed + ", price=" + price + "]";
	}
	
}



 <!-- 使用构造器注入属性值可以指定参数的位置和类型	!以区分重载构造器 -->
 <bean id="car02" class="com.zd.runsharing.maven.model.Car">
 <constructor-arg value="丰田" type="java.lang.String"> </constructor-arg>
 <constructor-arg value="30码" type="java.lang.String"></constructor-arg>
 <constructor-arg  type="double">
 		<value>300000</value>
 </constructor-arg>
 </bean>

<!-- 可以使用proproty 的ref 建立bean属性之间的引用关系 -->
 <bean id="person" class="com.zd.runsharing.maven.model.Person">
    <property name="name" value="张三丰"></property>
    <property name="age" value="25"></property>
    <!-- <property name="car" ref="car02"></property> -->
    <!-- 内部bean -->
    <property name="car">
     <bean class="com.zd.runsharing.maven.model.Car">
		 <constructor-arg value="丰田-内部bean"></constructor-arg>
		 <constructor-arg value="29码"></constructor-arg>
		 <constructor-arg value="299990000"></constructor-arg>
 </bean>
    </property>
 </bean>






public class Person {
	private String name;
	private String age;
    private Car car;
    private List<Car> list;
	public Car getCar() {
		return car;
	}

	public List<Car> getList() {
		return list;
	}

	public void setList(List<Car> list) {
		this.list = list;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public String getName() {
		return name;
	}

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

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", car=" + car + ", list=" + list + "]";
	}
}


 <!-- 测试为集合属性赋值 -->
 <bean id="person02" class="com.zd.runsharing.maven.model.Person">
    <property name="name" value="张三丰"></property>
    <property name="age" value="25"></property>
    <property name="list">
    <!-- 使用list节点给list类型赋值 -->
    <list>
    <ref bean="car02"/>
    <bean class="com.zd.runsharing.maven.model.Car">
		 <constructor-arg value="丰田-002"></constructor-arg>
		 <constructor-arg value="29码"></constructor-arg>
		 <constructor-arg value="299990000"></constructor-arg>
 	</bean>
 	 <bean class="com.zd.runsharing.maven.model.Car">
		 <constructor-arg value="丰田-003"></constructor-arg>
		 <constructor-arg value="292码"></constructor-arg>
		 <constructor-arg value="299990000"></constructor-arg>
 	</bean>
    </list>
    </property>
 </bean>


public class MapPerson {
	private String name;
	private Map<String, Car> mapCar;

	@Override
	public String toString() {
		return "MapPerson [name=" + name + ", mapCar=" + mapCar + "]";
	}

	public String getName() {
		return name;
	}

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

	public Map<String, Car> getMapCar() {
		return mapCar;
	}

	public void setMapCar(Map<String, Car> mapCar) {
		this.mapCar = mapCar;
	}
}


<bean id="mapperson" class="com.zd.runsharing.maven.model.MapPerson">
		<property name="name" value="mapPerson-张三丰"></property>
		<property name="mapCar">
			<!-- 使用map节点给map类型赋值 -->
			<map>
				<entry key="map01">
					<ref bean="car02" />
				</entry>
				<entry key="map02">
					<ref bean="car02" />
				</entry>
			</map>
		</property>
	</bean>

  /* 测试为集合属性map赋值*/ 
	    	   MapPerson mapperson = (MapPerson) applicationContext.getBean("mapperson");
	    	   System.out.println("测试为集合属性map赋值:"+mapperson);

测试为集合属性map赋值:MapPerson [name=mapPerson-张三丰, mapCar={map01=Car [name=丰田, speed=30码, price=0, price_2=300000.0], map02=Car [name=丰田, speed=30码, price=0, price_2=300000.0]}]



测试properties

public class DataSourceProperties {

	private Properties properties;

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	@Override
	public String toString() {
		return "DataSourceProperties [properties=" + properties + "]";
	}
	
}

<!-- 测试properties配置 -->
	<bean id="DataSource" class="com.zd.runsharing.maven.model.DataSourceProperties">
		<property name="properties"  >
		<props >
		<prop key="user">root</prop>
		<prop key="password">123456</prop>
		<prop key="jdbcurl">jdbc:mysql</prop>
		</props>
		</property>
		 
	</bean>

/* 测试properties配置 赋值*/ 
	    	   DataSourceProperties DataSource = (DataSourceProperties) applicationContext.getBean("DataSource");
	    	   System.out.println("测试properties配置 :"+DataSource);
测试properties配置 :DataSourceProperties [properties={jdbcurl=jdbc:mysql, user=root, password=123456}]


<strong style="font-size:18px;"><</strong><span style="font-size:14px;">!--  配置单例的集合bean,以供多个bean进行引用,需要引入 util 命名空间 -->
 <util:list  id="carss">
	 <ref bean="car01"/>
	 <ref bean="car02"/>
 </util:list>
 <bean id="personUtilList" class="com.zd.runsharing.maven.model.Person">
  <property name="name" value="张三丰"></property>
    <property name="age" value="25"></property>
    <property name="list" ref="carss"></property>
 
 </bean></span>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:util="http://www.springframework.org/schema/util"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">

/*   配置单例的集合bean,以供多个bean进行引用,需要引入 util 命名空间*/
	    	   Person personUtilList = (Person) applicationContext.getBean("personUtilList");
	    	   System.out.println(" 配置单例的集合bean,以供多个bean进行引用,需要引入 util 命名空间:"+personUtilList);

配置单例的集合bean,以供多个bean进行引用,需要引入 util 命名空间:Person [name=张三丰, age=25, car=null, list=[Car [name=丰田, speed=30码, price=300000, price_2=0.0], Car [name=丰田, speed=30码, price=0, price_2=300000.0]]]

<!-- 通过P命名空间为bean的属性赋值,需要先导入p 命名空间 -->
	<bean id="personP" class="com.zd.runsharing.maven.model.Person" p:name="李四死死死" p:age="35"></bean>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:util="http://www.springframework.org/schema/util"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">

 /*   通过P命名空间为bean的属性赋值,需要先导入p 命名空间 */
	    	   Person person1P = (Person) applicationContext.getBean("personP");
	    	   System.out.println(" 通过P命名空间为bean的属性赋值,需要先导入p 命名空间 :"+person1P);


console: 通过P命名空间为bean的属性赋值,需要先导入p 命名空间 :Person [name=李四死死死, age=35, car=null, list=null]


=============================================================================================================================================================================================================================================================================================================================================================================================================




   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值