Register groovy bean in spring java config

I have xml config

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> <lang:groovy id="foo" script-source="${groovyBeanLocation}"/> </beans> 

I have imported this configuration into my ApplicationConfig, but I do not want to mix several types of configurations (java and xml).

How can I perform a given configuration using java?

+5
source share
1 answer

If you use the Groovy class as a Spring bean, you don’t need the <lang: groovy> tag at all. Just expand your compiled class as if it were Java, and it should only work as long as you include the groovy -all jar file as a project dependency.

The <lang: groovy> tag with the script source is for "updated" beans. This is where you deploy the source code (not the compiled version), and Spring detects changes and recompilations for you. This is how you can update the code in a running application, which is very cool, but rather rare.

If all you want to do is write your implementation classes in Groovy, just compile them as usual and add them to your JavaConfig files just like any other bean. They are all bytecodes in Spring.

+1
source

Source: https://habr.com/ru/post/1212015/


All Articles