我正在尝试通过第三方库(OWL API)中的类实现@Bean
。
此实现使用@Inject
批注。 Spring试图解释它,从而干扰了第三方库的注入机制,并避免了它按预期工作。
在实例化bean时,是否有一种方法可以指示Spring忽略bean实现的@Inject
注释?
我发现关于这个主题的问题很少,但是没有一个问题提供了适用于我的情况的解决方案。
我实际上设法解决了这个问题,方法是将第三方对象包装在一个匿名类中,显然为Spring设置了一个障碍,并阻止它查看此对象(请参阅下面的要点3。),但是我认为是一个丑陋的解决方法。
详细信息:
根据OWL API文档,将以如下方式创建OWLOntologyManager
:
OWLOntologyManagerFactory ontologyManagerFactory = new OWLManager(); OWLOntologyManager owlOntologyManager = ontologyManagerFactory.get(); //... use owlOntologyManager
确实,在我的Spring应用程序中正在运行。但是,我需要具有
OWLOntologyManagerFactory
范围的application
和具有OWLOntologyManager
范围的Session
。
因此,我将这两个对象中的每一个都声明为具有适当范围的Spring @Bean
,并开始收到错误:
创建名称为'scopedTarget.sessionOWLOntologyManager'的bean时出错:通过方法'setIRIMappers'参数0表示的依赖关系未满足;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'java.util.Set'的合格Bean:应该至少有1个有资格作为自动装配候选的Bean。依赖项注释:{}
请参见下面的代码示例。
@RestController
public class OntologiesController {
@RequestMapping("ontologies")
public String manager_loadOntology(
@RequestParam(value="ontologyIriString") String ontologyIriString
) throws OWLOntologyCreationException
{
OWLOntologyManagerFactory ontologyManagerFactory = new OWLManager();
OWLOntologyManager owlOntologyManager = ontologyManagerFactory.get();
OWLOntology ontology = owlOntologyManager.loadOntology(IRI.create(ontologyIriString));
return ontology.toString();
}
}
@Configuration
public class ApplicationScopeConfig {
@Bean
@ApplicationScope
public OWLOntologyManagerFactory applicationOWLOntologyManagerFactory() {
return new OWLManager();
}
}
@Configuration
public class SessionScopeConfig {
@Autowired
OWLOntologyManagerFactory applicationOWLOntologyManagerFactory;
@Bean
@SessionScope
public OWLOntologyManager sessionOWLOntologyManager() {
return applicationOWLOntologyManagerFactory.get();
}
}
@RestController
public class OntologiesController {
@Autowired
private OWLOntologyManager sessionOWLOntologyManager;
@RequestMapping("ontologies")
public String manager_loadOntology(
@RequestParam(value="ontologyIriString") String ontologyIriString
) throws OWLOntologyCreationException
{
OWLOntology ontology = sessionOWLOntologyManager.loadOntology(IRI.create(ontologyIriString));
return ontology.toString();
}
}
在第2点的代码中,我如下修改了sessionOWLOntologyManager(),将其包装到一个匿名类中,该类防止Spring查看真正的owlOntologyManager。
@Bean
@SessionScope
public OWLOntologyManager sessionOWLOntologyManager() {
final OWLOntologyManager owlOntologyManager = applicationOWLOntologyManagerFactory.get();
return new OWLOntologyManager() {
public void clearOntologies() {
owlOntologyManager.clearOntologies();
}
//additional 400 lines implementing all methods by delegating to owlOntologyManager
//Apparently that creates a barrier for Spring so it does not conflict with the
//@Inject annotation in the implementation of the original owlOntologyManager,
//but in spite of having IDE support to generate this delegation, I consider it
//as an workaround.
}
}
我正在尝试通过第三方库(OWL API)中的类实现@Bean。此实现使用@Inject批注。 Spring试图解释它,从而干扰了注入...
[由于最终证明这与Configuring Spring to ignore dependencies annotated with @Inject有关,所以我认为获得更好答案的机会很低,我将自己的其他发现发布在对自己的答复中。