Spring入门学习(二)

Spring入门学习(二)

Spring3-轻量级的控制反转(IoC)和面向切面(AOP)的容量框架

Spring3 软件包下载地址:http://projects.spring.io/spring-framework/

Spring3 IoC(Inversion of Control)的原理和主要组件

       Spring3框架的核心是实现了IoC模式的轻量级容器,IoC是Spring3的框架的核心。

1)      IoC的原理

控制反转是Spring框架的核心。所有的组件都是被动的,所有的组件的初始化和调用都由容器负责。简单来说就是由容器控制程序之间的关系,而非传统实现中由程序代码直接超控,即在一个类中调用另外一个类。这也就是控制反转的概念所在:控制权由应用代码中转到了外部容器。

2)      IoC的主要组件

Spring3框架的两个最基本和最重要的包是:org.springframework.beans.factory(该包中的主要接口是BeanFactory)和org.springframework.context包(该包中的主要接口是ApplicationFactory)。这两个包中的代码提供了Spring IoC特性的基础。Spring IoC框架的主要组件如下:

a)      Beans。

Beans是指项目中提供业务功能的Bean,即容器要管理的Bean。

b)     配置文件(beans.xml或者applicatonContext.xml)

Spring中通过配置文件对Bean进行管理。可命名为beans.xml或者applicationContext.xml,一般使用后者。格式如下。Spring IoC框架可以根据Bean的id从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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->

<bean id="myField"
        class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
    <property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</bean>
</beans>

当通过Spring3容器创建一个Bean实例时,不仅可以完成Bean的实例化,也可以为Bean制定作用域。Bean的作用域可以设置为如下:

1.      原型模式和单例模式:Spring3中的Bean默认情况下单例模式,容器分配Bean时,总是返回同一个实例;但是如果每次向ApplicationContext请求一个Bean时需要得到不同的实例,需将Bean定义为原型模式。可以使用<bean>元素的singleton属性设置,默认为true。

2.      Request和session:作用域是request和session。每次请求都会残生一个不同的Bean实例。

3.      Global session:共享一个Bean实例。

a)      BeanFactory接口及其相关类。

BeanFactory采用了工厂设计模式,即Bean容器模式,负责读取Bean的配置文件,管理对象的生成、加载,维护Bean对象之间的依赖关系,负责Bean对象的生命周期,对于简单的应用程序来说,使用BeanFactory就已经足够管理Bean,在对象的管理上就可以获得许多便利性。org.springframework.beans.factory.xml.XmlBeanFactory是BeanFactory常用的实例类(Spring3.1以后已被废弃使用)。其根据配置文件中的定义装载Bean。要创建XmlBeanFactory需要传递一个FileInputStream对象,该对象把XML文件提供给工厂。

BeanFactory的常用方法如下:

getBean(String name)

getBean(String name, Class requiredType)

b)     ApplicationContext接口及其相关类。

作为一个应用程序框架,只提供Bean容器管理的功能是不够的。若要利用Spring3所提供的一些高级容器功能可以使用ApplicationContext接口,该接口是提供高级功能的容器。

ApplicationContext的基本功能与BeanFactory很相似,但它还提供一下功能。

1.      提供访问资源文件的更方便的方法。

2.      支持国际化消息。

3.      提供文字消息解析的方法。

4.      可以发布事件,对事件感兴趣的Bean可以接受到这些事件。

ApplicationContext接口的常用实现类有三个。

1.      FileSystemXmlApplicationContext:从文件系统中的XML文件加载上下文中定义的信息。

2.      ClassPathXmlApplicationContext:从类路径中的XML文件加载上下文中定义的信息,把上下文定义的文件当成类路径资源。

3.      XmlWebApplicationContext:从web系统中的XML文件加载上下文中定义的信息。

例如:

测试项目结构图:


 hello.java

package ioc;

public class hello {

	private String a;
	private String b;
	public hello(){
		
	}
	public hello(String a, String b){
		this.a = a;
		this.b = b;
	}
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public String getB() {
		return b;
	}
	public void setB(String b) {
		this.b = b;
	}
	
}

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"
    xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->

<bean id="testHello" class="ioc.hello">
    <property name="a" value="test-a"/>
    <property name="b" value="test-b"/>
</bean>
</beans>

test.java

package ioc;

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

public class test {
	
	public static void main(String[] args){
		
		ApplicationContext context = new FileSystemXmlApplicationContext("C://Users/houshaoli/workspace/Spring-jar/bin/ioc/applicationContext.xml");
		ApplicationContext context1 = new ClassPathXmlApplicationContext("ioc/applicationContext.xml");
		hello testHello = (hello)context1.getBean("testHello");
		System.out.println(testHello.getA());
		
	}

}

3)      注入的两种方式

c)      设置注入

通过setter方法注入被调用者的实例。

上面的例子的applicationContext.xml即为该种注入方式。

d)     构造注入

利用构造方法来设置依赖注入的方式成为构造注入。

如下:

<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->

<bean id="testHello" class="ioc.hello">
    <constructor-arg value="test-C"></constructor-arg>
    <constructor-arg value="test-D"></constructor-arg>
</bean>
</beans>


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值