This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
/target/
/project/
.classpath
.settings
.eclipse
.idea
.project
+7
View File
@@ -0,0 +1,7 @@
## Spring Groovy
This module contains articles about Spring with Groovy
## Relevant Articles:
- [Groovy Bean Definitions](https://www.baeldung.com/spring-groovy-beans)
+67
View File
@@ -0,0 +1,67 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldug</groupId>
<artifactId>spring-groovy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-groovy</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-spring-4</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-groovy</artifactId>
<version>${spring-integration-groovy.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy-all.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<verbose>true</verbose>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy-eclipse-compiler.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>${groovy-eclipse-batch.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<groovy-eclipse-compiler.version>2.9.2-01</groovy-eclipse-compiler.version>
<groovy-eclipse-batch.version>2.4.3-01</groovy-eclipse-batch.version>
<spring-integration-groovy.version>4.3.7.RELEASE</spring-integration-groovy.version>
<groovy-all.version>2.4.12</groovy-all.version>
</properties>
</project>
@@ -0,0 +1,17 @@
package com.baeldug.groovyconfig;
import java.util.ArrayList;
import java.util.List;
public class BandsBean {
private List<String> bandsList = new ArrayList<>();
public List<String> getBandsList() {
return bandsList;
}
public void setBandsList(List<String> bandsList) {
this.bandsList = bandsList;
}
}
@@ -0,0 +1,18 @@
package com.baeldug.groovyconfig;
beans {
javaPesronBean(JavaPersonBean) {
firstName = 'John'
lastName = 'Doe'
age ='32'
eyesColor = 'blue'
hairColor='black'
}
bandsBean(BandsBean) { bean->
bean.scope = "singleton"
bandsList=['Nirvana', 'Pearl Jam', 'Foo Fighters']
}
registerAlias("bandsBean","bands")
}
@@ -0,0 +1,21 @@
package com.baeldug.groovyconfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JavaBeanConfig {
@Bean
public JavaPersonBean javaPerson() {
JavaPersonBean jPerson = new JavaPersonBean();
jPerson.setFirstName("John");
jPerson.setLastName("Doe");
jPerson.setAge("31");
jPerson.setEyesColor("green");
jPerson.setHairColor("blond");
return jPerson;
}
}
@@ -0,0 +1,57 @@
package com.baeldug.groovyconfig;
public class JavaPersonBean {
public String jj;
private String firstName;
private String lastName;
private String age;
private String eyesColor;
private String hairColor;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getEyesColor() {
return eyesColor;
}
public void setEyesColor(String eyesColor) {
this.eyesColor = eyesColor;
}
public String getHairColor() {
return hairColor;
}
public void setHairColor(String hairColor) {
this.hairColor = hairColor;
}
}
@@ -0,0 +1,13 @@
package com.baeldug.spring_groovy;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
@@ -0,0 +1,8 @@
package com.baeldug.spring_groovy;
import org.springframework.stereotype.Component;
@Component
public class TestConfig {
}
@@ -0,0 +1,5 @@
beans{
testString String, 'test'
testNum int, 100
testObj(i:101,s:'objVal')
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="JavaPersonBean" class="com.baeldug.groovyconfig.JavaPersonBean">
<property name="firstName" value="John" />
<property name="LastName" value="Doe" />
<property name="age" value="30" />
<property name="eyesColor" value="brown" />
<property name="hairColor" value="brown" />
</bean>
</beans>
@@ -0,0 +1,50 @@
package com.baeldug.groovyconfig;
import static org.junit.Assert.assertEquals;
import java.io.File;
import org.junit.Test;
import org.springframework.context.support.GenericGroovyApplicationContext;
public class GroovyConfigurationUnitTest {
private static final String FILE_NAME = "GroovyBeanConfig.groovy";
private static final String FILE_PATH = "src/main/java/com/baeldug/groovyconfig/";
@Test
public void whenGroovyConfig_thenCorrectPerson() throws Exception {
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
ctx.load("file:" + getPathPart() + FILE_NAME);
ctx.refresh();
JavaPersonBean j = ctx.getBean(JavaPersonBean.class);
assertEquals("32", j.getAge());
assertEquals("blue", j.getEyesColor());
assertEquals("black", j.getHairColor());
}
@Test
public void whenGroovyConfig_thenCorrectListLength() throws Exception {
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
ctx.load("file:" + getPathPart() + FILE_NAME);
ctx.refresh();
BandsBean bb = ctx.getBean(BandsBean.class);
assertEquals(3, bb.getBandsList()
.size());
}
private String getPathPart() {
String pathPart = new File(".").getAbsolutePath();
pathPart = pathPart.replace(".", "");
pathPart = pathPart.replace("\\", "/");
pathPart = pathPart + FILE_PATH;
return pathPart;
}
}
@@ -0,0 +1,24 @@
package com.baeldug.groovyconfig;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class JavaConfigurationUnitTest {
@Test
public void whenJavaConfig_thenCorrectPerson() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(JavaBeanConfig.class);
ctx.refresh();
JavaPersonBean j = ctx.getBean(JavaPersonBean.class);
assertEquals("31", j.getAge());
assertEquals("green", j.getEyesColor());
assertEquals("blond", j.getHairColor());
}
}
@@ -0,0 +1,23 @@
package com.baeldug.groovyconfig;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlConfigurationUnitTest {
@Test
public void whenXmlConfig_thenCorrectPerson() {
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-bean-config.xml");
JavaPersonBean j = (JavaPersonBean) applicationContext.getBean("JavaPersonBean");
assertEquals("30", j.getAge());
assertEquals("brown", j.getEyesColor());
assertEquals("brown", j.getHairColor());
}
}
@@ -0,0 +1,38 @@
package com.baeldug.spring_groovy;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppUnitTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppUnitTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppUnitTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}