GenericXmlApplication ctx = newGenericXmlApplicationContect(); ----------> 스프링컨테이너생성
ctx.load("classpath.applicationCTX.xml"); ----> 스프링 컨테이너 설정 load를 해서 xml파일을 가져오는경우 반드시 refresh필요
ctx.refresh();
Student student =ctx.getbean("student", Student.class); ----------> 스프링컨테이너사용
System.out.println("이름" + student.getName);
ctx.close(); 종료
스프링 생명 주기
package com.javalec.ex;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Student implements InitializingBean, DisposableBean{
초기화할때, 빈소멸과정에서 오버라이딩에 의해 빈생성
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public void afterPropertiesSet() throws Exception { ///초기화될때 afferPropertiesSet이라는 메소드가 생성
// TODO Auto-generated method stub
System.out.println("afterPropertiesSet()");
}
@Override
public void destroy() throws Exception { ///빈소멸될때 afferPropertiesSet이라는 메소드가 생성
// TODO Auto-generated method stub
System.out.println("destroy()");
}
}
어노테이션 방식
package com.javalec.ex;
import javax.annotation.*;
public class OtherStudent {
private String name;
private int age;
public OtherStudent(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@PostConstruct
public void initMethod() {
System.out.println("initMethod()");
}
@PreDestroy
public void destroyMethod() {
System.out.println("destroyMethod()");
}
}
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");
Student student1 = ctx.getBean("student", Student.class);
System.out.println("이름 : " + student1.getName());
System.out.println("나이 : " + student1.getAge());
System.out.println("==============================");
Student student2 = ctx.getBean("student", Student.class); //student1과 student2의 객체는 같다.
student2.setName("홀길자"); //홀길자로 변경
student2.setAge(100); //나이변경
System.out.println("이름 : " + student1.getName());
System.out.println("나이 : " + student1.getAge());
System.out.println("==============================");
if(student1.equals(student2)) {
System.out.println("student1 == student2");
} else {
System.out.println("student1 != student2");
}
ctx.close();
}
}
package com.javalec.ex;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
<?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="student" class="com.javalec.ex.Student" scope="singleton">
<constructor-arg value="홍길순"></constructor-arg>
<constructor-arg value="30"></constructor-arg>
</bean>
</beans>
'프로그래밍 > spring' 카테고리의 다른 글
5.DI활용(의존 관계, di사용에 따른 장점) (492) | 2017.11.26 |
---|---|
4.DI(Dependency Injection)-II (1162) | 2017.11.22 |
3. DI(dependency Injection-1) (1199) | 2017.11.22 |
2.스프링 프로젝트 및 DI ,IDC (1178) | 2017.11.21 |
1.스프링 (1175) | 2017.11.20 |