aop与定时任务依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springBoot aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
aop
package com.tenzont.worksitejbq.sysadmin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
@EnableAsync
@EnableScheduling
@ComponentScan(basePackages = {"com.tenzont.worksitejbq"})
public class SysadminApplication {
public static void main(String[] args) {
SpringApplication.run(SysadminApplication.class, args);
}
}
## aop切面类代码
```java
package com.tenzont.worksitejbq.sysadmin.config;
import com.tenzont.worksitejbq.common.model.BaseDataSouceModel;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@Aspect
public class AUTimeAspect {
@Before("@annotation(org.apache.ibatis.annotations.InsertProvider)")
public void setInsertValue(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
if (args != null && args.length == 1 && args[0] instanceof BaseDataSouceModel) {
BaseDataSouceModel arg = (BaseDataSouceModel) args[0];
if (arg.getCreateTime() == null)
arg.setCreateTime(new Date());
if (arg.getCreateUser() == null) {
arg.setCreateUser(ShiroUserUtil.getUserId());
}
}
}
@Before("@annotation(org.apache.ibatis.annotations.UpdateProvider)")
public void setUpdateValue(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
if (args != null && args.length == 1 && args[0] instanceof BaseDataSouceModel) {
BaseDataSouceModel arg = (BaseDataSouceModel) args[0];
arg.setUpdateTime(new Date());
arg.setUpdateUser(ShiroUserUtil.getUserId());
}
}
}
定时器测试插入代码
package com.tenzont.worksitejbq.sysadmin.task;
import com.tenzont.worksitejbq.common.mapper.SysUserMapper;
import com.tenzont.worksitejbq.common.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class GldSchedule {
@Autowired
private SysUserMapper sysUserMapper;
@Scheduled(fixedDelay = 300000, initialDelay = 5000)
public void dayLisGldt1() {
SysUser sysUser = SysUser.builder()
.nickName("无敌的我")
.phone("17639644155")
.status("1")
.userName("宫本")
.organizationId(2)
.remark("测试")
.build();
sysUserMapper.insertSelective(sysUser);
System.out.println("测试完成");
}
}
启动类
package com.tenzont.worksitejbq.sysadmin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
@EnableAsync
@EnableScheduling
@ComponentScan(basePackages = {"com.tenzont.worksitejbq"})
public class SysadminApplication {
public static void main(String[] args) {
SpringApplication.run(SysadminApplication.class, args);
}
}