Spring框架自学之路(三)

(03Day)

        (1)Bean的自动装配

            在前面的例子中我们为Bean装配Bean很明显都是手动装配的,那么既然有手动装配那有没有自动装配呢?显然是有的。那么自动装配又该如何使用呢?其实很简单,来看看吧。

            为Bean设置自动装配只需要在Bean中设置autowire并指定自动装配的模式就行了。

            atuowire有三种装配模式分别是:

 

  1. byType:根据类型进行自动装配,若IOC容器中有多个目标与目标Bean类型一致,这种情况下会抛出异常。
  2. byName:根据名称完成自动装配:必须将目标Bean的属性名和名称设置的完全相同,IOC会根据Get方法获取。
  3. constructor:通过构造方法进行自动装配,当Bean中存在多个构造方法的时候,这种配置方法会很复杂,因此不推荐使用。

            在spring中的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "address" class="com.autowire.Address" p:city="厦门" p:street="集美"></bean>
<bean id = "car" class="com.autowire.Car" p:brand="奥迪" p:price="300000"></bean>
<!-- 手动装配 
<bean id = "person" class="com.autowire.Person" p:name="李四" p:car-ref="car" p:address-ref="address"></bean>
-->
<!-- 使用autowire自动装配,属性指定自动装配的方式
	 byNmae是根据该Bean的名字和当前 Bean的setter方法除去set后的方法名进行自动装配,若没有匹配的方法,则不装配
	 byType是根据该Bean的类型和当前Bean的属性的类型进行自动装配。注意:若IOC容器中有一个以上的该类型匹配的bean,则抛出异常。
 -->
<bean id = "person" class="com.autowire.Person" p:name="李四" autowire="byName"></bean>
</beans>

             测试类

package com.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main4 {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
		Person person = (Person) ctx.getBean("person");
		System.out.println(person);
	}

}

 

        1.一旦使用自动装配,那么该Bean所有的引用属性都必须使用自动装配,不能有的使用自动装配,有的使用手动装配。

 

        2.要么根据类型自动装配,要么根据属性名自动装配,不能两者同时使用。

        3.一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些。

    (2)Bean之间的关系

                2-1 继承

                    说到继承,也许会想到我们java中面向对象的继承,但是在这继承并不是指java中面向对象的父类子类继承,而是

                    指配置上的继承。在spring中Bean的继承其实很简单,主要就是在Bean的元素中添加parent = “继承的Bean”

                    来看看代码吧。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 不使用继承的方式 
		<bean id = "address1" class="com.autowire.Address"
		 p:city="厦门" p:street="集美区"></bean>
		<bean id = "address2" class="com.autowire.Address"
		p:city="厦门" p:street="海沧区"></bean>
	-->
	<!-- 使用继承的方式 parent 代表要继承的Bean 同时也可以继承模版,模版就是Bean 加上abstract = “true” -->
		<bean id = "address1" class="com.autowire.Address"
		 p:city="厦门" p:street="集美区"></bean>
		 <!-- 继承address1 Bean -->
		<bean id = "address2" parent="address1"
		p:street="海沧区"></bean>
		<!-- 定义一个模版,模版上可以不写class路径,也可以写,同时abstract为true的bean是不会被
			 IOC容器初化的,只能用来被继承配置。同理若一个Bean的Class没有指定,则该Bean必须是一个
			 抽象Bean。
		 -->
		<bean id = "address" p:city="厦门"  p:street="集美区" abstract="true"></bean>
		<!-- 继承模版 并不是所有的元素的都会被继承 例如 abstract就不会被继承 -->
		<bean id = "address3" class="com.autowire.Address" parent="address"
		p:street="思明区"></bean>
</beans>

                   2-2 依赖

                        那么何为依赖呢,通俗来说就是Bean1依赖于Bean2,那么Bean1创建的时候在IOC容器中必须要存在Bean2。

                        废话不多说,具体细节用代码来说明。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 不实用继承的方式 
		<bean id = "address1" class="com.autowire.Address"
		 p:city="厦门" p:street="集美区"></bean>
		<bean id = "address2" class="com.autowire.Address"
		p:city="厦门" p:street="海沧区"></bean>
	-->
	<!-- 使用继承的方式 parent 代表要继承的Bean 同时也可以继承模版,模版就是Bean 加上abstract = “true” -->
		<bean id = "address1" class="com.autowire.Address"
		 p:city="厦门" p:street="集美区"></bean>
		 <!-- 继承address1 Bean -->
		<bean id = "address2" parent="address1"
		p:street="海沧区"></bean>
		<!-- 定义一个模版,模版上可以不写class路径,也可以写,同时abstract为true的bean是不会被
			 IOC容器初化的,只能用来被继承配置。同理若一个Bean的Class没有指定,则该Bean必须是一个
			 抽象Bean。
		 -->
		<bean id = "address" p:city="厦门"  p:street="集美区" abstract="true"></bean>
		<!-- 继承模版 并不是所有的元素的都会被继承 例如 abstract就不会被继承 -->
		<bean id = "address3" class="com.autowire.Address" parent="address"
		p:street="思明区"></bean>
		
		<!-- 依赖Bean的配置 此时若是car没在IOC容器中定义,那么运行程序的时候会抛出异常
			 所以说依赖Bean就是配置Person的时候,必须有一个car与之关联,也就是说person这个
			 Bean依赖与Car这个Bean。依赖就是为了让创建person这个Bean之前IOC容器中必须存
			 在Car这个Bean,如果需要依赖多个Bean可以通过逗号或者空格的方式配置。
		 -->
		 <bean id = "car" class="com.autowire.Car" 
		 p:brand="奔驰" p:price="5000000"></bean>
		<bean id = "person" class="com.autowire.Person" 
		p:name="Garon" p:address-ref="address3" depends-on="car"></bean>
</beans>   

              测试类

package com.relation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.autowire.Address;
import com.autowire.Person;

public class Main5 {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
		Address address = (Address) ctx.getBean("address1");
		System.out.println(address);
		address = (Address) ctx.getBean("address2");
		System.out.println(address);
		address = (Address) ctx.getBean("address3");
		System.out.println(address);
		Person person = (Person) ctx.getBean("person");
		System.out.println(person);
	}

}

 

    (3)Bean的作用域

 

            默认情况下在IOC容器下配置的Bean是单例模式的,也就是说IOC容器只会为这个Bean节点创建一个实例对象,每次调用GetBean方法都会返回同一个Bean。

            那么如果配置Bean 的作用域呢?只需要在Bean的属性中配置 scope元素就行了,scope元素有以下4个元素:

                 1)singleton:单利的,默认值,容器在初始化的时候创建bean实例,整个容器的生命周期只创建这一个Bean。
 2)prototype:原型的,容器在初始化的时候不创建Bean,在每一次请求的时候都会创建一个新的实例并返回。
 3)request:每次http请求将会有各自的bean实例,类似于prototype。

 4)session:在一个http session中,一个bean定义对应一个bean实例。

                话不多说上代码。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 使用Bean的scope属性来配置bean的作用域
		 singleton:单利的,默认值,容器在初始化的时候创建bean实例,整个容器的生命周期只创建这一个Bean。
		 prototype:原型的,容器在初始化的时候不创建Bean,在每一次请求的时候都会创建一个新的实例并返回。
		 request:每次http请求将会有各自的bean实例,类似于prototype。
		 session:在一个http session中,一个bean定义对应一个bean实例。
	 -->
	<bean id= "car1" class="com.autowire.Car"
	p:brand="宝马" p:price="350000" scope="prototype"></bean>
	
</beans>

        测试类

package com.scope;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.autowire.Car;

public class Main6 {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
		Car car1 = (Car) ctx.getBean("car1");
		Car car2 = (Car) ctx.getBean("car1");
		System.out.println(car1 == car2);
	}

}

    (4)使用外部属性文件

            在实际开发中我们的spring配置文件会非常的复杂,那么如果我们需要修改系统部署的细节信息,是不是要在spring配置文件中找到对应的文件进行修改呢?很明显这样做非常的麻烦而且容易出错,于是spring引入了外部属性文件这种配置方法使得这些部署的细节和Bean配置相分离。

            那么如何做呢?需要在src目录下创建一个后缀为properties的文件,并在里面配置好信息后,只需要在spring的配置文件中导入context:properties这个命名空间使用<context:properties-placeholder location=“classpath:外部文件的名字”>就可以了。这样在Bean的配置文件就可以使用类似于EL表达式的方式${var}取值.

            外部属性文件类似于这样写

这样如果要取user这个值只需要再对应的value中使用${user}就可以取到root这个值,以后需要修改基础的配置信息,只需要到外部属性文件里修改就行了,这样是不是更方便且不容易出错。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值