您的位置:首页 > 编程语言 > Java开发

springIOC及Bean容器(2)

2016-01-15 17:17 375 查看

2016/1/15 11:39:07

1.接口

用于沟通的中介物的抽象化

实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其它实体与其交互的方式

对应Java接口即声明,声明了哪些方法是对外公开提供的

在Java8中,接口可以拥有方法体

2.面向接口编程

结构设计中,分清层次及调用关系,每层只向外(上层)提供一组功能接口,各层间仅依赖接口而非实现类

接口实现的变动不影响各层间的调用,这一点在公共服务中显得尤为重要

“面向接口编程”中的”接口”是用于隐藏具体实现和实现多态性的组件

package com.zjx.interfaces;

/**

@author acer

接口声明

*

*/

public interface IOneInterface {

public String hello(String words);

}

package com.zjx.interfaces.impl;

import com.zjx.interfaces.IOneInterface;

/**
* @author acer
* 被隐藏的接口实现类
*
*/
public class OneInterfaceImpl implements IOneInterface {

@Override
public String hello(String words) {
return "Word from interface\"IOneInterface\":"+words;
}

}


package com.zjx.interfaces.test;

import com.zjx.interfaces.IOneInterface;
import com.zjx.interfaces.impl.OneInterfaceImpl;

public class Test {
public static void main(String[] args) {
IOneInterface iyf = new OneInterfaceImpl();
System.out.println(iyf.hello("word..."));
}
}


结果:Word from interface”IOneInterface”:word…

3.什么是IOC

IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护,只是负责使用

DI(依赖注入):是其一种实现方式

目的:创建对象并且组装对象之间的关系


业务对象进入Spring容器,通过配置的元数据,来生产出符合系统需要的对象

2004年,Martin Fowler探讨了通过一个问题,既然IOC是控制反转,那么到底是“哪些方面的控制被反转了呢?”,经过详细的分析和论证后,他得出答案:“获得依赖对象的过程被反转了”。控制被反转之后,获得依赖对象的过程由自身管理变味了由IOC荣日主动注入。于是,他给控制反转去了一个更合适的名称叫做依赖注入(Dependency Injection)。他的答案实际上给出了实现IOC的方法,注入,所谓依赖注入,就是由IOC容器在运行期间,动态的将某种依赖关系注入到对象之中。

4.Spring的Bean配置

在Spring中,它把所有的对象都称作为Bean

Spring的配置有两种:1.基于XML的配置;2.注解方式

<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
<bean id="OneInterface" class="com.zjx.interfaces.impl.OneInterfaceImpl"></bean>

</beans>


5.单元测试

下载junit-*.jar并引入工程

创建UnitTestBase类,完成对Spring配置文件的加载/销毁

所有的单元测试类都继承自UnitTestBase,通过它的getBean方法获取想要得到的对象

子类(具体执行单元测试的类)加注解:@RunWit(BlockJUnit4ClassRunner.class)

单元测试方法加注解:@Test

右键选择要执行的单元测试方法执行或者执行一个类的全部单元测试方法

首先书写UnitTestBase类

package com.zjx.interfaces.test;

import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

public class UnitTestBase {
/**
* 相对路径应用上下文
*/
private ClassPathXmlApplicationContext context;

/**
* XML文件的存放路径
*/
private String springXmlPath;

public UnitTestBase() {

}

public UnitTestBase(String springXmlPath){
this.springXmlPath = springXmlPath;
}
@Before
public void before(){
if (StringUtils.isEmpty(springXmlPath)) {
springXmlPath = "classpath*:spring-*.xml";
}
try {
// xml文件用逗号或者空格符隔开,均可加载
context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
context.start();
} catch (BeansException e) {
e.printStackTrace();
}
}
@After
public void after(){
context.destroy();
}

@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId){
try {
return(T)context.getBean(beanId);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}

protected <T extends Object> T getBean(Class<T> clazz){
try {
return context.getBean(clazz);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
}


测试上面的IOneInterface接口

package com.zjx.interfaces.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.zjx.interfaces.IOneInterface;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestOneInterface extends UnitTestBase{

public TestOneInterface() {
// 通过父类构造方法传入XML配置文件相对路径
super("classpath*:spring-ioc.xml");
}

@Test
public void test(){
IOneInterface oneInterface = super.getBean("oneInterface");
System.out.println(oneInterface.hello("this is a test"));
}
}


6.Bean容器初始化

基础:两个包

-org.springframework.beans

-org.springframework.context

-BeanFactory提供配置结构和基本功能,加载并初始化Bean

-ApplicationContext保存了Bean对象并在Spring中被广泛使用

方式,ApplicationContext

-本地文件

-Classpath

-Web应用中依赖servlet或Listener

在Web应用中的依赖

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-action.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 设计 spring