Lin_hugh
林赝菖
采纳率0%
2018-05-15 07:16 阅读 5.6k

Springboot 使用aop ,切controll层可以,切service层不行,请问是为什么

5

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>
        <!--&lt;!&ndash;pagehelper 分页插件&ndash;&gt;-->
        <!--<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>

        <!--&lt;!&ndash;redis 的spring支持&ndash;&gt;-->
        <!--<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>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

7条回答 默认 最新

  • Lin_hugh 林赝菖 2018-05-15 07:47

    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;
        }
    }
    
    点赞 评论 复制链接分享
  • Lin_hugh 林赝菖 2018-05-15 07:48

    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");
        }
    
    }
    
    点赞 评论 复制链接分享
  • Lin_hugh 林赝菖 2018-05-15 07:49

    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;
        }
    
    点赞 评论 复制链接分享
  • Lin_hugh 林赝菖 2018-05-15 07:50
     @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);
        }
    }
    
    点赞 评论 复制链接分享
  • Lin_hugh 林赝菖 2018-05-15 08:13

    问题发现了,可能因为项目中有集合了shiro,在authRealm中把userService给设置成懒加载就好了,不知道为什么。

    点赞 评论 复制链接分享
  • ze_lin_huang ze_lin_huang 2018-11-29 02:53

    谢谢大神的指点,在shiro里面添加注解@Lazy就可以了

    图片说明

    点赞 评论 复制链接分享
  • weixin_43688435 龙码天下 2019-01-17 22:02

    谢谢大神的指点!!!!!!!!!!!!

    点赞 评论 复制链接分享

相关推荐