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

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>
aop

7个回答

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给设置成懒加载就好了,不知道为什么。

qq_40058321
cocosum
大约一年之前 回复
DearLinxu
chengLinxu 您好,我也碰到这个问题,也是用了shiro,您后面是怎么解决这个问题的,麻烦告知下
大约 2 年之前 回复

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

图片说明

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问