Spring Groovy Config with fixed pom (#1200)
* 'bean_injection' * 'bean_injection' * 'changes' * code * git ignore * pom fix * pom fix
This commit is contained in:
parent
494d6d57e1
commit
fda362f79d
@ -0,0 +1,48 @@
|
|||||||
|
package com.baeldung.java_bean_injection;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
import com.baeldung.java_bean_injection.*;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
|
||||||
|
import junit.framework.TestResult;
|
||||||
|
import junit.framework.TestFailure;
|
||||||
|
|
||||||
|
import java.util.Enumeration;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bean Injection Test Application
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
|
||||||
|
TestResult result = new TestResult();
|
||||||
|
AppTest.suite().run(result);
|
||||||
|
System.out.println(String.format("Tests: %d",result.runCount()));
|
||||||
|
System.out.println(String.format("Errors: %d",result.errorCount()));
|
||||||
|
System.out.println(String.format("Failures: %d",result.failureCount()));
|
||||||
|
if(result.failureCount() > 0){
|
||||||
|
Enumeration<TestFailure> failures = result.failures();
|
||||||
|
int failNum = 0;
|
||||||
|
TestFailure failure = null;
|
||||||
|
while(failures.hasMoreElements()){
|
||||||
|
failure = failures.nextElement();
|
||||||
|
|
||||||
|
System.out.println(failure.exceptionMessage());
|
||||||
|
System.out.println(String.format("Test Failure %d\n",failNum));
|
||||||
|
System.out.println(failure.trace());
|
||||||
|
System.out.print("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.java_bean_injection;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.*;
|
||||||
|
import org.springframework.context.annotation.*;
|
||||||
|
import org.springframework.stereotype.*;
|
||||||
|
|
||||||
|
import com.baeldung.java_bean_injection.*;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@PropertySource(value="classpath:inject.properties")
|
||||||
|
@Import(ImportConfig.class)
|
||||||
|
public class AppConfig {
|
||||||
|
@Value("${contructor.arg1}") String constructorArg;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public InjectedClass injectedClass(){
|
||||||
|
InjectedClass ic = new InjectedClass(constructorArg);
|
||||||
|
ic.setMyInt(10);
|
||||||
|
ic.setTestString("test");
|
||||||
|
return ic;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.baeldung.java_bean_injection;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
import com.baeldung.java_bean_injection.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest extends TestCase{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext();
|
||||||
|
context.register(AppConfig.class);
|
||||||
|
context.refresh();
|
||||||
|
InjectedClass injectedClass = (InjectedClass) context.getBean(InjectedClass.class);
|
||||||
|
assertTrue(injectedClass.getMyInt() == 10);
|
||||||
|
assertTrue(injectedClass.getTestString().equals("test"));
|
||||||
|
assertNotNull(injectedClass.obj);
|
||||||
|
assertTrue(injectedClass.myConstructorArg.equals("test"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.java_bean_injection;
|
||||||
|
|
||||||
|
|
||||||
|
public class AutowireObject {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.java_bean_injection;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.*;
|
||||||
|
|
||||||
|
import com.baeldung.java_bean_injection.*;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ImportConfig {
|
||||||
|
|
||||||
|
@Bean(name="autobean")
|
||||||
|
public AutowireObject autowireObject(){
|
||||||
|
return new AutowireObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Bean(name="autobean2")
|
||||||
|
public AutowireObject autowireObject2(){
|
||||||
|
return new AutowireObject();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.java_bean_injection;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.*;
|
||||||
|
import org.springframework.context.annotation.*;
|
||||||
|
|
||||||
|
import com.baeldung.java_bean_injection.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class InjectedClass {
|
||||||
|
private int myInt;
|
||||||
|
private String testString;
|
||||||
|
String myConstructorArg;
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("autobean")
|
||||||
|
AutowireObject obj;
|
||||||
|
|
||||||
|
public InjectedClass(String constArg){
|
||||||
|
this.myConstructorArg = constArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMyInt(int myInt){
|
||||||
|
this.myInt = myInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTestString(String testString){
|
||||||
|
this.testString = testString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMyInt(){
|
||||||
|
return this.myInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTestString(){
|
||||||
|
return this.testString;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
spring-groovy-config/.gitignore
vendored
Normal file
1
spring-groovy-config/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target/
|
@ -0,0 +1,4 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding//src/main/java=UTF-8
|
||||||
|
encoding//src/test/java=UTF-8
|
||||||
|
encoding/<project>=UTF-8
|
@ -0,0 +1,5 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.5
|
@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
groovy.compiler.level=24
|
@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
@ -0,0 +1,9 @@
|
|||||||
|
import com.baeldung.spring_groovy_config.TestClass
|
||||||
|
|
||||||
|
beans{
|
||||||
|
testString String, 'Test String'
|
||||||
|
testClass(TestClass){
|
||||||
|
beanDefinition ->
|
||||||
|
beanDefinition.constructorArgs = ["Test String",10.2]
|
||||||
|
}
|
||||||
|
}
|
5
spring-groovy-config/config/groovyPropConfig.groovy
Normal file
5
spring-groovy-config/config/groovyPropConfig.groovy
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import com.baeldung.spring_groovy_config.TestClassB
|
||||||
|
|
||||||
|
beans{
|
||||||
|
testClassB(TestClassB,testStringB:"Test String",testIntB:10.2)
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
import com.baeldung.spring_groovy_config.TestClassB
|
||||||
|
|
||||||
|
beans{
|
||||||
|
testClassB(TestClassB){
|
||||||
|
testStringB = "Test String"
|
||||||
|
testIntB = 10
|
||||||
|
}
|
||||||
|
}
|
10
spring-groovy-config/config/groovyTestWithRefBean.groovy
Normal file
10
spring-groovy-config/config/groovyTestWithRefBean.groovy
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import com.baeldung.spring_groovy_config.GroovyClass
|
||||||
|
import com.baeldung.spring_groovy_config.ClassWithRef
|
||||||
|
|
||||||
|
beans{
|
||||||
|
groovyClass(GroovyClass, groovyInt:5)
|
||||||
|
classWithRef(ClassWithRef){
|
||||||
|
myClass = groovyClass
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
67
spring-groovy-config/pom.xml
Normal file
67
spring-groovy-config/pom.xml
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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.baeldung</groupId>
|
||||||
|
<artifactId>spring-groovy-config</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>spring-groovy-config</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<!-- 2.8.0-01 and later require maven-compiler-plugin 3.1 or higher -->
|
||||||
|
<version>3.1</version>
|
||||||
|
<configuration>
|
||||||
|
<compilerId>groovy-eclipse-compiler</compilerId>
|
||||||
|
</configuration>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-eclipse-compiler</artifactId>
|
||||||
|
<version>2.9.1-01</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-eclipse-batch</artifactId>
|
||||||
|
<version>2.3.7-01</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-all</artifactId>
|
||||||
|
<version>2.4.8</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>4.3.6.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-core</artifactId>
|
||||||
|
<version>4.3.6.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>4.3.6.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.spring_groovy_config;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.baeldung.spring_groovy_config.GroovyClass;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ClassWithRef {
|
||||||
|
private GroovyClass myClass = null;
|
||||||
|
|
||||||
|
public void setMyClass(GroovyClass myClass){
|
||||||
|
this.myClass = myClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroovyClass getMyClass(){
|
||||||
|
return this.myClass;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.spring_groovy_config
|
||||||
|
|
||||||
|
class GroovyClass {
|
||||||
|
int groovyInt = 0
|
||||||
|
|
||||||
|
def getGroovyInt(){
|
||||||
|
return groovyInt
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.spring_groovy_config;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.GenericGroovyApplicationContext;
|
||||||
|
|
||||||
|
public class MainApp {
|
||||||
|
public static void main(String[] args){
|
||||||
|
ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyTestWithRefBean.groovy");
|
||||||
|
ClassWithRef test = (ClassWithRef) context.getBean("classWithRef");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.spring_groovy_config;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TestClass {
|
||||||
|
private String testString;
|
||||||
|
private double testDouble;
|
||||||
|
|
||||||
|
public TestClass(String testString, double testDouble){
|
||||||
|
this.testString = testString;
|
||||||
|
this.testDouble = testDouble;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTestString(){
|
||||||
|
return this.testString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getTestDouble(){
|
||||||
|
return this.testDouble;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.spring_groovy_config;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TestClassB {
|
||||||
|
private String testStringB;
|
||||||
|
private int testIntB;
|
||||||
|
|
||||||
|
public void setTestStringB(String testStringB){
|
||||||
|
this.testStringB = testStringB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTestStringB(){
|
||||||
|
return this.testStringB;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setTestIntB(int testIntB){
|
||||||
|
this.testIntB = testIntB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTestIntB(){
|
||||||
|
return this.testIntB;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.spring_groovy_config;
|
||||||
|
|
||||||
|
import com.baeldung.spring_groovy_config.*;
|
||||||
|
import groovy.lang.Binding;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
||||||
|
import org.springframework.context.support.GenericApplicationContext;
|
||||||
|
import org.springframework.context.support.GenericGroovyApplicationContext;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
|
/**
|
||||||
|
* Spring Framework Tests for Groovy.
|
||||||
|
*/
|
||||||
|
public class AppTest{
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimple(){
|
||||||
|
ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyContextWithConstructor.groovy");
|
||||||
|
TestClass test = (TestClass) context.getBean("testClass");
|
||||||
|
assertNotNull(test.getTestString());
|
||||||
|
assertEquals(test.getTestString(),"Test String");
|
||||||
|
assertTrue(test.getTestDouble() == 10.2);
|
||||||
|
|
||||||
|
String testString = context.getBean("testString",String.class);
|
||||||
|
assertEquals(testString,"Test String");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testProperties(){
|
||||||
|
ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyPropConfig.groovy");
|
||||||
|
TestClassB test = (TestClassB) context.getBean("testClassB");
|
||||||
|
assertNotNull(test.getTestStringB());
|
||||||
|
assertEquals(test.getTestStringB(),"Test String");
|
||||||
|
assertTrue(test.getTestIntB() == 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPropertiesWithClosure(){
|
||||||
|
ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyPropConfigWithClosure.groovy");
|
||||||
|
TestClassB test = (TestClassB) context.getBean("testClassB");
|
||||||
|
assertNotNull(test.getTestStringB());
|
||||||
|
assertEquals(test.getTestStringB(),"Test String");
|
||||||
|
assertTrue(test.getTestIntB() == 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithRef(){
|
||||||
|
ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyTestWithRefBean.groovy");
|
||||||
|
ClassWithRef test = (ClassWithRef) context.getBean("classWithRef");
|
||||||
|
assertEquals(test.getMyClass().getGroovyInt(),5);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user