Spring之IOC、AOP及基本使用

10 篇文章 1 订阅

概述

Spring是轻量级的开源的JavaEE框架,可以解决企业应用开发的复杂性,有两个核心部分:IOC和Aop。

  • IOC:控制反转,把创建对象过程交给spring进行。

  • Aop:面向切面,不修改源代码进行功能增强。

特点

  • 方便解耦,简化开发

  • AOP编程支持

  • 方便程序测试

  • 方便和其他框架进行整合

  • 方便进行事务操作

  • 降低API开发难度

相关依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

spring配置文件

<?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">

    <!--配置User对象相关的创建-->
    <bean id="user" class="com.company.User"></bean>
</beans>

基本使用

package com.company.testdemo;

import com.company.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5 {

    @Test
    public void testAdd(){
//        1.加载spring配置文件
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
//        2.获取配置创建的对象
        User user = context.getBean("user", User.class);
        System.out.println(user);
        user.add();
    }
}

IOC

控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理,使用IOC目的是为了降低耦合度。

底层原理

xml解析、工厂模式、反射

IOC 接口(BeanFactory)

IOC思想基于IOC容器完成,IOC容器底层就是对象工厂

Spring提供IOC容器实现两种方式:(两个接口)

  • BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用

特点:加载配置文件时候不会创建对象,在获取对象(或者使用对象)才去创建对象
  • ApplicationContext:BeanFactory接口的子接口,提供更多更强大的功能,一般由开发人人员进行调用

特点:加载配置文件时候就会把在配置文件对象进行创建

ApplicationContext接口有实现类

  • FileSystemXmlApplicationContext

  • configLocation:要写上配置文件在系统盘(某个盘)里的路径

  • ClassPathXmlApplicationContext

  • configLocation:要写上类路径

IOC操作Bean管理

Bean管理指的是两个操作Spring 创建对象、Spring 注入属性。Bean管理操作有两种方式,基于xml配置文件方式实现、基于注解方式实现。

基于xml方式

基于xml方式创建对象

<!--配置User对象创建-->
<bean id="user" class="com.xxx.spring5.User"></bean>
  • 在Spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建

  • 在bean标签有很多属性,介绍常用的属性

  • id属性:唯一标识

  • class属性:类全路径(包和类路径)

  • 创建对象时候,默认也是执行无参构造方法

基于xml方式注入属性

DI:依赖注入,就是注入属性

DI是IOC一种具体实现,表示依赖注入,注入属性是在创建对象的基础之上进行完成
  • 使用set方法进行注入

  • 创建类,定义属性和对应的set方法

  • 在Spring配置文件对象创建,配置属性注入

 <!--set方法注入属性-->
      <bean id="book" class="com.xxx.spring5.testdemo.Book">
          <!--使用property完成属性注入
              name:类里面属性名称
              value:向属性注入的值-->
          <property name="bname" value="xxx"></property>
          <property name="bauthor" value="xxx"></property>
      </bean>
  • 使用有参数构造方法进行注入

  • 创建类,定义属性,创建属性对应有参构造方法

  • 在Spring配置文件中进行配置

<!--用有参构造注入属性-->
    <bean id="orders" class="com.xxx.spring5.testdemo.Orders">
        <constructor-arg name="oname" value="computer"></constructor-arg>
        <constructor-arg name="address"value="China"></constructor-arg>
    </bean>
  • p名称空间注入

  • 使用p名称空间注入,可以简化基于xml配置方式,添加p名称空间在配置文件中

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"></beans>
  • 进行属性注入,在bean标签里面进行操作

 <!--set方法注入属性-->
        <bean id="book" class="com.xxx.spring5.testdemo.Book" p:bname="xxx" p:bauthor="xxx">
        </bean>
xml注入其他类型属性
字面量
  1. null值

     <!--null值-->
        <property name="address">
            <null/>
        </property>
  1. 属性值包含特殊符号

    <!--属性值包含特殊符号
         1. 把<>进行转义 &lt;&gt;
         2. 把带特殊符号内容写到CDATA
        -->
        <property name="address">
            <value><![CDATA[<<北京>>]]></value>
        </property>
注入属性-外部bean
  1. 创建两个类service类和dao类

  1. 在service调用dao里面的方法

  1. 在spring配置文件中进行配置


        <!--1 配置service和dao对象相关的创建-->
    <bean id="userService" class="com.xxx.spring5.service.UserService">
    <!--注入userDao对象
        name属性值:类里面属性名称
        ref属性:创建userDao对象bean标签id值-->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="UserDao" class="com.xxx.spring5.dao.UserDaoImpl"></bean>
    <bean id="userDaoImpl" class="com.xxx.spring5.dao.UserDaoImpl"/></bean>
注入属性-内部bean
    <!--内部bean-->
    <bean id="Emp" class="com.xxx.spring5.bean.Emp">
    <!--设置两个普通属性-->
        <property name="name" value="lucy"></property>
        <property name="gender" value="女"></property>
    <!--设置对象类型的属性-->
        <property name="dept">
            <bean id="dept" class="com.xxx.spring5.bean.Dept">
                <property name="dname" value="保安部"></property>
            </bean>
        </property>
    </bean>
注入属性-级联赋值
   <!--内部bean-->
    <bean id="Emp" class="com.xxx.spring5.bean.Emp">
    <!--设置两个普通属性-->
        <property name="name" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.xxx.spring5.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>
xml方式注入集合属性
  1. 注入数组类型属性

  1. 注入List集合类型属性

  1. 注入Map集合类型属性

<!--完成集合类型属性的注入-->
    <bean id="stu" class="com.xxx.spring5.collectiontype.Stu">
        <!--数组类型的属性注入-->
        <property name="courses">
            <array>
                <value>java课程</value>
                <value>数据库课程</value>
            </array>
        </property>
        <!--list属性注入-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>李四</value>
            </list>
        </property>
        <!--Map类型注入-->
        <property name="map">
            <map>
                <entry key="JAVA" value="java" ></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>
        <!--set集合注入-->
        <property name="set">
            <set>
                <value>MySQL</value>
                <value>redis</value>
            </set>
        </property>
    </bean>
  • 在集合里面设置对象类型的值

     <!--注入list集合类型,值是对象-->
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>
<!--    创建多个course对象-->
    <bean id="course1" class="com.xxx.spring5.collectiontype.Course">
        <property name="cname" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.xxx.spring5.collectiontype.Course">
        <property name="cname" value="Mybatis框架"></property>
    </bean>
  • 把集合注入部分提取出来

  • 在Spring配置文件中引入名称空间util

<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/util/spring-beans.xsd
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
        <!--把集合注入部分提取出来-->
</beans>
  • 使用util标签完成list集合注入提取

       <!--1.把提取list集合类型属性注入-->
    <util:list id="bookList">
        <value>降龙十八掌</value>
        <value>九阳神功</value>
    </util:list>

        <!--2.提取list集合类型属性注入使用-->
    <bean id="book" class="com.xxx.spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>

FactoryBean

  1. Spring有两种类型bean,一种普通bean,另一种工厂bean(FactoryBean)

  1. 普通bean:在配置文件中定义bean类型就是返回类型

  1. 工厂bean:在配置文件中定义bean类型可以和返回类型不一样

bean 作用域

  1. 在Spring里面,可以设置bean实例是单实例还是多实例

  1. 在Spring里面,默认情况下,bean是单实例对象

如何设置单实例还是多实例?

  1. 在Spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例

  1. scope属性值

  1. singleton,表示单实例对象,默认值

  1. prototype,表示是多实例对象

<bean id="myBean" class="com.xxx.spring5.factorybean.MyBean" scope="prototype"></bean></beans>

singleton和prototype区别

  1. singleton表示单实例,prototype表示多实例

  1. 设置scope值singleton时候,加载spring配置文件就会创建一个单实例对象

  • 设置scope值是prototype时候,不是在加载spring配置文件时候创建对象,在调用getBean方法时候创建多实例对象

bean 生命周期

  1. 生命周期

  • 从对象创建到对象销毁的过程

  1. bean生命周期

  1. 通过构造器创建bean实例(无参数构造)

  1. 为bean的属性设置值和对其他bean引用(调用set方法)

  1. 调用bean初始化的方法(需要进行配置初始化方法)

  1. bean可以使用了(对象获取到了)

  1. 当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)

bean的后置处理器,bean生命周期有七步

  1. 通过构造器创建bean实例(无参数构造)

  1. 为bean的属性设置值和对其他bean引用(调用set方法)

  1. 把bean实例传递bean前置处理器的方法postProcessBeforeInitialization

  1. 调用bean初始化的方法(需要进行配置初始化方法)

  1. 把bean实例传递bean后置处理器的方法postProcessAfterInitialization

  1. bean可以使用了(对象获取到了)

  1. 当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)

添加后置处理器

public class MyBeanPost implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之前执行的方法");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之后执行的方法");
        return bean;
    }

}

xml 自动装配

自动装配是指根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入

  • 根据属性名称自动注入

<!--    实现自动装配
        bean标签属性autowire,配置自动装配
        autowire属性常用两个值:
            byName根据属性名称注入,注入值bean的id值和类属性名称一样
            byType根据属性类型注入
       -->
    <bean id="emp" class="com.xxx.spring5.autowire.Emp" autowire="byName">
<!--        <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.xxx.spring5.autowire.Dept"></bean>
  • 根据属性类型自动注入

    <bean id="emp" class="com.xxx.spring5.autowire.Emp" autowire="byType">
<!--        <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.xxx.spring5.autowire.Dept"></bean>

引入外部属性文件

配置德鲁伊连接池

<!--    引入外部属性文件-->
    <context:property-placeholder location="classpath*:jdbc.properties"/>
    <!--配置连接池-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.userName}"></property>
        <property name="password" value="${prop.passwd}"></property>
        </bean>

基于注解方式

注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值),注解可以作用在类上面,方法上面,属性上面,使用注解目的是为了简化xml配置。

Spring针对Bean管理中创建对象提供注解

  • @Component

  • @Service

  • @Controller

  • @repository

上面四个注解功能是一样的,都可以用来创建bean实例
基于注解方式实现对象创建

开启组件扫描

    <!--开启组件扫描
        1 如果扫描多个包 使用逗号隔开
        2 扫描包上层目录-->
    <context:component-scan base-package="com.xxx.spring5"></context:component-scan>

创建类,在类上面添加创建对象注解

//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//  UserService -- userService
@Component(value = "userService")  //<bean id="userService" class=".."/>
public class UserService {

    public void add(){
        System.out.println("service add......");
    }
}

开启组件扫描细节配置

    <!--示例1
        use-default-filters="false 表示现在不使用默认filter,不扫描全部,自己配置filter
        context:include-filler,设置扫描那些内容-->
    <context:component-scan base-package="com.xxx" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--示例2
        下面配置扫描包所有内容
        context:exclude-filter:设置那些内容不进行扫描-->
    <context:component-scan base-package="com.xxx">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
基于注解方式实现属性注入
  • @AutoWired:根据属性类型自动装配

//    定义dao类型的属性
//    不需要添加set方法
//    添加注入属性注解
    @Autowired
    private UserDao userDao;
  • @Qualifier:根据属性名称注入

  • 这个@Qualifier注解的使用,和上面@Autowired一起使用

  • @Resource:可以根据类型注入,可以根据名称注入

  • Resource是Javax包里面的

  • @Value:注入普通类型属性

AOP

面向切面编程(方面),利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。不通过修改源代码方式,在主干功能里面添加新功能

底层原理

AOP底层使用动态代理

  • 有接口的情况,使用JDK动态代理

  • 创建接口实现类代理对象,增强类的方法

  • 没有接口情况,使用CGLIB动态代理

  • 创建子类的动态对象,增强类的方法

JDK动态代理

使用JDK动态代理,使用Proxy类里面的方法创建代理对象

java.lang.reflect.Proxy

newProxyInstance方法

方法有三个参数:
第一参数,类加载器
第二参数,增强方法所在的类,这个类实现的接口,支持多个接口
第三参数,实现这个接口InvocationHandler,创建代理对象,写增强的部分
//创建代理对象代码
class UserDaoProxy implements InvocationHandler{

    //1 把创建的是谁的代理对象 把谁传递过来、
    //有参数构造器
    private Object obj;
    public UserDaoProxy(Object obj){
        this.obj=obj;


    }

    //增强的逻辑
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //方法之前
        System.out.println("方法之前执行......"+method.getName()+" :传递的参数..."+ Arrays.toString(args));

        //被增强的方法执行
        Object res= method.invoke(obj,args);

        //方法之后
        System.out.println("方法之后执行......"+obj);
        return res;
    }
}
public class JDKProxy {
    public static void main(String[] args) {
        //创建接口实现类代理对象
        Class[] interfaces={UserDao.class};
//        Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new InvocationHandler() {
//            @Override
//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                return null;
//            }
//        });
        UserDaoImpl userDao=new UserDaoImpl();
        UserDao dao=(UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));
        int result = dao.add(1, 2);
        System.out.println("result"+result);
    }
}

AOP(术语)

  • 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化、方法执行、方法调用、字段调用或处理异常等等,Spring只支持方法执行连接点,在AOP中表示为在哪里干

  • 切入点(Pointcut): 选择一组相关连接点的模式,即可以认为连接点的集合,Spring支持perl5正则表达式和AspectJ切入点模式,Spring默认使用AspectJ语法,在AOP中表示为在哪里干的集合

  • 通知(Advice):在连接点上执行的行为,通知提供了在AOP中需要在切入点所选择的连接点处进行扩展现有行为的手段;包括前置通知(before advice)、后置通知(after advice)、环绕通知(around advice),在Spring中通过代理模式实现AOP,并通过拦截器模式以环绕连接点的拦截器链织入通知;在AOP中表示为干什么

  • 方面/切面(Aspect):横切关注点的模块化,比如上边提到的日志组件。可以认为是通知、引入和切入点的组合;在Spring中可以使用Schema和@AspectJ方式进行组织实现;在AOP中表示为在哪干和干什么集合

AOP相关操作

  • Spring框架一般基于AspectJ实现AOP操作

  • AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作

  • 基于AspectJ实现AOP操作

  • 基于xml配置文件实现

  • 基于注解方式实现(使用)

  • 切入点表达式

  • 切入点表达式的作用,知道对哪个类型里面的哪个方法进行增强

  • 语法结构:execution([权限修饰符] [返回类型] [类全路径] [方法名称] [参数列表])

AOP操作(@Aspect注解)

@Component
@Aspect //生成代理对象
//被增强的类
public class UserProxy {
}

在spring配置文件中开启生成代理对象

<!--    开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
package com.xxx.spring5.aopanno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect //生成代理对象
//被增强的类
public class UserProxy {

    //前置通知
    //@Before注解表示作为前置通知
    @Before(value = "execution(* com.xxx.spring5.aopanno.User.add(..))")
    public void before(){
        System.out.println("before.......");
    }
    //后置通知(返回通知)
    @AfterReturning(value = "execution(* com.xxx.spring5.aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning.....");
    }


    //最终通知
    @After(value = "execution(* com.xxx.spring5.aopanno.User.add(..))")
    public void after(){
        System.out.println("afterg.....");
    }

    //异常通知
    @AfterThrowing(value = "execution(* com.xxx.spring5.aopanno.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing.....");
    }

    //环绕通知
    @Around(value = "execution(* com.xxx.spring5.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕之前.......");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后.......");
    }
}

相同切入点抽取

    //相同切入点抽取
    @Pointcut(value ="execution(* com.xxx.spring5.aopanno.User.add(..))")
    public void pointdemo(){

    }

    //前置通知
    //@Before注解表示作为前置通知
    @Before(value = "pointdemo()")
    public void before(){
        System.out.println("before.......");
    }
有多个增强类多同一个方法进行增强,设置增强类优先级,在增强类上面添加注解@Order(数字类型值),数字类型值越小优先级越高
完全使用注解开发
@Configuration
@ComponentScan(basePackages = {"com.xxx"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}

AOP操作(AspectJ配置文件)

在Spring配置文件中配置切入点

    <!--配置aop增强-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="p" expression="execution(* com.xxx.spring5.aopxml.Book.buy(..))"/>
        <!--配置切面-->
        <aop:aspect ref="bookProxy">
        <!--配置增强作用在哪个方法上-->
            <aop:before method="before" pointcut-ref="p"/>
        </aop:aspect>
    </aop:config>
</beans>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

像向日葵一样~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值