不会注解的小伙伴看这里哦:Spring常用注解!!!-CSDN博客
pom.xml
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.1.8.RELEASE</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.19</version></dependency></dependencies>
UserDaoImpl:
package com.by.dao;import org.springframework.stereotype.Repository;@Repository
public class UserDaoImpl implements UserDao {@Overridepublic void addUser(){System.out.println("insert into tb_user......");}
}
UserServiceImpl:
package com.by.service;import com.by.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Overridepublic void addUser(){userDao.addUser();//System.out.println(8/0);}
}
MyLogActive:(增强类)
/** Copyright (c) 2020, 2024, All rights reserved.**/
package com.by.advice;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;/*** <p>Project: Spring - MyLogAdvice</p>* <p>Powered by scl On 2024-01-05 15:04:11</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
@Component
@Aspect
public class MyLogAdvice {@Before("execution(* com.by.service.*.*(..))")public void before() {System.out.println("前置通知、。、");}@After("execution(* com.by.service.*.*(..))")public void after() {System.out.println("最终通知、、、");}@AfterReturning("execution(* com.by.service.*.*(..))")public void afterReturn(){System.out.println("后置通知");}@AfterThrowing("execution(* com.by.service.*.*(..))")public void afterThrowing(){System.out.println("异常通知");}@Around("execution(* com.by.service.*.*(..))")public void around(ProceedingJoinPoint joinPoint) {try {System.out.println("前环绕通知。。。");joinPoint.proceed();System.out.println("后环绕通知。。。");} catch (Throwable e) {throw new RuntimeException(e);}}
}
applicationContext.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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--扫描com.by下的文件,将其添加到工厂--><context:component-scan base-package="com.by"></context:component-scan><!--aop,自动扫描注解--><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
测试:
/** Copyright (c) 2020, 2024, All rights reserved.**/
package com.by.web;import com.by.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** <p>Project: Spring - Client</p>* <p>Powered by scl On 2024-01-05 15:01:34</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
public class Client {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = applicationContext.getBean("userServiceImpl", UserService.class);System.out.println(userService);userService.addUser();}
}
结果展示: