맥북 기준
azulu 에서 인텔 맥 기준 zulu 11 zip으로 다운
압축푼 zulu-11.jdk폴더를 /Library/Java/JavaVirtualMachines로 옮겨준다
/Applications에 가서 open . 으로 열고 STS.app 우클릭 후 패키지 내용 보기
Contents 들어가서 info.plist 파일 우클릭으로 텍스트 편집기 혹은 vs code로 열어서
<!-- to use a specific Java version (instead of the platform's default) uncomment one of the following options,
or add a VM found via $/usr/libexec/java_home -V
<string>-vm</string><string>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Commands/java</string>
<string>-vm</string><string>/Library/Java/JavaVirtualMachines/1.8.0.jdk/Contents/Home/bin/java</string>
-->
주석 부분 밑에
<string>-vm</string><string>/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/bin/java</string>
추가
+) Spring framework 3 에서 낮은 버전으로 받아야 된다
Spring Legacy Project 생성
Project name 정하기
Spring MVC Project 선택
여기서 패키지가 있어야 하기 때문에 패키지 이름 작성
ex) framework.di.test / spring.mvc.start
pom.xml 들어가서
<properties>
<java-version>1.6</java-version>
<org.springframework-version>5.2.9.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
이 부분에 springframework-version을 상위 버전으로 바꾸기 위해 5.2.9.RELEASE로 변경
return 값에 출력할 String 값과 설정한 String msg를 넣어줌
public class Hello {
String msg="오늘은 스프링 배우는 날!!!";
public String getMessage() {
return "Hello메서드 호출: "+msg;
}
}
package framework.di.test;
public class Hello {
String msg="오늘은 스프링 배우는 날!!!";
public String getMessage() {
return "Hello메서드 호출: "+msg;
}
}
Hello bean 설정/등록
<bean name="hello" class="framework.di.test.Hello"></bean>
name은 우리가 정해준 이름 class는 실제 경로
<?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 https://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- Hello bean 설정/등록 -->
<bean name="hello" class="framework.di.test.Hello"></bean>
</beans>
System.out.println("자바방식으로 hello메서드 호출하기");
Hello hello1=new Hello();
System.out.println(hello1.getMessage());
Hello hello2=new Hello();
System.out.println(hello2.getMessage());
//주소값 비교
//java는 new로 생성할 때마다 새로운 주소값 생성하기 때문에 생성주소 다르다
System.out.println(hello1==hello2);
java는 new로 생성할 때마다 새로운 주소값 생성하기 때문에 생성주소 다르다
System.out.println("스프링방식으로 hello메서드 호출하기");
//xml파일 가져오기..웹으로 실행시 web.xml에 설정이 되어 있으므로 필요없다
//src/main/resources에 폴더를 만들면 폴더명/helloContext.xml로 소환해야한다
ApplicationContext app1=new ClassPathXmlApplicationContext("helloContext.xml");
helloContext.xml bean에 설정한 이름 가져오기 1
클래스명 변수명1=(클래스명)a변수명.getBean("xml에서 설정한 이름 중에 가져올 것");
+)getBean은 object반환값이기에 왼쪽에 Hello 자료명으로 맞춰줌
helloContext.xml bean에 설정한 이름 가져오기 2
클래스명 변수명2=a변수명.getBean("helloContext.xml bean에 설정한 이름", 클래스명.class);
//hello객체생성
//helloContext.xml bean에 설정한 이름을 가져오는 것
//방법1
//getBean은 object반환값이기에 왼쪽에 Hello 자료명으로 맞춰줌
Hello h1=(Hello)app1.getBean("hello");
System.out.println(h1.getMessage());
//방법2
//("helloContext.xml bean에 설정한 이름", 클래스명.class)
Hello h2=app1.getBean("hello", Hello.class);
System.out.println(h2.getMessage());
System.out.println(h1==h2); //생성주소 같다..스프링방식에서 기본이 싱글톤(스프링방식에서는 생성하는 주소가 같기 때문)
Spring방식은 기본이 싱글톤 이므로 생성주소 같다
->스프링방식에서는 생성하는 주소가 같기 때문
package framework.di.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("자바방식으로 hello메서드 호출하기");
Hello hello1=new Hello();
System.out.println(hello1.getMessage());
Hello hello2=new Hello();
System.out.println(hello2.getMessage());
//주소값 비교
//java는 new로 생성할 때마다 새로운 주소값 생성하기 때문에 생성주소 다르다
System.out.println(hello1==hello2);
System.out.println("스프링방식으로 hello메서드 호출하기");
//xml파일 가져오기..웹으로 실행시 web.xml에 설정이 되어 있으므로 필요없다
//src/main/resources에 폴더를 만들면 폴더명/helloContext.xml로 소환해야한다
ApplicationContext app1=new ClassPathXmlApplicationContext("helloContext.xml");
//hello객체생성
//helloContext.xml bean에 설정한 이름을 가져오는 것
//방법1
//getBean은 object반환값이기에 왼쪽에 Hello 자료명으로 맞춰줌
Hello h1=(Hello)app1.getBean("hello");
System.out.println(h1.getMessage());
//방법2
//("helloContext.xml bean에 설정한 이름", 클래스명.class)
Hello h2=app1.getBean("hello", Hello.class);
System.out.println(h2.getMessage());
System.out.println(h1==h2); //생성주소 같다..스프링방식에서 기본이 싱글톤(스프링방식에서는 생성하는 주소가 같기 때문)
}
}
생성과 동시에 값 변경가능
String name;
int age;
String addr;
public MyInfo(String name,int age,String addr) {
this.name=name;
this.age=age;
this.addr=addr;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "MyInfo[name="+name+", age="+age+", addr="+addr+"]";
}
package spring.di.ex2;
public class MyInfo {
//생성자 주입..생성과 동시에 값 변경가능
String name;
int age;
String addr;
//생성자 주입을 위해 만들어진 것
public MyInfo(String name,int age,String addr) {
this.name=name;
this.age=age;
this.addr=addr;
}
//출력용
@Override
public String toString() {
// TODO Auto-generated method stub
return "MyInfo[name="+name+", age="+age+", addr="+addr+"]";
}
}
String schoolName;
//MyInfo 에 있는 것을 Person에 주입
MyInfo info;
public Person(MyInfo info) {
super();//super 생략되어있는데 눈에 보여주기 위해 적어놓음
this.info=info;
}
//setter
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
//출력
public void writeData()
{
System.out.println("**학생 정보 출력**");
System.out.println("학교명: "+schoolName);
System.out.println("학생명: "+info.name);
System.out.println("나이: "+info.age);
System.out.println("주소: "+info.addr);
}
package spring.di.ex2;
public class Person {
//setter 주입
String schoolName;
//MyInfo 에 있는 것을 Person에 주입
MyInfo info;
//MyInfo만 생성자 주입으로 만들어 놓음
//생성자
public Person(MyInfo info) {
super();//super 생략되어있는데 눈에 보여주기 위해 적어놓음
this.info=info;
}
//setter
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
//출력
public void writeData()
{
System.out.println("**학생 정보 출력**");
System.out.println("학교명: "+schoolName);
System.out.println("학생명: "+info.name);
System.out.println("나이: "+info.age);
System.out.println("주소: "+info.addr);
}
}
이 컨텍스트는 XML 설정 파일인 "appContext2.xml"을 사용하여 구성됩니다. 이 설정 파일에는 빈(bean) 정의와 의존성 관계가 정의되어 있습니다.
ApplicationContext context=new ClassPathXmlApplicationContext("appContext2.xml");
//MyInfo생성 후 확인
MyInfo my=(MyInfo)context.getBean("my");
System.out.println(my.toString());
System.out.println(my);
//Person에 있는 메서드 출력
Person p=(Person)context.getBean("person");
p.writeData();
package spring.di.ex2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Ex2Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("appContext2.xml");
//MyInfo생성 후 확인
MyInfo my=(MyInfo)context.getBean("my");
System.out.println(my.toString());
System.out.println(my);
//Person에 있는 메서드 출력
Person p=(Person)context.getBean("person");
p.writeData();
}
}
<bean id="my" class="spring.di.ex2.MyInfo">
<constructor-arg value="민희"/>
<constructor-arg value="22"/>
<constructor-arg>
<value>강남구 역삼동 쌍용교육센터</value>
</constructor-arg>
</bean>
MyInfo는 생성자주입
reference주입 a라는 것을 그대로 가져와서 주입하는 것
bean에 설정한 id/name 값을 ref에 넣어줘야한다
<constructor-arg ref="my"/>
학교명은 setter에 주입
<property name="schoolName" value="쌍용고등학교"/>
<bean id="person" class="spring.di.ex2.Person">
<!-- MyInfo는 생성자주입
reference주입 a라는 것을 그대로 가져와서 주입하는 것
bean에 설정한 id/name 값을 ref에 넣어줘야한다 -->
<constructor-arg ref="my"/>
<!-- 학교명은 setter주입 -->
<property name="schoolName" value="쌍용고등학교"/>
</bean>
<?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 https://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="my" class="spring.di.ex2.MyInfo">
<constructor-arg value="민희"/>
<constructor-arg value="22"/>
<constructor-arg>
<value>강남구 역삼동 쌍용교육센터</value>
</constructor-arg>
</bean>
<bean id="person" class="spring.di.ex2.Person">
<!-- MyInfo는 생성자주입
reference주입 a라는 것을 그대로 가져와서 주입하는 것
bean에 설정한 id/name 값을 ref에 넣어줘야한다 -->
<constructor-arg ref="my"/>
<!-- 학교명은 setter주입 -->
<property name="schoolName" value="쌍용고등학교"/>
</bean>
</beans>