赞
踩
创建商品业务类,定义增删改查方法。
使用aop注解,实现日志增强,增强方式为前置,后置,环绕
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Test_0915_KQC</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>Test_0915_KQC Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <servlet.version>4.0.1</servlet.version> <jsp.version>2.2</jsp.version> <jstl.version>1.2</jstl.version> <spring.version>5.3.14</spring.version> <commons-dbcp.version>1.4</commons-dbcp.version> <mybatis.version>3.4.6</mybatis.version> <mybatis-spring.version>1.3.3</mybatis-spring.version> <mysql-connector-java.version>8.0.11</mysql-connector-java.version> <fastjson.version>1.2.78</fastjson.version> </properties> <dependencies> <!-- 添加javaEE支持 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <!-- provided只在编译时支持,发布时不拷贝文件 --> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <!-- provided只在编译时支持,发布时不拷贝文件 --> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- 引入spring基础模块 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--dbcp连接池 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${commons-dbcp.version}</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis spring整合 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!-- mybatis插件PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.0</version> </dependency> <!--通用mapper插件--> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>3.0.1</version> </dependency> <!-- mysql驱动类 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency> <!-- fastjson处理json数据 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!-- lombok 简化实体内容 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.2</version> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>com.github.xuwei-k</groupId> <artifactId>html2image</artifactId> <version>0.1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> </dependency> </dependencies> </project>
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="goodsService" class="com.service.impl.GoodsServiceImpl"/>
<context:component-scan base-package="com.advise"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- <aop:aspectj-autoproxy/>-->
</beans>
package com.advise; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component @Aspect public class LogAdvise { @Pointcut("execution(* com.service.impl.*.*(..))") public void pc(){ } //@Before("execution(* com.service.impl.*.*(..))") @Before("pc()") public void bf(){ System.out.println("前置通知:开始记录日志"); } //@AfterReturning("execution(* com.service.impl.*.*(..))") @AfterReturning("pc()") public void af(){ System.out.println("后置通知:结束记录日志"); } //@Around("execution(* com.service.impl.*.*(..))") @Around("pc()") public void hr(ProceedingJoinPoint jp) throws Throwable{ System.out.println("环绕通知:开始记录日志"); System.out.println("业务方法:"+jp.getSignature()); System.out.println("业务类:"+jp.getTarget()); jp.proceed(); System.out.println("环绕通知:结束记录日志"); } @AfterThrowing("pc()") public void yc(){ System.out.println("异常通知:记录日志"); } }
package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
private Integer goodsId;
private String goodsName;
}
package com.service;
import com.entity.Goods;
import java.util.List;
public interface IGoodsService {
Integer delete(Integer id);
Integer update(Goods goods);
Integer insert(Goods goods);
List<Goods> query();
}
package com.service.impl; import com.entity.Goods; import com.service.IGoodsService; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class GoodsServiceImpl implements IGoodsService { @Override public Integer delete(Integer id) { return 1; } @Override public Integer update(Goods goods) { System.out.println("。。。。。更新业务。。。。。"); return 1; } @Override public Integer insert(Goods goods) { System.out.println("。。。。。插入业务。。。。。"); return 1; } @Override public List<Goods> query() { return new ArrayList<>(); } }
package com.test; import com.entity.Goods; import com.service.IGoodsService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-2.xml"); IGoodsService iGoodsService=ac.getBean(IGoodsService.class); iGoodsService.update(new Goods()); iGoodsService.insert(new Goods()); } }
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
参考:出现SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.的解决方法https://blog.csdn.net/weixin_47872288/article/details/122358131
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。