通知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>