spring - 在 Grails 中 Autowiring 定制的 Spring 组件

标签 spring grails spring-annotations

我有一个定制的 Spring,它捆绑在一个 jar 中,然后将其设置为我的 Grails 应用程序的依赖项。我在 resouces.groovy 中使用 importBeans 语句加载 bean 的应用程序上下文,例如

beans = {
  importBeans('classpath:app-context-my-component.xml')
}
app-context-my-component.xml有一些 bean 定义和以下几行
<context:annotation-config />
<context:property-placeholder location="classpath:my-component.properties" />

我试图与 Grails 一起使用的组件用 @Component("myComponent") 注释。 .

Grails 正在加载外部应用程序上下文。我知道这一点,因为我首先在类路径上没有 .properties 文件,并且我们没有用于在我的 @Value 声明中丢失属性的回退机制。

在 Grails Controller 中,该组件用作
class MyController {

  def myComponent

  def someaction() {
    myComponent.doSomething()
  }
}

结果是 NullPointerException,即组件的 Autowiring 毕竟不起作用 .我尝试在 Controller 中使用@Autowired,但这带来了如此奇怪的问题,我认为这不可能是我想要走的路。

使用的 Grails 版本是 2.3.6
Spring 组件也设置为使用 Spring 3.2.7 版 以避免不兼容。

更新

现在,我再次掌握了这个问题的时间,我设置了 Spring 日志记录以找出发生了什么。好吧,Spring 上下文加载产生了大量日志,但这是我设法从全部日志中获取的内容
INFO xml.XmlBeanDefinitionReader Loading XML bean definitions from class path resource [app-context-my-component.xml]
INFO support.PropertySourcesPlaceholderConfigurer Loading properties file from class path resource [my-component.properties]
DEBUG framework.CglibAopProxy Unable to apply any optimisations to advised method: public myapp.external.MyComponent myapp.MyService.getMyComponentClient()
DEBUG framework.CglibAopProxy Unable to apply any optimisations to advised method: public myapp.external.MyComponent myapp.MyService.getMyComponentClient()
DEBUG framework.CglibAopProxy Unable to apply any optimisations to advised method: public myapp.external.MyComponent myapp.MyService.getMyComponentClient()
DEBUG framework.CglibAopProxy Unable to apply any optimisations to advised method: public void myapp.MyService.setMyComponentClient(myapp.external.MyComponent)
DEBUG framework.CglibAopProxy Unable to apply any optimisations to advised method: public void myapp.MyService.setMyComponentClient(myapp.external.MyComponent)
DEBUG framework.CglibAopProxy Unable to apply any optimisations to advised method: public void myapp.MyService.setMyComponentClient(myapp.external.MyComponent)

我更改了日志的命名空间,但是myapp.external命名空间指的是外部jar包,而myapp命名空间指的是Grails应用程序命名空间。我更改了用法,以便从服务而不是直接从 Controller 使用外部组件,但该细节在行为上没有变化。

根据我的理解,Spring 上下文加载进行得很顺利。

更新 2

基于 @th3morg 我有一个想法,只尝试使用基于 XML 的配置,跳过 bean 中的所有 @Component 和此类注释。它奏效了!现在 Grails 设法导入 bean,不再导致使用 NPE。

尽管这至少部分解决了我的问题。我仍然对可以使用 Spring 注释的解决方案感兴趣。

最佳答案

您应该确保在您的 jar 中设置了组件扫描。在此处查看标题为“使用 Spring 命名空间”的部分 http://grails.org/doc/latest/guide/spring.html#theBeanBuilderDSLExplained .如果您无法修改 jar,您还可以使用 resources.xml 并将组件扫描添加到该文件。

或者,您也可以将 bean 一个一个地连接起来,尽管这既麻烦又乏味:

beans = {
  myComponent(com.my.MyComponent){
    someOtherService = ref('someOtherService') //if there are other beans to wire by name
    propertyOne = "x"
    propertyTwo = 2
  }
}

关于spring - 在 Grails 中 Autowiring 定制的 Spring 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26374159/

相关文章:

mysql - 如何在 grails 数据源中同时使用 MySQL 和 MSSQL?

spring - 无法将 Spring Security @Secured Annotation 导入 Grails 3

java - Spring 4 - addResourceHandlers 不解析静态资源

java - Spring 3.0.5 + hibernate 3.6 hibernate.show_sql 未显示。是否由于使用了事务?

tomcat - 部署 grails 应用程序时 stacktrace.log 出现问题

spring - 在 Websphere 上使用 Aspectj

使用 optaplanner 和 drools 的 Grails 依赖项

java - 如何将响应变量建模为来自后端的响应

javascript - SpringMVC : display error view on failed AJAX request

java - 随机 java.util.ConcurrentModificationException : null