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
Spring学习二----------IOC及Bean容器的更多相关文章
- spring 学习 二 IOC/DI
中文名称:控制反转 英文名称:( Inversion of Control ) 1 控制反转作用: 一般在编写java程序时,需要程序员自己创建对象的实例,例如 A a=new A();语句,就是程序 ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- Spring学习之Ioc控制反转(2)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
- Spring源码-IOC部分-Bean实例化过程【5】
实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
- Spring基础学习(二)—详解Bean(上)
在Spring配置文件中,用户不但可以将String.int等字面值注入Bean中,还可以将集合.Map等类型注入Bean中,此外还可以注入配置文件中其他定义的Bean. 一.字面值 ...
- Spring学习(二):Spring支持的5种Bean Scope
序言 Scope是定义Spring如何创建bean的实例的.Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0以后,又引入了另外三种scope ...
- Spring——IOC与Bean容器
[IOC] (1)IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护.也就是说由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中 (2)DI: ...
- Spring学习(二)--装配Bean
一.Spring装配机制 Spring提供了三种主要的装配机制: 1.在XML中进行显示配置 2.在Java中进行显示配置 3.隐式的bean发现机制和自动装配--自动化装配bean Spring可以 ...
随机推荐
- 解决ASP.NET裁剪图片失真
//有的时候剪切图片时,出现图片失真的问题,是因为图片质量下降造成的 //按照指定的数据画出画板(位图) System.Drawing.Image imgPhoto = System.Drawing. ...
- 转载——为Xamarin更好的开发而改写的库
本人现今一直奋战在Xamarin.Android,可能有人会疑惑Xamarin本身就是跨平台的,为什么不能直接跨IOS和Android,这个当然是最后的目标,只是现今你连Android都不能拿出符合商 ...
- maven工程开始
clipse中,maven工程,更新pom.xml文件后,会让你更新工程.快捷键是Alt + F5,也可以右键工程,Maven-->update project...,这样有个问题就是默认的JR ...
- HDU 2795.Billboard-完全版线段树(区间求最值的位置、区间染色、贴海报)
HDU2795.Billboard 这个题的意思就是在一块h*w的板子上贴公告,公告的规格为1*wi ,张贴的时候尽量往上,同一高度尽量靠左,求第n个公告贴的位置所在的行数,如果没有合适的位置贴则输出 ...
- APP专项测试 | 内存及cpu
命令: adb shell dumpsys meminfo packagename 关注点: 1.Native/Dalvik 的 Heap 信息 具体在上面的第一行和第二行,它分别给出的是JNI层和 ...
- Lowest Common Ancestor of a Binary Search Tree -- LeetCode
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [POI2014]Ant colony
题目大意: 给定一棵$n(n\le10^6)$个结点的树.在每个叶子结点,有$g$群蚂蚁要从外面进来,其中第$i$群有$m_i$只蚂蚁.这些蚂蚁依次爬树(一群蚂蚁爬完后才会爬另一群),若当前经过结点度 ...
- Runtime对象
Runtime简单概念: Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. * 这也是jvm实现跨平台的一个重要原因. * 可以通过 ge ...
- 【转】三种方式在C++中调用matlab
C/C++调用Matlab 在工程实践中,C/C++调用Matlab 的方法主要有调用Matlab 计算引擎.包含m 文件转 换的C/C++文件,以及调用m文件生成的DLL 文件. 1 利用Mat ...
- Git:fatal: The remote end hung up unexpectedly
一.配置公共密钥 https://help.github.com/articles/generating-ssh-keys/ 二.设置缓冲值(push文件较大时导致错误) \.git\config [ ...