小编chk*_*chk的帖子

在 Shiro 领域中自动装配时,具有可缓存方法的 Spring 服务在没有缓存的情况下被初始化

在这个问题上花了 2 天后,我真的无法自己取得更多进展。我正在使用 Spring 开发用于依赖注入等的标准 Web 应用程序。我还使用 Spring 来缓存我经常使用的几种昂贵的方法。

在我为安全层引入 Apache Shiro 之后,我遇到了一个奇怪的问题,@Cacheable某个服务中的方法不再被缓存。到目前为止,我已经能够将问题剥离到其核心,但仍有很多代码需要您查看 - 抱歉……

首先,我配置所有相关的包(下面显示的所有类都在其中一个)。

@Configuration
@ComponentScan(basePackages = {
        "my.package.config",
        "my.package.controllers",
        "my.package.security",
        "my.package.services",
})
public class AppConfiguration {

}
Run Code Online (Sandbox Code Playgroud)

这是缓存的配置文件。

@Configuration
@EnableCaching
public class CacheConfiguration {
    @Bean(name = "cacheManager")
    public SimpleCacheManager cacheManager() {
        SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
        simpleCacheManager.setCaches(Arrays.asList(
                new ConcurrentMapCache("datetime")
        ));
        return simpleCacheManager;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于我的最小示例,我使用了一个非常简单的服务,它只返回当前时间戳。这Impl门课和你想象的一样简单。

public interface DateService {
    @Cacheable("datetime")
    LocalDateTime getCurrent();
}
Run Code Online (Sandbox Code Playgroud)

我将此服务注入控制器。

@Controller
@RequestMapping("/v1/date")
public class DateController {
    @Autowired
    DateService dateService; …
Run Code Online (Sandbox Code Playgroud)

spring caching autowired shiro

5
推荐指数
1
解决办法
4573
查看次数

标签 统计

autowired ×1

caching ×1

shiro ×1

spring ×1