课程链接:

本节讲了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. 1 加载文件
  2. FileSystemXmlApplicationContext context1 = new FileSystemXmlApplicationContext("F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-ioc.xm");
  3. 2 加载路径
  4. ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
  1. 3  web应用
    <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>
  4. <servlet>
  5. <servlet-name>context</servlet-name>
  6. <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  7. <load-on-startup>1</load-on-startup>
  8. </servlet>

1  接口及面向接口编程

1.1  java8拥有方法体

1.2  接口只声明不实现

1.3  接口只能有声明,抽象类既可以有声明,也可以有实现;类中只能实现

6  Demo

共两个案例,

Demo1:

1  介绍面向接口编程的Demo

Demo2:

1  控制反转应用演示 (产生对象的过程由容器负责,不再由类去创建)

注意事项:spring-*.xml必须放在项目的classpath下(右击项目==》project==》buildpath==》source==》最下边是项目的默认classpath路径),否则不加载

Demo1:

测试类:

  1. package com.imooc.bean.ioc.interfaces;
  2.  
  3. public class Main {
  4.  
  5. /**
  6. * 面向接口编程的简单说明,用接口声明,
  7. * 将接口的实现类赋值给对象的声明,然后进行调用
  8. * @param args
  9. */
  10. public static void main(String[] args) {
  11. OneInterface oif = new OneInterfaceImpl();
  12. System.out.println(oif.hello("word."));;
  13. }
  14.  
  15. }

接口:

  1. package com.imooc.bean.ioc.interfaces;
  2.  
  3. public interface OneInterface {
  4.  
  5. String hello(String word);
  6.  
  7. }

实现类:

  1. package com.imooc.bean.ioc.interfaces;
  2.  
  3. public class OneInterfaceImpl implements OneInterface{
  4.  
  5. @Override
  6. public String hello(String word) {
  7. // TODO Auto-generated method stub
  8. return "Word form interfacce\"OneInterface\":"+word;
  9. }
  10.  
  11. }

Demo2:

测试类:

  1. package com.imooc.test.ioc.interfaces;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.junit.runners.BlockJUnit4ClassRunner;
  6.  
  7. import com.imooc.bean.ioc.interfaces.OneInterface;
  8. import com.imooc.bean.ioc.interfaces.OneInterfaceImpl;
  9. import com.imooc.test.base.UnitTestBase;
  10. /**
  11. * The Question We Need To Focus:
  12. * 1 ioc-spring.xml的路径必须放在classpath下边,否则无法加载xml
  13. * @author weijingli
  14. *
  15. */
  16. @RunWith(BlockJUnit4ClassRunner.class)
  17. public class TestOneInterface extends UnitTestBase {
  18.  
  19. public TestOneInterface(){
  20. super("classpath*:spring-ioc.xml");
  21. }
  22.  
  23. @Test
  24. public void testHello() {
  25. // TODO Auto-generated method stub
  26. OneInterface oif = super.getbean("oneInterface");
  27. System.out.println(oif.hello("myInput"));
  28. }
  29.  
  30. }

接口:

  1. package com.imooc.bean.ioc.interfaces;
  2.  
  3. public interface OneInterface {
  4.  
  5. String hello(String word);
  6.  
  7. }

实现类:

  1. package com.imooc.bean.ioc.interfaces;
  2.  
  3. public class OneInterfaceImpl implements OneInterface{
  4.  
  5. @Override
  6. public String hello(String word) {
  7. // TODO Auto-generated method stub
  8. return "Word form interfacce\"OneInterface\":"+word;
  9. }
  10.  
  11. }

基类:

  1. package com.imooc.test.base;
  2.  
  3. import org.apache.commons.lang.StringUtils;
  4. import org.junit.After;
  5. import org.junit.Before;
  6. import org.springframework.beans.BeansException;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;

  8. /*

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]+"));

*/

  1. public class UnitTestBase {
  2. //spring-core的jar包下
  3. private ClassPathXmlApplicationContext context;
  4. private String springXmlPath;
  5. public UnitTestBase(){
  6. }
  7. //初始化该类,刚进来时传入xml路径
  8. public UnitTestBase(String springXmlPath){
  9. this.springXmlPath = springXmlPath;
  10. }
  11. //StringUtils在common-lang-2.4包下
  12. @Before
  13. public void before(){
  14. if(StringUtils.isEmpty(springXmlPath)){
  15. springXmlPath="classpath*:spring-*.xml";
  16. }
  17. try{
  18. context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
  19. context.start();
  20. }catch(BeansException e){
  21. e.printStackTrace();
  22. }
  23. }
  24. @After
  25. public void after(){
  26. context.destroy();
  27. }
  28. @SuppressWarnings("unchecked")
  29. protected <T extends Object> T getbean(String beanId){
  30. try {
  31. return (T)context.getBean(beanId);
  32. } catch (BeansException e) {
  33. // TODO: handle exception
  34. e.printStackTrace();
  35. }
  36. return null;
  37. }
  38. protected <T extends Object> T getbean(Class <T> clazz){
  39. return context.getBean(clazz);
  40. }
  41. }

spring-ioc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd"
  6. default-init-method="init" default-destroy-method="destroy">
  7.  
  8. <bean id="oneInterface" class="com.imooc.bean.ioc.interfaces.OneInterfaceImpl"></bean>
  9.  
  10. </beans>

pom.xml(maven通用)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>com.ddwei</groupId>
  8. <artifactId>ddwei-dao</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10.  
  11. <name>ddwei-dao</name>
  12. <!-- FIXME change it to the project's website -->
  13. <url>http://www.example.com</url>
  14.  
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <maven.compiler.source>1.7</maven.compiler.source>
  18. <maven.compiler.target>1.7</maven.compiler.target>
  19. </properties>
  20.  
  21. <dependencies>
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. <version>4.10</version>
  26. <scope>test</scope>
  27. </dependency>
  28. </dependencies>
  29.  
  30. <build>
  31. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  32. <plugins>
  33. <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
  34. <plugin>
  35. <artifactId>maven-clean-plugin</artifactId>
  36. <version>3.1.0</version>
  37. </plugin>
  38. <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
  39. <plugin>
  40. <artifactId>maven-resources-plugin</artifactId>
  41. <version>3.0.2</version>
  42. </plugin>
  43. <plugin>
  44. <artifactId>maven-compiler-plugin</artifactId>
  45. <version>3.8.0</version>
  46. </plugin>
  47. <plugin>
  48. <artifactId>maven-surefire-plugin</artifactId>
  49. <version>2.22.1</version>
  50. </plugin>
  51. <plugin>
  52. <artifactId>maven-jar-plugin</artifactId>
  53. <version>3.0.2</version>
  54. </plugin>
  55. <plugin>
  56. <artifactId>maven-install-plugin</artifactId>
  57. <version>2.5.2</version>
  58. </plugin>
  59. <plugin>
  60. <artifactId>maven-deploy-plugin</artifactId>
  61. <version>2.8.2</version>
  62. </plugin>
  63. <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
  64. <plugin>
  65. <artifactId>maven-site-plugin</artifactId>
  66. <version>3.7.1</version>
  67. </plugin>
  68. <plugin>
  69. <artifactId>maven-project-info-reports-plugin</artifactId>
  70. <version>3.0.0</version>
  71. </plugin>
  72. </plugins>
  73. </pluginManagement>
  74. </build>
  75. </project>

Spring课程 Spring入门篇 2-1 IOC和bean容器的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期

    课程链接: 本节主要讲了三大块内容 1 bean的生命周期概念 2 bean的初始化和销毁的三种方式对比(代码演练) 3 总结 1 bean的生命周期概念 1.1 bean的定义:xml中关于bean ...

  4. Spring课程 Spring入门篇 1-2Spring简介

    课程链接: 1 Spring是什么? 2 为什么是Spring 3 Spring的作用: 4 适用范围 1 Spring是什么? a 开源框架 b 轻量级的控制反转(Ioc)和面向切面编程(AOP)的 ...

  5. Spring 框架 详解 (三)-----IOC装配Bean

    IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...

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

    © 版权声明:本文为博主原创文章,转载请注明出处 接口 用于沟通的中介物的抽象化 实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的 ...

  7. Spring——IOC与Bean容器

    [IOC] (1)IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护.也就是说由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中 (2)DI: ...

  8. Ioc及Bean容器(三)

    专题一 IoC 接口及面向接口编程 什么是 IoC Spring 的Bean配置 Bean 的初始化 Spring 的常用注入方式 接口 用于沟通的中介物的抽象化 实体把自己提供给外界的一种抽象化说明 ...

  9. Spring课程 Spring入门篇 6-2 ProxyFactoryBean及相关内容(上)

    1 解析 1.1 类的方式实现各种通知需要实现的接口 1.2 创建Spring aop代理的优点及方法 1.3 代理控制切入点和通知的顺序的代码实现(具体完全实现,见代码2.1) 1.4 代理方式选择 ...

随机推荐

  1. java volatile 关键字(转)

    volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...

  2. winform按钮文字换行

    用button的Bounds属性 btn.Bounds = new Rectangle(new Point(5 + i * 143, 25), new Size(100, 60));

  3. Buy or Build UVA - 1151 Kruskal+枚举

    题意: 大概意思是有 n 个点,现在有 q 个方案 ,第 i 个方案耗费为 ci ,使 Ni 个点联通 ,当然也可以直接使两点联通 ,现求最小生成树的代价. 两点直接联通的代价是欧几里得距离的平方: ...

  4. 树莓派-开启spi

    1. sudo raspi-config #进入树莓派配置页 2. #进入每5项,进入启用spi即可

  5. pytorch contiguous的使用

    contiguous一般与transpose,permute,view搭配使用 即使用transpose或permute进行维度变换后,调用contiguous,然后方可使用view对维度进行变形. ...

  6. C语言文件I/O和标准I/O函数

    读取/写入  相对于文件而言 输入/输出 相对于程序/内存而言 一切皆文件,键盘.显示屏也是文件,只不过是特殊的标准文件: 标准文件:标准输入.标准输出.标准错误:---->对应的文件指针:st ...

  7. Github如何在Linux系统下创建本地仓库

    一.电脑上安装 Git Ubuntu安装GIt:  apt-get install git 查看版本信息:    git version 配置Git用户信息  输入: git config --glo ...

  8. linux 中iscsi服务

      ###############第一步: 创建一个2G的分区第二步: yum install targetcli -y 第三步:创建一个2G的分区,并同步  第四步:  执行tagetclils查看 ...

  9. Java 类型转换(int->String)

    1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  10. Vue 中怎么发起请求(二)

    fetch 是新一代XMLHttpRequest的一种替代方案.无需安装其他库.可以在浏览器中直接提供其提供的api轻松与后台进行数据交互. 基本用法: 1 fetch(url,{parmas}).t ...