Spring框架的AOP(IOC和DI)

1.什么是spring框架?
spring是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,是一个分层的javaEE一站式轻量级开 源框
架.
2.spring的作用?
方便解耦,简化开发,AOP编程支持,声明式事务支持,集成Junit更加方便的进行分层测试,方便集成各种优秀框架.
3.什么是IOC?
控制反转,把创建对象的权利交给spring
4.什么是DI?
属性的依赖注入,spring在通过IOC创建对象的时候,如果对象还有属性,就一并给赋值进去DI是在IOC的基础上进行对象的属性 注入
5.依赖注入的三种实现方式?
构造器注入,Setter方法注入,接口注入

5.什么是AOP:在软件业,AOP为Aspect Oriented Programming的缩写,意味:面向切面编程.通过预编译方式和运行期动态代理实现程序功能的统 一维护的一种技术,AOP是OOP的延续.将一些共性的内容进行抽取,在需要用到的地方,以动态代理的方式进行插入.在不修 改源 码的基础上,还能对源码进行前后增强。

6.Spring支持的事务管理类型?

a、编程式事务管理:这意味你通过编程的方式管理事务,给你带来极大的灵活性,但是难维护。 b、声明式事务管理:这意味着你可以将业务代码和事务管理分离,你只需用注解和XML配置来管理事务。

一,Spring注解

1.创建项目封装jar包,创建包类,如下:

 2.各层代码

advice层(增强类)

package com.wang.advice;

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

@Component
@Aspect
public class Loger {
    @Before("execution(* *..BookServiceImpl.*(..))")
    public void check(){
        System.out.println("前置通知/增强 ,执行系统的权限验证");
    }
    @AfterReturning("execution(* *..BookServiceImpl.*(..))")
    public void logPrint(){
        System.out.println("后置");
    }
    @AfterThrowing("execution(* *..BookServiceImpl.*(..))")
    public void exception(){
        System.out.println("异常");
    }
    @After("execution(* *..BookServiceImpl.*(..))")
    public void distory(){
        System.out.println("最终");
    }
}

sevice层

//接口
package com.wang.service;

public interface BookService {
    int save(int n);
    int qian();
    int hou();
    void rap();
}
//实现类
package com.wang.service.impl;

import com.wang.service.BookService;
import org.springframework.stereotype.Component;

@Component
public class BookServiceImpl implements BookService {
    @Override
    public int save(int n) {
        System.out.println("save");
        return 6;
    }
    @Override
    public int qian() {
        return 6;
    }

    @Override
    public int hou() {
        return 7;
    }

    @Override
    public void rap() {
        System.out.println("rap");
    }
}

spring层(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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描component及同名注解-->
     <context:component-scan base-package="com.wang"/>
<!--开启AOP注解支持-->
    <aop:aspectj-autoproxy/>

servlet层

package com.wang.servlet;

import com.wang.service.BookService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
    @Test
    public void add(){
        //获取ClassPathXmlApplicationContext对象
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        //通过ClassPathXmlApplicationContext对象调取getBean
        BookService bookService=context.getBean(BookService.class);
        bookService.save(6);
    }

}

二,Spring纯注解

1.创建的项目包名类名 如下:

 2.各类中的代码

advice层

package com.wang.advice;

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

@Component
@Aspect
public class Loger {
    @Before("execution(* *..BookServiceImpl.*(..))")
    public void befor() {
        System.out.println("前置通知");
    }

    @AfterReturning("execution(* *..BookServiceImpl.*(..))")
    public void houzhi() {
        System.out.println("后置通知");
    }

    @AfterThrowing("execution(* *..BookServiceImpl.*(..))")
    public void yichang() {
        System.out.println("异常通知");
    }

    @After("execution(* *..BookServiceImpl.*(..))")
    public void zuizhong() {
        System.out.println("最终通知");
    }
}

config层

package com.wang.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration//表示该类是配置类
@ComponentScan("com.wang")
@EnableAspectJAutoProxy
public class SpringConfig {
}

service层

//接口
package com.wang.service;

public interface BookService {
    void select();
    void update();
    void del();
    void add();
}
//实现类
package com.wang.service.impl;

import com.wang.service.BookService;
import org.springframework.stereotype.Component;

@Component
public class BookServiceImpl implements BookService {

    @Override
    public void select() {
        System.out.println("查询...select");
    }

    @Override
    public void update() {
        System.out.println("修改...update");
    }

    @Override
    public void del() {
        System.out.println("删除...del");
    }

    @Override
    public void add() {
        System.out.println("增加...add");
    }
}

servlet层

package com.wang.servlet;

import com.wang.config.SpringConfig;
import com.wang.service.BookService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test01 {
    @Test
    public void add(){
        //加载配置类,获得ioc的容器
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        BookService bookService=context.getBean(BookService.class);
        bookService.add();
    }
}

运行效果

 三,spring环绕通知

1.创建的项目,包名类名如下

 2.各类中的代码

advice层

package com.wang.advice;

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

@Component
@Aspect
public class Loger {
public Object around(ProceedingJoinPoint pjp){
        try{
            //前置增强
            System.out.println("环绕通知----前置增强");
            //通知ProceedingJoinPoint 完成代理对象的方法调用
            Object result=null;//定义返回值变量
            Object[] args=pjp.getArgs();//获取参数列表
            result=pjp.proceed(args);//核心类方法的执行

            //后置增强
            System.out.println("环绕通知----后置增强");
            return result;
        } catch (Throwable e) {
            //异常
            System.out.println("环绕通知----异常增强");
            throw new RuntimeException(e);
        }finally {
            //最终
            System.out.println("环绕通知----最终增强");
        }
    }

}

service层

//接口
package com.wang.service;

public interface BookService {
    int save(int n);
    int qian();
    int hou();
    void rap();
}
//实现类
package com.wang.service.impl;

import com.wang.service.BookService;
import org.springframework.stereotype.Component;

@Component
public class BookServiceImpl implements BookService {
    @Override
    public int save(int n) {
        System.out.println("save");
        return 6;
    }
    @Override
    public int qian() {
        return 6;
    }

    @Override
    public int hou() {
        return 7;
    }

    @Override
    public void rap() {
        System.out.println("rap");
    }
}

spring.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--1.把所有类的对象交给IOC容器进行管理-->
    <bean id="loger" class="com.wang.advice.Loger"/>
    <bean id="bookService" class="com.wang.service.impl.BookServiceImpl"/>
<!--    2.AOP的配置:让增强类 的 哪个方法  动态进行何种增强   核心类 的 哪个方法-->
    <aop:config>
        <aop:aspect id="log" ref="loger">
            <aop:around method="around" pointcut="execution(* *..BookServiceImpl.*(..))"/>
        </aop:aspect>
    </aop:config>

    </beans>

servlet层

package com.wang.servlet;

import com.wang.service.BookService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
    @Test
    public void add(){
        //获取ClassPathXmlApplicationContext对象
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        //通过ClassPathXmlApplicationContext对象调取getBean
        BookService bookService=context.getBean(BookService.class);
        bookService.save(6);
    }

}

运行效果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值