Spring基础系列(二)

1、使用注解配置Spring

配置XML文件:java

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

    <!--扫描指定包及其子包下全部类中的注解-->
    <context:component-scan base-package="domain"></context:component-scan>

</beans>

将car对象注入到Spring中:web

@Component("car2")
public class Car {
    @Value("yellow")
    private String color;

    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public String toString() {
        return "Car{" +
                "color='" + color + '\'' +
                '}';
    }
}

将user对象注入到Spring中:spring

//对象名为user,将其放入Spring容器中
@Component("user")
//@Service("user") //service层
//@Controller("user") //web层
//@Repository("user") //dao层
//@Scope(scopeName = "protoType") //指定对象做用范围:多例
public class User {
    @Value("long")  //经过反射的Field赋值,破坏了封装性
    private String name;
    //@Autowired //自动装配
    //自动装配问题:若是匹配多个类型一致的对象,将没法选择具体注入哪个对象
    //@Qualifier("car2") 使用Qualifier注解告诉Spring容器自动装配哪一个名称的对象
    //使用Resource注解手动注入,指定注入哪一个名称的对象(推荐)
    @Resource(name="car2")
    private Car car;
    @PostConstruct   //在对象被建立后调用
    public void init(){
        System.out.println("我是初始化方法");
    }
    @PreDestroy      //在对象被销毁以前调用
    public void destroy(){
        System.out.println("我是销毁方法");
    }

    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }

    public String getName() {
        return name;
    }
    //@Value("long") //经过set方法赋值(推荐)
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", car=" + car +
                '}';
    }
}

测试:express

public class Main {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        User bean = (User) context.getBean("user");
        System.out.println(bean.toString());
    }
}

运行结果:
我是初始化方法
User{name=’long’, car=Car{color=’yellow’}}
dom

2、Spring中的AOP

Spring可以为容器中管理的对象生成动态代理对象。
Spring实现Aop的原理:Spring封装了动态代理和cglib代理
动态代理局限性:被代理对象必需要有接口,才能产生代理对象。
cglib代理:第三方代理技术,能够对任何类生成代理。代理的原理是对目标对象进行继承代理。若是目标对象被final修饰,那么该类没法被cglib代理。
使用动态代理:
UserService:
ide

public interface UserService {
    void save();
    void delete();
    void update();
    void find();
}

UserServiceImpl:svg

public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        System.out.println("保存用户!");
        //int i = 1/0;
    }
    @Override
    public void delete() {
        System.out.println("删除用户!");
    }
    @Override
    public void update() {
        System.out.println("更新用户!");
    }
    @Override
    public void find() {
        System.out.println("查找用户!");
    }
}

生成代理:测试

public class UserServiceProxyFactory implements InvocationHandler {
    public UserServiceProxyFactory(UserService us) {
        super();
        this.us = us;
    }
    private UserService us;
    public UserService getUserServiceProxy(){
        //生成动态代理
        UserService usProxy = (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(),
                UserServiceImpl.class.getInterfaces(),  //被代理对象实现的接口
                this);
        //返回
        return usProxy;
    }
    @Override
    public Object invoke(Object arg0, Method method, Object[] arg2) throws Throwable {
        System.out.println("打开事务!");
        Object invoke = method.invoke(us, arg2);
        System.out.println("提交事务!");
        return invoke;
    }
}

测试:this

public class Demo {
    @Test
    //动态代理
    public void fun1(){
        UserService us = new UserServiceImpl();
        //传入代理对象
        UserServiceProxyFactory factory = new UserServiceProxyFactory(us);
        UserService usProxy = factory.getUserServiceProxy();
        usProxy.save();
        //代理对象与被代理对象实现了相同的接口
        //代理对象 与 被代理对象没有继承关系
        System.out.println(usProxy instanceof UserServiceImpl );//false
    }
}

运行结果:
打开事务!
保存用户!
提交事务!
false

关于动态代理部分,能够看这篇博客JavaWeb基础系列(十三)类加载器、动态代理
使用cglib代理:
spa

public class UserServiceProxyFactory2 implements MethodInterceptor {
    public UserService getUserServiceProxy(){
        //帮咱们生成代理对象
        Enhancer en = new Enhancer();
        //设置对谁进行代理
        en.setSuperclass(UserServiceImpl.class);
        //代理要作什么
        en.setCallback(this);
        //建立代理对象
        UserService us = (UserService) en.create();
        return us;
    }
    @Override
    public Object intercept(Object prxoyobj, Method method, Object[] arg, MethodProxy methodProxy) throws Throwable {
        //打开事务
        System.out.println("打开事务!");
        //调用原有方法
        Object returnValue = methodProxy.invokeSuper(prxoyobj, arg);
        //提交事务
        System.out.println("提交事务!");
        return returnValue;
    }
}

代码测试:

@Test
    public void fun2(){
        UserServiceProxyFactory2 factory = new UserServiceProxyFactory2();
        UserService usProxy = factory.getUserServiceProxy();
        usProxy.save();
        //判断代理对象是否属于被代理对象类型
        //代理对象继承了被代理对象=>true
        System.out.println(usProxy instanceof UserServiceImpl );  //true
    }

运行结果:
打开事务!
保存用户!
提交事务!
true

2.一、Spring中的AOP名词解释

JoinPoint(链接点):目标对象中全部能够加强的方法。
PointCut(切入点):目标对象,已经加强的方法。
Advice(通知 / 加强):加强的代码。
Target(目标对象):被代理对象。
Weaving(织入):将通知应用到切入点的过程。
Proxy(代理):将通知织入到目标对象以后造成代理对象。
Aspect(切面):切入点+通知

2.二、Spring中的AOP演示

建立通知类:

//表示该类是一个通知类
public class MyAdvice {
    //前置通知
    public void before(){
        System.out.println("这是前置通知!!");
    }
    //后置通知
    public void afterReturning(){
        System.out.println("这是后置通知(若是出现异常不会调用)!!");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知以前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知以后的部分!!");
        return proceed;
    }
    //异常通知
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}

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">
    <!--扫描指定包及其子包下全部类中的注解-->
    <context:component-scan base-package="domain"></context:component-scan>
    <context:component-scan base-package="aop"></context:component-scan>
    <!--一、配置目标对象,获得的是代理的对象-->
    <bean name="userServiceTarget" class="aop.service.UserServiceImpl"></bean>
    <!--二、配置通知对象-->
    <bean name="myAdvice" class="aop.advice.MyAdvice"></bean>
    <!--三、配置将通知织入目标对象-->
    <aop:config>
        <!-- 配置切入点 public void cn.long.service.UserServiceImpl.save() void cn.long.service.UserServiceImpl.save() * cn.long.service.UserServiceImpl.save() * cn.long.service.UserServiceImpl.*() * cn.long.service.*ServiceImpl.*(..) * cn.long.service..*ServiceImpl.*(..) -->
        <aop:pointcut id="pc" expression="execution(* aop.service.UserServiceImpl.*(..))"></aop:pointcut>
        <aop:aspect ref="myAdvice" >
            <!-- 指定名为before方法做为前置通知 -->
            <aop:before method="before" pointcut-ref="pc"></aop:before>
            <!-- 后置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc"></aop:after-returning>
            <!-- 环绕通知 -->
            <aop:around method="around" pointcut-ref="pc" />
            <!-- 异常拦截通知 -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"></aop:after-throwing>
            <!-- 后置 -->
            <aop:after method="after" pointcut-ref="pc"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

测试:

package aop;

import aop.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/spring-config.xml")
public class Demo {
    @Resource(name="userServiceTarget")
    private UserService us;

    @Test
    public void fun1(){
        us.save();
    }
}

运行结果:
我是初始化方法这是前置通知!!
这是环绕通知以前的部分!!
保存用户!
这是后置通知(出现异常也会调用)!!
这是环绕通知以后的部分!!
这是后置通知(若是出现异常不会调用)!!
我是销毁方法

2.三、Spring中的AOP注解方式演示

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

    <!--一、配置目标对象-->
    <bean name="userServiceTarget" class="aop.service.UserServiceImpl"></bean>
    <!--二、配置通知对象-->
    <bean name="myAdvice" class="aop.advice.MyAdvice2"></bean>
    <!-- 3.开启使用注解完成织入 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

通知:

package aop.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice2 {
    @Pointcut("execution(* aop.service.UserServiceImpl.*(..))")
    public void pc(){}
    //前置通知
    //指定该方法是前置通知,并制定切入点
    @Before("MyAdvice2.pc()")
    public void before(){
        System.out.println("这是前置通知!!");
    }
    //后置通知
    @AfterReturning("execution(* aop.service.UserServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("这是后置通知(若是出现异常不会调用)!!");
    }
    //环绕通知
    @Around("execution(* aop.service.UserServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知以前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知以后的部分!!");
        return proceed;
    }
    //异常通知
    @AfterThrowing("execution(* aop.service.UserServiceImpl.*(..))")
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    @After("execution(* aop.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}

测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/aop/advice/spring-config.xml")
public class Demo {
    @Resource(name="userServiceTarget")
    private UserService us;
    @Test
    public void fun1(){
        us.save();
    }
}

运行结果:
这是环绕通知以前的部分!!
这是前置通知!!
保存用户!
这是环绕通知以后的部分!!
这是后置通知(出现异常也会调用)!!
这是后置通知(若是出现异常不会调用)!!


Spring系列目录:
Spring基础系列(一)
Spring基础系列(二)
Spring基础系列(三)

转载请标明出处,原文地址:https://blog.csdn.net/weixin_41835916
若是以为本文对您有帮助,请点击支持一下,您的支持是我写做最大的动力,谢谢。
这里写图片描述