简体   繁体   English

春季表达语言:@Bean找不到自己

[英]Spring Expression Language: @Bean cannot find itself

Within my class SimpleBookRepository, I am trying to access an instance method from within Spring's @Cacheable annotation to determine if I should use caching or not. 在我的类SimpleBookRepository中,我试图从Spring的@Cacheable批注中访问一个实例方法,以确定是否应该使用缓存。 When I try to run the application, it fails to start and tells me: 当我尝试运行该应用程序时,它无法启动并告诉我:

Description: 描述:

A component required a bean named 'SimpleBookRepository' that could not be found. 一个组件需要找不到名为“ SimpleBookRepository”的bean。

Action: 行动:

Consider defining a bean named 'SimpleBookRepository' in your configuration. 考虑在配置中定义一个名为“ SimpleBookRepository”的bean。

This is quite confusing to me because the application runs perfectly fine when I remove the condition = "@SimpleBookRepository.useCache()" bit. 这让我很困惑,因为当我删除condition =“ @ SimpleBookRepository.useCache()”位时,应用程序运行得很好。 I would think the condition evaluation and therefore the bean resolution would happen during runtime after the autowiring, and it would be impossible to call the getByIsbn() method without the bean existing. 我认为条件评估以及因此的bean解析将在自动装配后的运行时发生,并且在没有bean的情况下调用getByIsbn()方法是不可能的。 Even when I do explicitly declare a bean in the configuration like: 即使我确实在配置中明确声明一个bean,例如:

@Bean
public SimpleBookRepository simpleBookRepository(){
    return new SimpleBookRepository();
}

I receive the same error. 我收到同样的错误。

I would be very grateful if someone could explain this behavior to me. 如果有人可以向我解释这种行为,我将不胜感激。

I have the following classes: 我有以下课程:

SimpleBookRepository.java SimpleBookRepository.java

package com.mycompany.app;

@Component
public class SimpleBookRepository implements BookRepository{

    @Value("${cache.use}")
    private boolean useCache;

    public boolean useCache(){
        return useCache;
    }

    @Override
    @Cacheable(cacheNames="books", condition = "@SimpleBookRepository.useCache()")
    public Book getByIsbn(String isbn){
        //Get mock data
    }

}

Application.java Application.java

package com.mycompany.app;

@SpringBootApplication
@EnableCaching
public class Application {

    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }

}

CachingConfiguration.java CachingConfiguration.java

package com.mycompany.app;

@EnableCaching
@EnableAutoConfiguration
@Configuration
public class CachingConfiguration {
    //Configure CacheManager bean
}

AppRunner.java AppRunner.java

package com.mycompany.app;

@Component
public class AppRunner implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(AppRunner.class);
    private final BookService bookService;

    @Autowired
    public AppRunner(BookService bookService){
        this.bookService = bookService;
    }

    @Override
    public void run(String... args) throws Exception{
        getBooks();

    }
}

BookService.java BookService.java

package com.mycompany.app;

@Service
public class BookService {

    private BookRepository bookRepository;

    @Autowired
    public BookService(BookRepository bookRepository){
        this.bookRepository = bookRepository;
    }

    public Book getByIsbn(String isbn){
        return bookRepository.getByIsbn(isbn);
    }

}

BookRepository.java BookRepository.java

package com.mycompany.app

@Component
public interface BookRepository {

    Book getByIsbn(String isbn);

}

I actually just found the proper way to do what I want in SpEL. 我实际上只是在SpEL中找到了执行我想要的操作的正确方法。 I changed the condition to 我将条件更改为

condition = "#root.target.useCache()"

Thanks to everyone who answered. 感谢所有回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM