Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch08] 8.3 Use anonymous bean
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed May 27, 2017
1 parent 6402c1a commit fb201d0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/main/java/exam/test03/Score.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package exam.test03;

/**
* Created by nhnent on 2017. 5. 27..
*/
public class Score {

private String name;
private float kor;
private float eng;
private float math;

public Score() {

}

public Score(String name, float kor, float eng, float math) {
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
}

public float average() {
return sum() / (float)3;
}

public float sum() {
return kor + eng + math;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getKor() {
return kor;
}

public void setKor(float kor) {
this.kor = kor;
}

public float getEng() {
return eng;
}

public void setEng(float eng) {
this.eng = eng;
}

public float getMath() {
return math;
}

public void setMath(float math) {
this.math = math;
}
}
36 changes: 36 additions & 0 deletions src/main/java/exam/test03/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package exam.test03;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Created by nhnent on 2017. 5. 27..
*/
public class Test {

public static void main(String[] args) {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("file:/Users/nhnent/workspace/workspace-intellij/java_webdev_workbook_spring/src/main/java/exam/test03/beans.xml");

System.out.println("[Print the name of beans stored in container]");

for (String name : ctx.getBeanDefinitionNames()) {
System.out.println(name);
}

System.out.println("[Extract annonymous bean]");
Score score1 = (Score)ctx.getBean("exam.test03.Score");
Score score2 = (Score)ctx.getBean("exam.test03.Score#0");

if (score1 == score2) {
System.out.println("score == score#0");
}

Score score3 = (Score)ctx.getBean("exam.test03.Score#1");
if (score1 != score3) {
System.out.println("score != score#1");
}

System.out.println("[Extract by class type]");
Score score4 =(Score)ctx.getBean(exam.test03.Score.class);
}
}
10 changes: 10 additions & 0 deletions src/main/java/exam/test03/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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 class="exam.test03.Score"></bean>
<bean class="exam.test03.Score"></bean>

</beans>

0 comments on commit fb201d0

Please sign in to comment.