Spring课程 Spring入门篇 2-1 IOC和bean容器
本节讲了5部分内容,6为项目demo:
1 接口及面向接口编程
2 什么是IOC
3 Spring的bean配置
4 Bean的初始化
5 Demo
自己理解:
1 高层模块和底层模块都依赖于他们共同的接口,而不是高层模块依赖于底层模块进行开发
2 IOC 控制反转,控制权的转移,将产生对象的过程反转了过来。本来由高层模块new对象,现在将创建对象的任务交给了外部容器。实现方式是依赖注入DI
3 参见demo2,spring-ioc.xml中bean的配置
对spring bean的使用(注入)有两种方式,一种是xml配置,一种是注解的方式,本节主要采用xml的方式
4 基础两个包:org.springframework.beans 和 org.springframework.context
方式:ApplicationContext,参见基类
1 加载文件
FileSystemXmlApplicationContext context1 = new FileSystemXmlApplicationContext("F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-ioc.xm");
2 加载路径
ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
3 web应用
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
1 接口及面向接口编程
1.1 java8拥有方法体
1.2 接口只声明不实现
1.3 接口只能有声明,抽象类既可以有声明,也可以有实现;类中只能实现
6 Demo
共两个案例,
Demo1:
1 介绍面向接口编程的Demo
Demo2:
1 控制反转应用演示 (产生对象的过程由容器负责,不再由类去创建)
注意事项:spring-*.xml必须放在项目的classpath下(右击项目==》project==》buildpath==》source==》最下边是项目的默认classpath路径),否则不加载
Demo1:
测试类:
package com.imooc.bean.ioc.interfaces;
public class Main {
/**
* 面向接口编程的简单说明,用接口声明,
* 将接口的实现类赋值给对象的声明,然后进行调用
* @param args
*/
public static void main(String[] args) {
OneInterface oif = new OneInterfaceImpl();
System.out.println(oif.hello("word."));;
}
}
接口:
package com.imooc.bean.ioc.interfaces;
public interface OneInterface {
String hello(String word);
}
实现类:
package com.imooc.bean.ioc.interfaces;
public class OneInterfaceImpl implements OneInterface{
@Override
public String hello(String word) {
// TODO Auto-generated method stub
return "Word form interfacce\"OneInterface\":"+word;
}
}
Demo2:
测试类:
package com.imooc.test.ioc.interfaces; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.bean.ioc.interfaces.OneInterface;
import com.imooc.bean.ioc.interfaces.OneInterfaceImpl;
import com.imooc.test.base.UnitTestBase;
/**
* The Question We Need To Focus:
* 1 ioc-spring.xml的路径必须放在classpath下边,否则无法加载xml
* @author weijingli
*
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class TestOneInterface extends UnitTestBase { public TestOneInterface(){
super("classpath*:spring-ioc.xml");
} @Test
public void testHello() {
// TODO Auto-generated method stub
OneInterface oif = super.getbean("oneInterface");
System.out.println(oif.hello("myInput"));
} }
接口:
package com.imooc.bean.ioc.interfaces;
public interface OneInterface {
String hello(String word);
}
实现类:
package com.imooc.bean.ioc.interfaces;
public class OneInterfaceImpl implements OneInterface{
@Override
public String hello(String word) {
// TODO Auto-generated method stub
return "Word form interfacce\"OneInterface\":"+word;
}
}
基类:
package com.imooc.test.base; import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
1 加载文件
FileSystemXmlApplicationContext context1 = new FileSystemXmlApplicationContext("F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-ioc.xm");
2 加载路径
ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
*/
public class UnitTestBase {
//spring-core的jar包下
private ClassPathXmlApplicationContext context;
private String springXmlPath;
public UnitTestBase(){
}
//初始化该类,刚进来时传入xml路径
public UnitTestBase(String springXmlPath){
this.springXmlPath = springXmlPath;
}
//StringUtils在common-lang-2.4包下
@Before
public void before(){
if(StringUtils.isEmpty(springXmlPath)){
springXmlPath="classpath*:spring-*.xml";
}
try{
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) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
protected <T extends Object> T getbean(Class <T> clazz){
return context.getBean(clazz);
}
}
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"
default-init-method="init" default-destroy-method="destroy"> <bean id="oneInterface" class="com.imooc.bean.ioc.interfaces.OneInterfaceImpl"></bean> </beans>
pom.xml(maven通用)
<?xml version="1.0" encoding="UTF-8"?> <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>com.ddwei</groupId>
<artifactId>ddwei-dao</artifactId>
<version>0.0.1-SNAPSHOT</version> <name>ddwei-dao</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Spring课程 Spring入门篇 2-1 IOC和bean容器的更多相关文章
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- Spring实践系列-入门篇(一)
本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...
- Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期
课程链接: 本节主要讲了三大块内容 1 bean的生命周期概念 2 bean的初始化和销毁的三种方式对比(代码演练) 3 总结 1 bean的生命周期概念 1.1 bean的定义:xml中关于bean ...
- Spring课程 Spring入门篇 1-2Spring简介
课程链接: 1 Spring是什么? 2 为什么是Spring 3 Spring的作用: 4 适用范围 1 Spring是什么? a 开源框架 b 轻量级的控制反转(Ioc)和面向切面编程(AOP)的 ...
- Spring 框架 详解 (三)-----IOC装配Bean
IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...
- Spring学习二----------IOC及Bean容器
© 版权声明:本文为博主原创文章,转载请注明出处 接口 用于沟通的中介物的抽象化 实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的 ...
- Spring——IOC与Bean容器
[IOC] (1)IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护.也就是说由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中 (2)DI: ...
- Ioc及Bean容器(三)
专题一 IoC 接口及面向接口编程 什么是 IoC Spring 的Bean配置 Bean 的初始化 Spring 的常用注入方式 接口 用于沟通的中介物的抽象化 实体把自己提供给外界的一种抽象化说明 ...
- Spring课程 Spring入门篇 6-2 ProxyFactoryBean及相关内容(上)
1 解析 1.1 类的方式实现各种通知需要实现的接口 1.2 创建Spring aop代理的优点及方法 1.3 代理控制切入点和通知的顺序的代码实现(具体完全实现,见代码2.1) 1.4 代理方式选择 ...
随机推荐
- (原创)团体程序设计天梯赛-练习集 L1-048 矩阵A乘以B (15 分)
给定两个矩阵A和B,要求你计算它们的乘积矩阵AB.需要注意的是,只有规模匹配的矩阵才可以相乘.即若A有Ra行.Ca列,B有Rb行.Cb列,则只有Ca与Rb相等时,两 ...
- 想要转行/入行做产品经理,你得先get这些正确姿势
转自:https://mp.weixin.qq.com/s/Bh9QEihdV1JLWwB5I4VJ7Q 参考: 张小龙首次公开演讲(官方无删减版) 前腾讯高级产品经理:如何用“女性思维”做更好的产品 ...
- 区块链中的密码学(-)区块链中运用最广的散列算法-SHA256算法分析与实现
在很多技术人员的眼中,区块链并不是一种新的技术,而是过去很多年计算机技术的组合运用.而在这个方方面面技术的运用上,基于密码学的加密算法可以说是区块链各种特点得以表现的根本,一旦目前使用的加密算法被证实 ...
- ADX3000 三层网络 纠错
基于三层网络设计要点: 1 组网配置,两端LAN口,IP地址不同网段,设置主IP地址,当做网段内部的网关使用 2 接口域添加trust接口 3 静态IP,有且仅能有一条,目的IP 0.0.0.0 子网 ...
- luogu4345 [SHOI2015]超能粒子炮·改(组合数/Lucas定理)
link 输入\(n,k\),求\(\sum_{i=0}^k{n\choose i}\)对2333取模,10万组询问,n,k<=1e18 注意到一个2333这个数字很小并且还是质数这一良好性质, ...
- kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
- VS2008 生成的程序有管理员权限
vs 2008 . 解决方案---右键属性----连接器---清单文件---UAC执行级别---设置为requireAdministrator
- Unity 使用小技巧
本文介绍我遇到过我Unity使用小技巧,有了这些技巧,项目做起来,溜得飞起 1.快速设置相机的位置 2.固定面板
- poj1062 昂贵的礼物(dijkstra+枚举)
传送门:点击打开链接 题目大意:买东西,每个东西有了替代品,拥有替代品后可以有优惠价格,每个物品的主人有自己的等级,等级超过m的不能直接或者间接交易,问买1号物品的最低价格是多少. 思路:一开始想到d ...
- Linux网络编程基础
1. Linux网络模型 ① OSI七层模型和Linux四层模型 ② 各种协议之间的关系及在Linux模型中的位置 ③ 协议封装:各种协议处于一种层层封装的关系 (1)Ethernet (2)IP * ...