java - 在 Grails 3 项目中为 xml bean 配置依赖注入(inject)

标签 java spring spring-mvc grails groovy

我在 grails 3 中有一个项目,它有一个项目 spring 依赖项,在 spring 项目中,配置了 xml beans。 grails架构中应该如何导入bens?

构建.gradle

dependencies {
    compile (project(':spring-project')) { transitive = false }
}

设置.gradle
includeFlat 'spring-project'

我尝试了以下方法:

在 resources.groovy 中加载 bean:
beans = {
  importBeans('path/to/beans-definition.xml')
}

在 resources.xml 中:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

    <import resource="path/to/beans-definition.xml" />

</beans>

除此之外,在spring项目中,配置了使用java注解的bean。 bean 在 grails 应用程序中不起作用,即使在组件扫描中设置 spring 项目包也是如此。
@ComponentScan(basePackages = ["package.spring.project.beans"])
class Application extends GrailsAutoConfiguration {
     static void main(String[] args) {
          GrailsApp.run(Application, args)
     }
}

但是,没有任何效果。任何帮助,将不胜感激..

最佳答案

也可以使用 grails-app/conf/spring/resources.xml 配置 Bean。在早期版本的 Grails 中,此文件是由 run-app 脚本自动为您生成的,但 resources.groovy 中的 DSL 现在是首选方法,因此现在不会自动生成。但它仍然受支持 - 您只需要自己创建它。

<bean id="myBean" class="my.company.MyBeanImpl">
<property name="someProperty" value="42" />
<property name="otherProperty" value="blue" />

进一步检查 link

关于java - 在 Grails 3 项目中为 xml bean 配置依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52303982/

相关文章:

spring - 通过 Eclipse 插件工具将 Web 应用程序添加到 WebSphere Liberty Profile

java - 如何在Hibernate中动态添加Entity?

Java-Spring反射带来了类中不存在的方法

java - 如何从重复的映射值生成唯一键

java - POST 请求 Java CouchDB

java - 使用 java spring 中的 Post-Redirect-Get 模式转换 Post 请求

java - 将 Hibernate Sessions 功能与 Spring Data JPA 一起使用

java - 在 Spring MVC 中将 View 模型映射到数据库模型时验证相关数据

java - 如何让两个线程轮流修改一个arraylist?

Java 存储文件中的行号和值