- Spring project name : FirstMVC
- 디렉터리 구조
Calculator.java
package com.javalec.ex;
public class Calculator {
public Calculator() {
}
public void addition(int f, int s) {
int result = f + s;
System.out.println(f + " + " + s + " = " + result);
}
public void subtraction(int f, int s) {
int result = f - s;
System.out.println(f + " - " + s + " = " + result);
}
public void multiplication(int f, int s) {
int result = f * s;
System.out.println(f + " * " + s + " = " + result);
}
public void division(int f, int s) {
int result = f / s;
System.out.println(f + " / " + s + " = " + result);
}
}
MainClass.java
package com.javalec.ex;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
// Calculator calculation = new Calculator();
// calculation.setFirstnum(10);
// calculation.setSecondnum(2);
//
// calculation.addition();
// calculation.subtraction();
// calculation.multiplication();
// calculation.division();
// MyCalculator myCalculator = new MyCalculator(new Calculator());
// myCalculator.setCalculator(new Calculator()); // DI 의존성 주입
//
// myCalculator.setFirstNum(10);
// myCalculator.setSecondNum(2);
//
// myCalculator.add();
// myCalculator.sub();
// myCalculator.mul();
// myCalculator.div();
String configLocation = "classpath:applicationCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocation);
MyCalculator myCalculator = ctx.getBean("myCalculator", MyCalculator.class);
myCalculator.add();
myCalculator.sub();
myCalculator.mul();
myCalculator.div();
}
}
MyCalculator
package com.javalec.ex;
public class MyCalculator {
Calculator calculator;
private int firstNum;
private int secondNum;
// public MyCalculator(Calculator calculator) {
// this.calculator = calculator;
// }
public void add() {
calculator.addition(firstNum, secondNum);
}
public void sub() {
calculator.subtraction(firstNum, secondNum);
}
public void mul() {
calculator.multiplication(firstNum, secondNum);
}
public void div() {
calculator.division(firstNum, secondNum);
}
public void setFirstNum(int firstNum) {
this.firstNum = firstNum;
}
public void setCalculator(Calculator calculator) {
this.calculator = calculator;
}
public void setSecondNum(int secondNum) {
this.secondNum = secondNum;
}
}
applicationCTX
<?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="calculator" class="com.javalec.ex.Calculator" />
<bean id="myCalculator" class="com.javalec.ex.MyCalculator">
<property name="calculator">
<ref bean="calculator" />
</property>
<property name="firstNum" value="10" />
<property name="secondNum" value="2"></property>
</bean>
</beans>
- Spring project name : secondMVC
- 디렉터리 구조
BMICalculator.java
package com.javalec.ex;
public class BMICalculator {
private double lowWeight;
private double normal;
private double overWeight;
private double obesity;
public void bmicalculation(double weight, double height) {
double h = height * 0.01 ;
double result = weight / (h*h);
System.out.println("BMI 吏��닔 : " + (int)result);
if(result > obesity) {
System.out.println("鍮꾨쭔 �엯�땲�떎.");
} else if(result > overWeight) {
System.out.println("怨쇱껜以� �엯�땲�떎.");
}else if(result > normal) {
System.out.println("�젙�긽 �엯�땲�떎.");
}else {
System.out.println("��泥댁쨷 �엯�땲�떎.");
}
}
public void setLowWeight(double lowWeight) {
this.lowWeight = lowWeight;
}
public void setNormal(double normal) {
this.normal = normal;
}
public void setOverWeight(double overWeight) {
this.overWeight = overWeight;
}
public void setObesity(double obesity) {
this.obesity = obesity;
}
}
MainClass.java
package com.javalec.ex;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
String configLocation = "classpath:applicationCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocation);
MyInfo myInfo = ctx.getBean("myInfo",MyInfo.class);
myInfo.getInfo();
ctx.close();
}
}
MyInfo.java
package com.javalec.ex;
import java.util.ArrayList;
public class MyInfo {
private String name;
private double height;
private double weight;
private ArrayList<String> hobbys;
private BMICalculator bmiCalculator;
public BMICalculator getBmiCalculator() {
return bmiCalculator;
}
public void setBmiCalculator(BMICalculator bmiCalculator) {
this.bmiCalculator = bmiCalculator;
}
public void setName(String name) {
this.name = name;
}
public void setHeight(double height) {
this.height = height;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setHobbys(ArrayList<String> hobbys) {
this.hobbys = hobbys;
}
public void bmiCalculation() {
bmiCalculator.bmicalculation(weight, height);
}
public void getInfo() {
System.out.println("이름 : " + name);
System.out.println("키 : " + height);
System.out.println("몸무게 : " + weight);
System.out.println("취미 : " + hobbys);
bmiCalculation();
}
}
applicationCTX.xml
<?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="bmiCalculator" class="com.javalec.ex.BMICalculator" >
<property name="lowWeight"><value>18.5</value></property>
<property name="normal"><value>23</value></property>
<property name="overWeight"><value>25</value></property>
<property name="obesity"><value>30</value></property>
</bean>
<bean id="myInfo" class="com.javalec.ex.MyInfo">
<property name="name">
<value>홍길동</value>
</property>
<property name="height" value="187" />
<property name="weight" value="84"></property>
<property name="hobbys">
<list>
<value>수영</value>
<value>요리</value>
<value>독서</value>
</list>
</property>
<property name="bmiCalculator">
<ref bean="bmiCalculator"/>
</property>
</bean>
</beans>
- Spring project name : ThirdMVC
- 디렉터리 구조
MainClass.java
package com.javalec.ex;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main (String[] args) {
String configLocation = "classpath:applicationCTX.xml";
AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocation);
StudentInfo studentInfo = ctx.getBean("studentInfo", StudentInfo.class);
studentInfo.getStudentInfo();
Student student2 = ctx.getBean("student2", Student.class);
studentInfo.setStudent(student2);
studentInfo.getStudentInfo();
ctx.close();
}
}
Student.java
package com.javalec.ex;
public class Student {
private String name;
private String age;
private String gradeNum;
private String classNum;
public Student(String name,String age,String gradeNum, String classNum) {
this.name = name;
this.age = age;
this.gradeNum = gradeNum;
this.classNum = classNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGradeNum() {
return gradeNum;
}
public void setGradeNum(String gradeNum) {
this.gradeNum = gradeNum;
}
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
}
StudentInfo.java
package com.javalec.ex;
public class StudentInfo {
private Student student;
public StudentInfo(Student student) {
this.student = student;
}
public void getStudentInfo() {
if(student != null) {
System.out.println("이름 : " + student.getName());
System.out.println("나이 : " + student.getAge());
System.out.println("학년 : " + student.getGradeNum());
System.out.println("반 : " + student.getClassNum());
System.out.println("======================");
}
}
public void setStudent(Student student) {
this.student = student;
}
}
applicationCTX.xml
<?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="student1" class = "com.javalec.ex.Student">
<constructor-arg>
<value>홍길동</value>
</constructor-arg>
<constructor-arg>
<value>10살</value>
</constructor-arg>
<constructor-arg>
<value>3학년</value>
</constructor-arg>
<constructor-arg>
<value>20번</value>
</constructor-arg>
</bean>
<bean id="student2" class = "com.javalec.ex.Student">
<constructor-arg value="홍길동"/>
<constructor-arg value="9살"/>
<constructor-arg value="2학년"/>
<constructor-arg value="10번"/>
</bean>
<bean id="studentInfo" class = "com.javalec.ex.StudentInfo">
<constructor-arg>
<ref bean="student1"/>
</constructor-arg>
</bean>
</beans>
- Spring project name : FMVC
- 디렉터리 구조
MainClass.java
package com.javalec.ex;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX.xml");
Pencil pencil = ctx.getBean("pencil",Pencil.class);
pencil.use();
ctx.close();
}
}
Pencil.java
package com.javalec.ex;
public interface Pencil {
public void use(); // 추상메서드
}
Pencil4B.java
package com.javalec.ex;
public class Pencil4B implements Pencil{
public void use() {
System.out.println("4B 굵기로 쓰인다.");
}
}
Pencil6B.java
package com.javalec.ex;
public class Pencil6B implements Pencil{
@Override
public void use() {
System.out.println("6B 굵기로 쓰인다.");
}
}
Pencil6BWithEraser.java
package com.javalec.ex;
public class Pencil6BWithEraser extends Pencil6B implements Pencil {
@Override
public void use() {
super.use();
System.out.println("6B 굵기로 쓰이고, 지우개가 있다.");
}
}
applicationCTX.xml
<?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 = "pencil" class="com.javalec.ex.Pencil6BWithEraser"/>
</beans>
Spring project name : TestMVC
- 디렉터리 구조
Family.java
package com.javalec.ex;
public class Family {
String papaName; // 생성자를 통한 초기화
String mamiName;
String sisterName; // 세터를 통한 초기화
String brotherName;
public Family(String papaName, String mamiName) {
this.papaName = papaName;
this.mamiName = mamiName;
}
public String getPapaName() {
return papaName;
}
public void setPapaName(String papaName) {
this.papaName = papaName;
}
public String getMamiName() {
return mamiName;
}
public void setMamiName(String mamiName) {
this.mamiName = mamiName;
}
public String getSisterName() {
return sisterName;
}
public void setSisterName(String sisterName) {
this.sisterName = sisterName;
}
public String getBrotherName() {
return brotherName;
}
public void setBrotherName(String brotherName) {
this.brotherName = brotherName;
}
}
MainClass.java
package com.javalec.ex;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.javalec.ex.Family;
import com.javalec.ex.Student;
import com.javalec.ex.StudentInfo;
public class MainClass {
public static void main(String[] args) {
String configLocation1 = "classpath:applicationCTX.xml";
String configLocation2 = "classpath:applicationCTX1.xml";
AbstractApplicationContext ctx
= new GenericXmlApplicationContext(configLocation1, configLocation2);
Student student1 = ctx.getBean("student1",Student.class);
System.out.println(student1.getName()); // 홍길동
System.out.println(student1.getHobbys()); // 수영 요리
StudentInfo studentInfo = ctx.getBean("studentInfo1", StudentInfo.class);
Student student2 = studentInfo.getStudent(); // student == student2
System.out.println(student2.getName()); // 홍길동
System.out.println(student2.getHobbys()); // 수영 요리
if(student1.equals(student2)) { // student 1과 studentInfo(student2)가 같으면 ref 참조쓰기
System.out.println("student1 == student2");
}
Student student3 = ctx.getBean("student3", Student.class);
System.out.println(student3.getName());
if(student1.equals(student3)) {
System.out.println("student1 == student3");
}else {
System.out.println("student1 != student3");
}
Family family = ctx.getBean("family", Family.class);
System.out.println(family.getPapaName());
System.out.println(family.getMamiName());
System.out.println(family.getSisterName());
System.out.println(family.getBrotherName());
ctx.close();
}
}
Student.java
package com.javalec.ex;
import java.util.*;
public class Student {
private String name;
private int age;
private ArrayList<String> hobbys;
private double height;
private double weight;
public Student(String name, int age, ArrayList<String> hobbys) {
this.name = name;
this.age = age;
this.hobbys = hobbys;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ArrayList<String> getHobbys() {
return hobbys;
}
public void setHobbys(ArrayList<String> hobbys) {
this.hobbys = hobbys;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
StudentInfo.java
package com.javalec.ex;
public class StudentInfo {
// 레퍼런스 참조만 되게끔
private Student student;
public StudentInfo() {
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
applicationCTX.xml
<?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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentInfo1" class="com.javalec.ex.StudentInfo">
<property name="student">
<ref bean="student1"></ref>
</property>
</bean>
<bean id="student1" class="com.javalec.ex.Student" c:name="홍길동"
c:age="20">
<!-- <constructor-arg> <value>홍길동</value> </constructor-arg> <constructor-arg>
<value>19</value> </constructor-arg> -->
<constructor-arg>
<list>
<value>수영</value>
<value>요리</value>
</list>
</constructor-arg>
</bean>
<bean id="student3" class="com.javalec.ex.Student" c:name="홍길자"
c:age="30">
<!-- <constructor-arg> <value>홍길자</value> </constructor-arg> <constructor-arg>
<value>30</value> </constructor-arg> -->
<constructor-arg>
<list>
<value>히히</value>
<value>하하</value>
</list>
</constructor-arg>
</bean>
</beans>
applicationCTX1.xml
<?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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="family" class="com.javalec.ex.Family" c:papaName="홍아빠"
c:mamiName="홍엄마" p:sisterName="홍누나" p:brotherName="홍오빠">
<!-- <constructor-arg> <value>홍아빠</value> </constructor-arg> <constructor-arg>
<value>홍엄마</value> </constructor-arg> <property name="sisterName" value="홍누나"/>
<property name="brotherName" value="홍오빠"/> -->
</bean>
</beans>
반응형
'자바과정 > 스프링' 카테고리의 다른 글
스프링 라이프사이클 실습(생명주기, Bean 객체 생성 - 소멸) (0) | 2021.04.06 |
---|---|
라이프 사이클이란? (0) | 2021.04.06 |
스프링 실습 예제 (어노테이션, Configuration/Bean) (0) | 2021.04.06 |
스프링 namespace : c와 p 사용 (0) | 2021.04.05 |
스프링 과 메이븐 디렉터리 구조 (0) | 2021.04.05 |
댓글