Spring学习二----------IOC及Bean容器

时间:2023-03-09 16:15:13
Spring学习二----------IOC及Bean容器

© 版权声明:本文为博主原创文章,转载请注明出处

接口

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

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

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

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

面向接口编程

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

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

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

实例:

1.OneInterface.java

package org.spring.ioc.interfaces;

/**
* 接口
*
*/
public interface OneInterface { String hello(String word); }

2.OneInterfaceImpl.java

package org.spring.ioc.interfaces.impl;

import org.spring.ioc.interfaces.OneInterface;

/**
* 接口实现类
*
*/
public class OneInterfaceImpl implements OneInterface { public String hello(String word) { return "Word form interface \"OneInterface\":" + word; } }

3.Main.java

package org.spring.ioc.main;

import org.spring.ioc.interfaces.OneInterface;
import org.spring.ioc.interfaces.impl.OneInterfaceImpl; public class Main { public static void main(String[] args) { OneInterface oif = new OneInterfaceImpl();//手动获取依赖对象
System.out.println(oif.hello("word."));
} }

IOC

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

DI(依赖注入)是IOC的一种实现方式

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

实例:

1.OneInterface.java(与上面一样)

2.OneInterfaceImpl.java(与上面一样)

3.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.spring</groupId>
<artifactId>Spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Spring</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.7.RELEASE</spring.version>
</properties> <dependencies>
<!-- junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>

4.spring-ioc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="oneInterface" class="org.spring.ioc.interfaces.impl.OneInterfaceImpl"/> </beans>

5.UnitTestBase.java

package org.spring.ioc.test;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils; /**
* 单元测试初始化类
*
*/
public class UnitTestBase { private ClassPathXmlApplicationContext context;
private String springXmlPath; /**
* 无参构造器
*/
public UnitTestBase() { } /**
* 含参构造器
*
* @param springXmlPath
* spring配置文件路径
*/
public UnitTestBase(String springXmlPath) { this.springXmlPath = springXmlPath; } /**
* 初始化spring配置文件
*/
@Before//在@Test注解的方法执行前执行
public void before() { if(StringUtils.isEmpty(springXmlPath)) {//默认spring配置文件路径
springXmlPath = "classpath*:spring-*.xml";
}
//加载配置文件
context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
//启动组件
context.start(); } /**
* 销毁spring组件
*/
@After//在@Test注解的方法执行后执行
public void after() { context.destroy();//销毁组件 } /**
* 获取spring中定义的bean实例
*
* @param beanId
*
* @return
*/
@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId) { return (T) context.getBean(beanId); } /**
* 获取spring中定义的bean实例
*
* @param clazz
*
* @return
*/
protected <T extends Object> T getBean(Class<T> clazz) { return (T) context.getBean(clazz); } }

6.TestOneInterface.java

package org.spring.ioc.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.spring.ioc.interfaces.OneInterface; @RunWith(BlockJUnit4ClassRunner.class)//指定JUnit默认执行类
public class TestOneInterface extends UnitTestBase { public TestOneInterface() {//通过构造方法传入spring配置文件路径 super("classpath*:spring-ioc.xml"); } @Test
public void testHello() { OneInterface oneInterface = super.getBean("oneInterface");//从spring容器中获取依赖对象
System.out.println(oneInterface.hello("Word.")); } }

Bean容器初始化

文件

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:/properties/spring-ioc.xml");

ClassPath

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-ioc.xml");

Web应用

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

参考:http://www.imooc.com/video/3665