有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何不基于其他bean创建bean?

我有一个配置类,其中用@configuration注释。创建2个bean。这些bean使用@Profile注释,因此如果配置文件设置为prod,那么它将被实例化

我正在使用Lombok,我的一个类用@RequiredArgsConstructor注释,我有几个private final字段,这些字段基本上是Lombok在后台自动连接的。从它自动连接的bean中,该类中只有一个方法依赖于这些bean,并且该方法基本上只能在生产环境中使用

我遇到的问题是,如何告诉lombok不要实例化bean

配置:

@Configuration
@Profile("PROD")
@RequiredArgsConstructor
public class ClientFilterConfiguration {



@Bean
public Client createClient() {
    ... return client;
}

@Bean
public Filter createFilterBean(){
    ....return filter;
}

}

类别:

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/client")
public class ClientController {

   private final Admin admin;
   private final AdminDuo adminDuo;
   private final Client client;
   private final Filter filter;

   public void clientPurchase(){
       ...
   }

}

获取以下错误:

...required a bean of type 'com.example.Client' that could not be found.
Action: Consider defining a bean of type 'com.example.Client' in your configuration.

该类中只有一个方法依赖于客户机和过滤器bean,那么有没有办法防止该错误


共 (0) 个答案