spring简介

1、spring7大功能模块

Maven项目中使用spring aop的相关依赖

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

2、控制反转的含义

IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes or a mechanism such as the Service Locator pattern.

  • 中文解释

IoC也称为依赖项注入(DI)。它是对象仅通过构造器、工厂方法、对象实例的属性三种方式来定义它们的依赖(依赖就是与它们一起运作的其他对象)的过程。然后容器在它们创建bean时注入这些依赖对象。这个过程与bean自身直接通过使用构造器实例化是相反的(即bean不是自己通过构造器等机制实例化自己的,是通过IOC容器实例化的),因此称为控制反转。

2、AOP(面向切面编程)的含义

AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP加粗样式允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系.
AOP将应用分为核心关注点横切关注点两个部分。业务处理流程为核心关注点,被业务所依赖的公共部分为横切关注点。横切关注点的特征是其行为经常发生在核心关注点的多处。AOP的核心思想是将核心关注点和横切关注点分离开来,以降低模块耦合度。

  • 横切关注点
    对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

  • 切面(aspect)
    切面就是对横切关注点的抽象

  • 连接点(joinpoint)
    在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

  • 切入点(pointcut)
    对连接点进行拦截的定义

  • 通知(advice)
    拦截到连接点之后要执行的具体的操作,通知分为前置、后置、异常、成功、环绕通知五类

  • 目标对象
    代理的目标对象

  • 织入(weave)
    将切面应用到目标对象并执行代理对象创建的过程

  • 引入(introduction)
    在运行期为类动态地添加一些方法或字段而不用修改类的代码

3、AOP中execution表达式解析

在这里插入图片描述

4、使用spring实现AOP

使用AOP在业务类中添加日志功能

  • [1 ] 方式一:使用spring的API接口
package com.wust.demo2;

public interface UserService {
    void add();
    void delete();
    void update();
    void query();
}
package com.wust.demo2;

public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("增加了一个用户!!!");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户!!!");
    }

    @Override
    public void update() {
        System.out.println("修改了一个用户!!!");
    }

    @Override
    public void query() {
        System.out.println("查询了一个用户!!!");
    }
}
package com.wust.demo2;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforLog implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + ":" + method.getName());
    }
}

package com.wust.demo2;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + ":" + method.getName() + ";返回结果为:" + returnValue);
    }
}
    <bean id="userService" class="com.wust.demo2.UserServiceImpl"/>
    <bean id="beforLog" class="com.wust.demo2.BeforLog"/>
    <bean id="afterLog" class="com.wust.demo2.AfterLog"/>
    
    <aop:config>
    <!--切入点-->
        <aop:pointcut id="pointcut" expression="execution(* com.wust.demo2.UserServiceImpl.*(..))"/>
        //设置环绕通知
        <aop:advisor advice-ref="beforLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
@Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

结果:
com.wust.demo2.UserServiceImpl:add
增加了一个用户!!!
com.wust.demo2.UserServiceImpl:add;返回结果为:null
  • [2 ] 方式二:自定义来实现AOP
//自定义切面
package com.wust.demo2;

public class MyAspect {
    public void before(){
        System.out.println("方法执行前!!!");
    }
    public void after(){
        System.out.println("方法执行后!!!");
    }
}

<bean id="userService" class="com.wust.demo2.UserServiceImpl"/>
<bean id="myaspect" class="com.wust.demo2.MyAspect"/>

<aop:config>
		//定义切面
       <aop:aspect ref="myaspect">
       		//切入点
           <aop:pointcut id="point" expression="execution(* com.wust.demo2.UserServiceImpl.*(..))"/>
           //通知
           <aop:before method="before" pointcut-ref="point"/>
           <aop:after method="after" pointcut-ref="point"/>
       </aop:aspect>
</aop:config>

结果:
  • [2 ] 方式三:使用注解来实现AOP
**//配置类**
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration //声明改类为配置类 ,该类等同于applicationContext.xml
@ComponentScan("com.wust") //扫描指定包小的注解,否则该包内的注解不生效
@EnableAspectJAutoProxy //开启aop相关注解的支持
public class MyConfig {

}

package com.wust.demo3;

import com.wust.demo2.UserService;
import org.springframework.stereotype.Component;

@Component("userService") //将该类注册到IOC容器中,并指定bean的名称为userService
public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("增加了一个用户!!!");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户!!!");
    }

    @Override
    public void update() {
        System.out.println("修改了一个用户!!!");
    }

    @Override
    public void query() {
        System.out.println("查询了一个用户!!!");
    }
}

package com.wust.demo3;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component //注册到IOC容器中
@Aspect  //表明这个类是一个切面
public class MyAspect {
	//前置通知
    @Before("execution(* com.wust.demo3.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("方法执行前!!!");
    }
	//后置通知
    @After("execution(* com.wust.demo3.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("方法执行后!!!");
    }

    //在环绕通知中,可以在方法中定义一个参数,代表我们要处理的切入点(这里指的就是userServiceImpl中的add方法)
    @Around("execution(* com.wust.demo3.UserServiceImpl.*(..))")
    public void arround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前!!!");

        //执行切入点所代表的方法
        Object proceed = joinPoint.proceed();

        System.out.println("环绕后!!!");

    }
}

    @Test
    public void test4(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }

	结果:
	环绕前!!!
	方法执行前!!!
	增加了一个用户!!!
	方法执行后!!!
	环绕后!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值