通知advice

Posted yunhemeihe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通知advice相关的知识,希望对你有一定的参考价值。

 

1.

import com.baizhi.Annotataion.TestAnnotation;
import com.baizhi.DAO.UserDAO;
import com.baizhi.entity.User;

/**
 * @ClassName UserServiceImpl
 * @Discription
 * @Author
 * @Date 2019/11/6 0006 10:08
 * @Version 1.0
 */
public class UserServiceImpl implements  UserService {

    //交给 工厂创建 userDAO
    UserDAO userDAO;

    public UserDAO getUserDAO() {
        return userDAO;
    }
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Override
    public void register(User user) {
     /*   UserDAO userDAO = (UserDAO)FactoryUtil.getBean("userDAO");*/
        System.out.println("--------UserService    register  -----");
        //userDAO.insertUser(user);

    }

    @TestAnnotation(name = "zhangsan")
    @Override
    public boolean login(String name, String password) {
        System.out.println("-----这里是登陆-------");
        return false;
    }
}

2.

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * @ClassName Before1
 * @Discription
 * @Author
 * @Date 2019/11/6 0006 19:24
 * @Version 1.0
 */
public class Before1  implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("-----------------测试性能开始-----------------------");
        System.out.println(method.getName());
    }
}

3.spring-core.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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

   <!--
      id:当前xml配置文件中唯一,建议id的值首字母小写
      class:是要创建对象的全限定名 com.baizhi.xxx
   -->
    <bean id="userService" class="com.baizhi.service.UserServiceImpl"></bean>
    <bean id="userService1" class="com.baizhi.service.service1.UserServiceImpl"></bean>
  <!--  <bean id="user" class="com.baizhi.entity.User"></bean>-->
    <bean id="before" class="com.baizhi.advice.Before1"></bean>
    <aop:config>
        <aop:pointcut id="pc" expression="@annotation(com.baizhi.Annotataion.TestAnnotation)"></aop:pointcut>
        <aop:advisor advice-ref="before" pointcut-ref="pc"></aop:advisor>
    </aop:config>

</beans>

以上是关于通知advice的主要内容,如果未能解决你的问题,请参考以下文章

Spring中Aop是如何使用的呢?

Spring AOP通知实例 – Advice

Spring通知(Advice)有哪些类型

Spring的通知(Advice)

Spring学习(十五)----- Spring AOP通知实例 – Advice

Spring AOP里面的通知Advice类型