spring框架之依赖注入

    科技2022-07-11  108

    spring框架

     

    spring framework

    core container核心容器

    aop面向切面编程

    ioc依赖注入

    用户可以按照自己的需要,去定制spring功能模块。

    achema文件夹内分了模块信息。

    软件应用分层架构

    标准三层架构:

    1.数据访问层

    2.业务逻辑层

    3.表示层

    web层

    services层

    dao层

    高内聚低耦合

    public class LoginServlet{ private UserServlet userService; public void doGet() { userService.login(); } } ​ public class UserServlet{ public void login() { System.out.println("登陆程序"); } }

    解决方式:面向接口。

     

    public interface IUserService{ public void login(); } public class UserServlet implements IUserService{ public void login() { System.out.println("登陆程序"); } } public class LoginServlet{ //接口的引用可以指向接口的任何一个实现类对象 private IUserServlet userService=new UserService(); public void doGet() { userService.login(); } }

    spring解决的方法;降耦。

    依赖关系:

    D d=new D(); C c=new C(); c.setD(d); B b=new B(); b.setc(c); A a=new A(); a.setB(b);

    spring解决的问题:简化复杂的依赖关系

    事务处理:service

    spring解决的问题:处理事务

    轻量级:想用哪个就用哪个,不用就添加

    容器:可以管理项目中各种对象的生命周期(对象的创建,初始化,使用,销毁)

    在使用spring框架的时候,项目中的代码里面,可以不出现任何spring 相关的api。(非侵入的)

    容器:container

    1.GUI

    2.servlet/jsp

    3.spring

    bean对象:谁是bean对象?

    bean包。实体类,配置给spring的类

    需求分析

    2.为什么要让对象成为spring容器中的bean对象,

    因为spring容器管理这些对象之后,spring容器可以负责这些对象的创建,初始化,销毁,对象与对象之间建立关系。

    corejava 数组/集合

    spring模块:

    spring core(spring核心容器)ioc控制反转

    spring context(上下文)

    spring web

    spring DAO()

    spring ORM():映射框架,与hibernate,mybatis结合的框架模块

    AOP(面向切面编程)

    Spring mvc(代替serlvet)

     

    ioc inversion of control控制反转

    思考1:是什么东西的控制被反转了,

    思考2:自己组装电脑和直接购买整机的区别。

    总结:ioc将你设计好的类交给spring容器去控制,而不是在类的内部进行控制。

    2.堆积木

    3.ioc容器并没有写对象和对象是什么关系,而是把这种关系的建立交给ioc容器去做,并在代码运行的时候动态建立起来。

    4.ioc其实就是将调用者与被调用者分离的思想。

     

    DI : dependency injection依赖注入

    表示让调用类对某一接口实现类的依赖关系由容器注入,以移除调用类对某一接口实现类的依赖。

    web service servlet IUserService UserServiceImp

    IUserServices service=new UserServiceImp (); #由容器来注入

    DL: dependency loopup 依赖查找

    容器创建对象并提供回调接口和上下文环境给这个对象需要时通过接口从容器中查找其他对象。

    spring IOC核心api

    BeanFactory接口:用于bean的初始化和配置

    Object getBean(String name) :根据指定名称返回一个bean实例

    boolean isPrototype(String name); 判断其是否是原型模式

    boolean isSingleton(String name): 判断名称为name的bean是否是单例

    ApplicationContext接口:继承于BeanFactory,增加事务处理,事件传递,

    //寻找配置文件 String path="com/briup/ioc/set/set.xml" ApplicationContext container=new ClassPathXmlApplicationContext(path); container.getBean(name);

    set.xml是配置文件:

    使用eclipse

    1.改编码

    window-最后一个-workspace-改编码

    在配置文件导入schema文件(.xsd文件)这个文件可以在下载的spring文档中的示例中找到。在eclipse中把xml文件和schema文件关联后,xml中就可以有标签代码的提示了。spring框架是模块化的,之后使用其他模块的时候,还可以在添加其他的配置头文件。

    set.xml文件

    <!--set.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>

    ioc-set

    alt+/ 有自动提示就配置成功了。

    IOC的相关功能,

    1.set方式注入(必须依赖set方法)

    bean.xml测试文件

    <beans> <bean name="stu" class="com.briup.theory.ioc.Student"></bean> <!-- class是全限定名-创建对象为stu --> <bean name="t" class="com.briup.theory.ioc.Teacher"> <property name="student" ref="stu"></property> <!-- property表示对象中的方法,有getstudent方法,和setstudent方法,引用(ref)stu对象 t中就得到stu的对象的方法,实现dl注入 --> </bean> </beans> java测试文件 /* * 创建对象,获取个对象之间的关系 Student s=new Student(); Teacher t=new Teacher(); t.setStudent(s); System.out.println(t); System.out.println(t.getStudent()); */ /** * 通过容器拿出想用的对象 */ BeanFactory container=new BeanFactory(); Teacher t=(Teacher)container.getBean("t"); System.out.println(t); System.out.println(t.getStudent());

    1.理解

    2.印象

    需要的是解决问题的思路

    <!--追加set.xml文件--> <bean name="stu" class="com.briup.bean.Student"> <!-- 依赖注入 --> <property name="id" value="1"></property> <property name="name" value="tom"></property> <property name="age" value="20"></property> </bean>  

    测试application类,关联源码,查看方法,outline查看方法 BeanFactory是核心接口。

    单元测试依赖注入

    @Test public void ioc_set() { try { String[] path = {"com/briup/ioc/set/set.xml"}; ApplicationContext container = new ClassPathXmlApplicationContext(path); Student stu=(Student)container.getBean("stu"); System.out.println(stu.getAge()); System.out.println(stu.getId()); System.out.println(stu.getName()); } catch (Exception e) { e.printStackTrace(); } }

    以上代码实现了spring的注入。

    Processed: 0.031, SQL: 8