• Spring注解配置、Spring aop、整合Junit——Spring学习 day2


    注解配置:

    1.为主配置文件引入新的命名空间(约束)

    preference中引入文件

    2.开启使用注解代理配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
    <context:component-scan base-package="com.littlepage.entity"></context:component-scan>
    </beans>

    3.类表名注解

    //相当于xml文件配了bean的name为user

    @Component("user")//entity层
    @Service("user")//service层
    @Controller("user")//web层
    @Repository("user")//dao层

    四个注解一样,但是可以根据注解名字可以了解功能

    @Scope("singlenton")

    scope功能在上一篇博客中讲过

    @Autowared

    自动找到相应类

    4.运行

    sts插件:

    1.手动安装(成功率低)

    2.使用老师的Eclipse(我选用)

    3.spring装好插件的eclipse

    Spring的AOP:

    AOP名词解释:

    Joinpoint(连接点):目标对象中,所有可以增强的方法。(未代理的方法)

    Pointcut(切入点):目标对象中,已经增强的方法。(已代理的方法)

    Advice(通知/增强):增强的代码

    Target(目标对象):被代理对象

    Weaving(织入):将通知应用到切入点的过程

    Proxy(代理):将通知织入到目标对象之后,形成的代理对象。

    Aspect(切面):切入点+通知

    AOP:aspect oriented programming

    步骤:

    1.准备工作,导入aop(约束)命名空间

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

    2.配置目标对象

    package com.littlepage.entity;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    /**
     * 通知类
     * @author 74302
     *
     */
    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("后置通知,出现异常也会调用");
        }
        
        
    }

    <bean name="userServiceTarget" class="com.littlepage.service.UserService"></bean>

    3.将目标织入对象
    <bean name="myAdvice" class="com.littlepage.entity.MyAdvice"></bean>

    4.配置将通知织入目标对象

  • 相关阅读:
    cordova windows环境配置
    javascript 变量声明 和 作用域
    javascript 数组总结
    处理事件的兼容写法
    javascript 闭包
    事件委托和事件绑定
    依赖注入
    .Net委托
    sql游标循环结果集
    T-Sql之集合
  • 原文地址:https://www.cnblogs.com/littlepage/p/10858680.html
Copyright © 2020-2023  润新知