[Spring] Xml Java vs XML
- Web/Spring
- 2020. 10. 21.
공통 코드
더보기
xml 과 java식 공통 코드
Student.java
package com.day02.Ex01.Java;
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.day02.Ex01.Java;
public class StudentInfo {
private Student student;
public StudentInfo() {
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
Family.java
package com.day02.Ex01.Java;
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;
}
}
Java 방식
더보기
Java 방식
ApplicationConfig.java
package com.day02.Ex01.Java;
import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig {
@Bean
public Student student1()
{
ArrayList<String> hobby = new ArrayList<String>();
hobby.add("수영");
hobby.add("요리");
Student student = new Student("홍길동", 20, hobby);
student.setHeight(80);
student.setWeight(180);
return student;
}
@Bean
public StudentInfo sutudentInfo1()
{
StudentInfo studentInfo = new StudentInfo();
studentInfo.setStudent(student1());
return studentInfo;
}
}
ApplicationConfig1.java
package com.day02.Ex01.Java;
import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig1 {
@Bean
public Student student3()
{
ArrayList<String> hobby = new ArrayList<String>();
hobby.add("수영");
hobby.add("요리");
Student student = new Student("홍길동", 20, hobby);
student.setHeight(80);
student.setWeight(180);
return student;
}
@Bean
public Family femily()
{
Family family = new Family("홍아빠", "홍엄마");
family.setBrotherName("홍오빠");
family.setSisterName("홍언니");
return family;
}
}
MainClass.java
package com.day02.Ex01.Java;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.day02.Ex01.*;
public class MainClass {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Student student1 = ctx.getBean("student1", Student.class);
System.out.println(student1.getName()); // 홍길동
System.out.println(student1.getHobbys()); // 수영, 요리
StudentInfo studentInfo = ctx.getBean("sutudentInfo1", StudentInfo.class);
Student student2 = studentInfo.getStudent(); // student1 == student2
System.out.println(student2.getName()); // 홍길동
System.out.println(student2.getHobbys()); // 수영, 요리
if (student1.equals(student2)) {
System.out.println("student1 == student2");
}
//2번째
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();
}
}
XML 방식
더보기
XML 방식
StudentFamilCTX.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="student1" class="com.Ex03.Student" c:name="홍길동" c:age="15" p:height="90" p:weight="180">
<constructor-arg>
<list>
<value>수영</value>
<value>요리</value>
</list>
</constructor-arg>
</bean>
<bean id="sutudentInfo1" class="com.Ex03.StudentInfo" p:student-ref="student1">
</bean>
</beans>
StudentFamilCTX1.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student3" class="com.Ex03.Student" c:name="홍길동" c:age="15" p:height="90" p:weight="180">
<constructor-arg>
<list>
<value>수영</value>
<value>요리</value>
</list>
</constructor-arg>
</bean>
<bean id="family" class="com.Ex03.Family" c:papaName="홍아빠"
c:mamiName="홍엄마" p:sisterName="홍언니" p:brotherName="홍오빠" />
</beans>
MainClass.java
package com.Ex03;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
String configLocation1 = "classpath:StudentFamilyCTX.xml";
String configLocation2 = "classpath:StudentFamilyCTX1.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("sutudentInfo1", StudentInfo.class);
Student student2 = studentInfo.getStudent(); //student1 == student2
System.out.println(student2.getName()); //홍길동
System.out.println(student2.getHobbys()); // 수영, 요리
if(student1.equals(student2)) {
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();
}
}
전 과정하고 다르게 new GenericXmlApplicationContext 사용하였다.
그러나 xml방식과 java방식 모두 2개를 주입해도 사용 되는 것을 확인 할 수 있다. 그러므로 이는 오버로딩을 확인하게 된다.
xml 방식은 setter 부분이 xml에다가 넣었다면 java 방식은 java 코드 방식으로 넣었다는 점이다.
그러나 Java 방식에는 @Configuration과 @Beans가 있다는 것을 알 수 있다.
이는 어노테이션이라고 부르는 연결방식이다.
Java를 안 다면, 누구나 접할 수 있다는 장점이 있다.
반응형
'Web > Spring' 카테고리의 다른 글
[Spring] 외부의 xml 자료를 가져오기 (0) | 2020.10.22 |
---|---|
[Spring] xml 시작전, 시작후 동작하는 메소드 (0) | 2020.10.21 |
[Spring] xml 활용하기 (0) | 2020.10.21 |
[Spring] xml사용하기1 (0) | 2020.10.20 |
[Spring] xml 사용하기3 (0) | 2020.10.20 |