Skip to content

Commit fda362f

Browse files
asevans48adamd1985
authored andcommitted
Spring Groovy Config with fixed pom (eugenp#1200)
* 'bean_injection' * 'bean_injection' * 'changes' * code * git ignore * pom fix * pom fix
1 parent 494d6d5 commit fda362f

22 files changed

Lines changed: 449 additions & 0 deletions
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.java_bean_injection;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
import com.baeldung.java_bean_injection.*;
8+
9+
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.support.ClassPathXmlApplicationContext;
11+
12+
13+
import junit.framework.TestResult;
14+
import junit.framework.TestFailure;
15+
16+
import java.util.Enumeration;
17+
18+
19+
/**
20+
* Bean Injection Test Application
21+
*
22+
*/
23+
public class App
24+
{
25+
public static void main( String[] args )
26+
{
27+
28+
TestResult result = new TestResult();
29+
AppTest.suite().run(result);
30+
System.out.println(String.format("Tests: %d",result.runCount()));
31+
System.out.println(String.format("Errors: %d",result.errorCount()));
32+
System.out.println(String.format("Failures: %d",result.failureCount()));
33+
if(result.failureCount() > 0){
34+
Enumeration<TestFailure> failures = result.failures();
35+
int failNum = 0;
36+
TestFailure failure = null;
37+
while(failures.hasMoreElements()){
38+
failure = failures.nextElement();
39+
40+
System.out.println(failure.exceptionMessage());
41+
System.out.println(String.format("Test Failure %d\n",failNum));
42+
System.out.println(failure.trace());
43+
System.out.print("\n");
44+
}
45+
}
46+
47+
}
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.java_bean_injection;
2+
3+
import org.springframework.beans.factory.annotation.*;
4+
import org.springframework.context.annotation.*;
5+
import org.springframework.stereotype.*;
6+
7+
import com.baeldung.java_bean_injection.*;
8+
9+
@Configuration
10+
@PropertySource(value="classpath:inject.properties")
11+
@Import(ImportConfig.class)
12+
public class AppConfig {
13+
@Value("${contructor.arg1}") String constructorArg;
14+
15+
@Bean
16+
public InjectedClass injectedClass(){
17+
InjectedClass ic = new InjectedClass(constructorArg);
18+
ic.setMyInt(10);
19+
ic.setTestString("test");
20+
return ic;
21+
}
22+
23+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.java_bean_injection;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9+
import org.springframework.context.support.ClassPathXmlApplicationContext;
10+
11+
import com.baeldung.java_bean_injection.*;
12+
13+
import java.io.File;
14+
import java.util.Scanner;
15+
16+
/**
17+
* Unit test for simple App.
18+
*/
19+
public class AppTest extends TestCase{
20+
/**
21+
* Create the test case
22+
*
23+
* @param testName name of the test case
24+
*/
25+
public AppTest( String testName )
26+
{
27+
super( testName );
28+
}
29+
30+
/**
31+
* @return the suite of tests being tested
32+
*/
33+
public static Test suite()
34+
{
35+
return new TestSuite( AppTest.class );
36+
}
37+
38+
/**
39+
* Rigourous Test :-)
40+
*/
41+
public void testApp()
42+
{
43+
AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext();
44+
context.register(AppConfig.class);
45+
context.refresh();
46+
InjectedClass injectedClass = (InjectedClass) context.getBean(InjectedClass.class);
47+
assertTrue(injectedClass.getMyInt() == 10);
48+
assertTrue(injectedClass.getTestString().equals("test"));
49+
assertNotNull(injectedClass.obj);
50+
assertTrue(injectedClass.myConstructorArg.equals("test"));
51+
}
52+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.java_bean_injection;
2+
3+
4+
public class AutowireObject {
5+
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.java_bean_injection;
2+
3+
import org.springframework.context.annotation.*;
4+
5+
import com.baeldung.java_bean_injection.*;
6+
7+
@Configuration
8+
public class ImportConfig {
9+
10+
@Bean(name="autobean")
11+
public AutowireObject autowireObject(){
12+
return new AutowireObject();
13+
}
14+
15+
16+
@Bean(name="autobean2")
17+
public AutowireObject autowireObject2(){
18+
return new AutowireObject();
19+
}
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.java_bean_injection;
2+
3+
import org.springframework.beans.factory.annotation.*;
4+
import org.springframework.context.annotation.*;
5+
6+
import com.baeldung.java_bean_injection.*;
7+
8+
9+
public class InjectedClass {
10+
private int myInt;
11+
private String testString;
12+
String myConstructorArg;
13+
14+
15+
@Autowired
16+
@Qualifier("autobean")
17+
AutowireObject obj;
18+
19+
public InjectedClass(String constArg){
20+
this.myConstructorArg = constArg;
21+
}
22+
23+
public void setMyInt(int myInt){
24+
this.myInt = myInt;
25+
}
26+
27+
public void setTestString(String testString){
28+
this.testString = testString;
29+
}
30+
31+
public int getMyInt(){
32+
return this.myInt;
33+
}
34+
35+
public String getTestString(){
36+
return this.testString;
37+
}
38+
39+
}

‎spring-groovy-config/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
3+
org.eclipse.jdt.core.compiler.compliance=1.5
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.5
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
groovy.compiler.level=24

0 commit comments

Comments
 (0)