Springboot 使用aop ,切controll层可以,切service层不行,找了好久不知道是为啥。
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--end mybatis-->
<!--通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.5</version>
</dependency>
<!--<!–pagehelper 分页插件–>-->
<!--<dependency>-->
<!--<groupId>com.github.pagehelper</groupId>-->
<!--<artifactId>pagehelper-spring-boot-starter</artifactId>-->
<!--<version>1.2.3</version>-->
<!--</dependency>-->
<!--增加alibaba的连接池-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.19</version>
</dependency>
<!--end 增加连接池-->
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--end 热部署-->
<!--默认日志Logback-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!--shiro依赖包-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<!--<!–redis 的spring支持–>-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--</dependency>-->
<!-- 整合redis -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<!-- redis客户端操作 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.aspectj</groupId>-->
<!--<artifactId>aspectjrt</artifactId>-->
<!--<version>1.7.4</version>-->
<!--</dependency>-->
</dependencies>
service层
@Service
public class UserService extends BaseService {
@Autowired
UserMapper userMapper ;
/**
* 分页查询
* @param user
* @param page
* @return
*/
@RedisCacheEnable(invalidTime = 15)
public Page<User> findPage(User user , Page<User> page){
user.setPage(page);
page.setResultList(userMapper.findList(user));
return page;
}
}
RedisCacheAspect
@Component
@Aspect
public class RedisCacheAspect {
@Autowired
private RedisUtils redisUtils;
private Logger logger = LoggerFactory.getLogger(this.getClass());
// @Pointcut("@annotation(com.lin.commons.config.cache.annotation.RedisCacheEnable)")
@Pointcut("execution(* com.lin.modules.sys.services.UserService.*(..))")
public void pointCut(){}
@Before(value = "pointCut()")
public void before(){
System.out.println("===========================================>before");
}
}
controller:
@Autowired
RedisUtils redisUtils;
@Autowired
UserService userService ;
@RequestMapping("get")
public User get(@Param("id") String id ){
return userService.get(id);
}
/**
* 获取
* @return
*/
@RequestMapping("findPage")
public Page<User> findPage(){
String key = "findPage";
Page<User> page ;
page = userService.findPage(new User(), new Page<>());
return page;
}
@SpringBootApplication
@EnableTransactionManagement // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@MapperScan(basePackages = "com.lin", markerInterface = GlobalMapper.class)//在com.lin这里面的包进行扫描
@EnableCaching//增加redis的支持需要增加这一行
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DemoApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoApplication.class);
// @RequestMapping("/hello")
// public String sayHello(){
// return "hello";
// }
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
}
}
问题发现了,可能因为项目中有集合了shiro,在authRealm中把userService给设置成懒加载就好了,不知道为什么。
谢谢大神的指点,在shiro里面添加注解@Lazy就可以了
谢谢大神的指点!!!!!!!!!!!!