Spring IOC和DI

Spring的简单介绍

  • 为什么要使用spring?
    我们的三层架构之间仍然有少量的耦合,为了达到理想的高内聚、低耦合状态,spring是一个很好的选择。
  • spring是什么?
    Spring是一个分层的Java SE/EE full-stack轻量级开源框架
  • spring的核心
    1. IOC=Inverse Of Control控制反转
    2. AOP=Aspect OrientedProgramming面向切面的编程思想
  • spring的优点
    1. 方便解耦、简化开发(高内聚、低耦合)(最核心)
      可以这么说,spring就是一个大工厂,可以将所有对象创建和依赖关系维护交给Spring管理
      spring工厂是用于生成bean
    2. AOP编程的支持(最核心)
      Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
    3. 声明式事务的支持(依赖上面两点)
      只需要通过配置就可以完成对事务的管理,而无需手动编程
    4. 方便程序测试
      Spring对Junit4支持,可以通过注解方便的测试Spring程序
    5. 方便集成各种优秀框架
      Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis等)的直接支持
    6. 1.5.6降低JavaEE API的使用难度
      Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

Spring IOC

  • 什么是IOC
    控制什么?我们去控制service对象的生成->框架控制对象的生成
    反转什么?控制权由我们自己反转到Spring的框架
    之前由程序员手动去创建对象实例的过程,引入spring框架以后,由spring帮我们去完成
  • 配置spring环境
    这里写图片描述

  • 值得一提的问题!
    applicationContext.xml一定要放置在src目录下!!!
    这里写图片描述

package com.bamzhy.test;

import com.bamzhy.dao.UserDao;
import com.bamzhy.dao.impl.UserDaoImpl;
import com.bamzhy.service.UserService;
import com.bamzhy.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class test {
    @Test
    public void test1(){
        UserService service=new UserServiceImpl();
        service.register();
    }
    @Test
    public void test2(){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserServiceImpl service = (UserServiceImpl) applicationContext.getBean("hahaservice");
        UserDao dao= (UserDao) applicationContext.getBean("hahadao");
        service.setHaha(dao);
        service.register();
    }
}

Spring DI

  • 什么是DI
    Dependency Injection
    某个bean里面维护了另一个bean的引用,称为这个bean依赖于另一个bean
    注入指的是给bean里的成员赋值
    依赖注入:beanA里依赖的成员变量beanB是通过注入的方式给其赋值
    <bean id="serviceImpl" class="com.bamzhy.factory.ServiceFactory" factory-method="getServiceInstance"/>

        @Test
        public void test5() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService serviceInstance = (UserService) applicationContext.getBean("serviceImpl");

        serviceInstance.register();
    }
}
实例工厂

Bean的生命周期

  • Bean的种类
  • 普通Bean:bean id=”” class=”” 这样由spring直接创建A的实例
  • factory-bean
    是一个特殊的Bean,具有工厂生产对象的能力,只能生产特定的对象,

  • Bean的作用域(生命周期)scope

这里写图片描述

scope=singleton只执行单例
scope=protptype多例

  • BeanPostProcessor (后处理bean )的使用方法
//xml文件中
<bean  class="com.bamzhy.day2.test.PostProcessor" ></bean>

//新建的类
package com.bamzhy.day2.test;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;

public class PostProcessor implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Before输出一些log信息");
        return bean;
    }


    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("After输出一些log信息");
        return bean;
    }

}
  • init和destory的用法
        //使用init方法和destory方法来使得每次创建、close都会输出log信息
        ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        DaoImpl dao = (DaoImpl) classPathXmlApplicationContext.getBean("dao");
        dao.method1();
        ServiceImpl service = (ServiceImpl) classPathXmlApplicationContext.getBean("service");
        service.method1();
        classPathXmlApplicationContext.close();
//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
http://www.springframework.org/schema/beans ">
    <bean id="dao" class="com.bamzhy.day2.dao.impl.DaoImpl" init-method="init" destroy-method="destory"></bean>
    <bean id="service" class="com.bamzhy.day2.service.impl.ServiceImpl" init-method="init" destroy-method="destory"></bean>

</beans>

//bean类中
    public void init(){
        System.out.println("service输出了一些初始化log");
    }

    public void destory(){
        System.out.println("service输出了一些destroy log");
    }
  • 生命周期演示

这里写图片描述

实例化——填充属性——设置Bean名称——执行前预处理程序——初始化——调用个人定制的初始化方法——执行后处理程序——Bean已经准备好了——容器关闭——执行destroy方法——调用客制化的destroy方法

  • BeanPostProcessor (上图倒数第二行左边)
  • Spring提供的一种机制,只要implements BeanPostProcessor,有两个方法需要实现
  • 将实现类提供给spring容器,spring容器将自动执行,在初始化方法前执行before(),在初始化方法后执行after()

基于Xml的装配Bean

  • 构造方法注入属性
  • Setter方法注入属性
  • 集合注入
  • 基于注解的装配Bean
基于注解的装配Bean
<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">

        //扫描这个包下的所有含注解的类
        //这里是命名空间
        <context:component-scan base-package="com.bamzhy"/>

Bean 的实例化方式

  • 默认构造(无参构造)
    大多数spring管理的Bean都是用无参构造
    如果让spring去实例化一个Bean而这个Bean又忘记增加无参构造,那么就会报错
  • 静态工厂
    在没有使用Spring之前
public class ServiceFactory{
    public static AdminService getAdminService(){
        return new AdminServiceImpl();
    }
}
AdminService service=ServiceFactory.getAdminService();

使用了Spring以后

<bean id="myserviceimpl" 
      class="com.bamzhy.factory.ServiceFactory"
      factory-method="getAdminService"
>
AdminService service=(AdminService)classPathXmlApplicationContext.getBean(myserviceimpl)

作用:
用spring去整合一些其他项目、框架、工具类以及重构一些已有项目的时候

注意点:
这种方式获取bean,工厂里的获取bean的方法一定必须是静态的

  • 实例工厂
    必须先实例化工厂,通过工厂实例来产生bean
    提供的方法是非静态的。
//先设置一个名叫serviceFactory的Bean
<bean id="serviceFactory" class="com.bamzhy.fatory.ServiceFactory2"/>
//再在下面的factory-bean中使用它
<bean id="serviceimpl" 
      factory-bean="servicefactory"
      factory-method="getServiceInstance">  
      <property name="dao" ref="daoimpl">   
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值