Spring入门——Spring IOC容器

我不是女神ヾ 2022-07-12 06:30 370阅读 0赞

` IOC`

  • 接口及面向接口编程
  • 什么是IOC
  • Spring的Bean配置
  • Bean的初始化
  • Spring的常用注入方式

接口

  • 用于沟通的中介物的抽象化
  • 实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式
  • 对应Java接口即声明,声明了哪些方法是对外公开提供的
  • 在Java8中,接口可以拥有方法体

面向接口编程

  • 结构设计中,分清层次及调用关系,每层只向外(上层)提供一组功能接口, 各层间仅依赖接口而非实现类 (设计模式中的依赖倒转原则)
  • 接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要
  • “面向接口编程”中的“接口”适用于隐藏具体实现和实现多态性的组件
  • 例子

接口

``

  1. package com.txr.interfaces;
  2. public interface OneInterface {
  3. public String hello(String str);
  4. }

实现类

  1. package com.txr.interfaces.impl;
  2. import com.txr.interfaces.OneInterface;
  3. public class OneInterfaceImpl implements OneInterface{
  4. @Override
  5. public String hello(String str) {
  6. return str+" word!!";
  7. }
  8. }

测试类

  1. package com.txr.test;
  2. import com.txr.interfaces.OneInterface;
  3. import com.txr.interfaces.impl.OneInterfaceImpl;
  4. public class Main {
  5. public static void main(String[] args) {
  6. OneInterface oif=new OneInterfaceImpl();
  7. System.out.println(oif.hello("hello"));
  8. }
  9. }

运行结果

  1. hello word!!

什么是IOC

  • IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护
  • DI:(依赖注入)是其一种实现方式
  • 目的:创建对象并且封装对象之间的关系

Center

Spring的Bean配置

刚才的接口在Spring中的配置方式

  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" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
  7. >
  8. <bean id="oneInterface" class="com.txr.interfaces.impl.OneInterfaceImpl"/>
  9. </beans>

测试类:

  1. package com.txr.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.txr.interfaces.OneInterface;
  5. public class Main {
  6. public static void main(String[] args) {
  7. /*OneInterface oif=new OneInterfaceImpl();
  8. System.out.println(oif.hello("hello"));*/
  9. ApplicationContext context=new ClassPathXmlApplicationContext("com/txr/resource/spring-ioc.xml");
  10. OneInterface oneInterface=(OneInterface)context.getBean("oneInterface");
  11. System.out.println(oneInterface.hello("word"));
  12. }
  13. }

测试结果

  1. word word!!

Bean容器初始化

基础:俩个包

-org.springframework.beans

-org.springframework.context

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

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

方式,ApplicationContext

-本地文件

-Classpath

-Web应用中依赖servelet或Listener

Bean容器的初始化方法

Center 1

Spring注入

  • Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为
  • 常用的俩种注入方式

-设值注入

-构造注入

例子

接口

  1. package com.txr.interfaces;
  2. public interface InjectionDAO {
  3. public void save(String arg);
  4. }

实现类

  1. package com.txr.interfaces.impl;
  2. import com.txr.interfaces.InjectionDAO;
  3. import com.txr.interfaces.OneInterface;
  4. public class InjectionDAOImpl implements InjectionDAO {
  5. public OneInterface oneInterface;
  6. public void setOneInterface(OneInterface oneInterface)
  7. {
  8. this.oneInterface=oneInterface;
  9. }
  10. @Override
  11. public void save(String arg) {
  12. System.out.println("信息为:"+arg);
  13. System.out.println(oneInterface.hello("hello"));
  14. }
  15. public InjectionDAOImpl(OneInterface oneInterface) {
  16. this.oneInterface=oneInterface;
  17. }
  18. public InjectionDAOImpl() {
  19. // TODO Auto-generated constructor stub
  20. }
  21. }

设值注入

  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" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
  7. >
  8. <bean id="oneInterface" class="com.txr.interfaces.impl.OneInterfaceImpl"/>
  9. <bean id="injectionDAO" class="com.txr.interfaces.impl.InjectionDAOImpl">
  10. <property name="oneInterface" ref="oneInterface"></property>
  11. </bean>
  12. </beans>

构造注入

  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" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
  7. >
  8. <bean id="oneInterface" class="com.txr.interfaces.impl.OneInterfaceImpl"/>
  9. <bean id="injectionDAO" class="com.txr.interfaces.impl.InjectionDAOImpl">
  10. <!-- <property name="oneInterface" ref="oneInterface"></property> -->
  11. <constructor-arg name="oneInterface" ref="oneInterface"></constructor-arg>
  12. </bean>
  13. </beans>

运行结果:

  1. 信息为:test
  2. hello word!!

发表评论

表情:
评论列表 (有 0 条评论,370人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring IoC容器

    Spring IoC(Inversion of Control,控制反转)容器是Spring框架的核心功能之一,它是一个轻量级的容器,负责管理Java对象的生命周期和依赖关系。

    相关 Spring IoC容器

    我们将详细介绍 Spring 的 Ioc 容器。 IoC 是指在程序开发中,实例的创建不再由调用者管理,而是由 Spring 容器创建。Spring 容器会负责控制程序之

    相关 Spring IOC容器

    IOC(控制反转):不负责对象的创建,只负责使用,由外部容器创建 DI(依赖注入):创建对象并且组装对象之间的关系        ![watermark_type