728x90
Spring Framework에서 빈(Bean)을 정의하고 구성하는 데 사용되는 주요 어노테이션
DI는 resource 디렉토리에 따로 config 를 만들고 xml을 넣어서 bean으로 클래스들을 관리했는데
이번에는 config를 java디렉토리 밑에 생성해서 클래스를 만들고 그 클래스를 통해 다른 클래스들을 관리한다.
@Configration - 수동주입
package main;
import beans.Test;
import config.BeanClass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String[] args) {
//파일형식이 자바이기 때문에 클래스 바뀜
AnnotationConfigApplicationContext ctx=
new AnnotationConfigApplicationContext(BeanClass.class);
Test t1=ctx.getBean("test1", Test.class);
System.out.println(t1);
Test t11=ctx.getBean("test1", Test.class);
System.out.println(t11);
Test t12=ctx.getBean("test2", Test.class);
System.out.println(t11);
}
}
package config;
import beans.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//현재 자바 파일이 빈 등록을 하기위한 (객체등록) 자바파일임을 알리는 어노테이션
public class BeanClass {
@Bean
public Test test1() { // 메소드 이름이 bean의 이름(주소값)이 된다
Test t1 = new Test();
return t1;
}
@Bean(name = "test2")
public Test test11() {
Test t1 = new Test();
return t1;
}
}
package beans;
public class Test {
public Test() {
System.out.println("test");
}
}
@Bean @Lazy (싱글톤)
package main;
import beans.Test;
import beans.Test2;
import config.BeanClass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String[] args) {
//파일형식이 자바이기 때문에 클래스 바뀜
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(BeanClass.class);
Test2 t2 = ctx.getBean("test3", Test2.class);
System.out.println(t2);
Test2 t22 = ctx.getBean("test3", Test2.class);
System.out.println(t22);
}
}
package config;
import beans.Test;
import beans.Test2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
@Configuration
//현재 자바 파일이 빈 등록을 하기위한 (객체등록) 자바파일임을 알리는 어노테이션
public class BeanClass {
@Bean
public Test test1() { // 메소드 이름이 bean의 이름(주소값)이 된다
Test t1 = new Test();
return t1;
}
@Bean(name = "test2")
public Test test11() {
Test t1 = new Test();
return t1;
}
@Bean
@Lazy
public Test2 test3() {
Test2 t2 = new Test2();
return t2;
}
}
package beans;
public class Test2 {
public Test2() {
System.out.println("test2");
}
}
@Bean @Scope("prototype") (싱글톤X)
package main;
import beans.Test;
import beans.Test2;
import beans.Test3;
import config.BeanClass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String[] args) {
//파일형식이 자바이기 때문에 클래스 바뀜
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(BeanClass.class);
Test3 t2 = ctx.getBean("test4", Test3.class);
System.out.println(t2);
Test3 t22 = ctx.getBean("test4", Test3.class);
System.out.println(t22);
}
}
package config;
import beans.Test;
import beans.Test2;
import beans.Test3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
@Configuration
//현재 자바 파일이 빈 등록을 하기위한 (객체등록) 자바파일임을 알리는 어노테이션
public class BeanClass {
@Bean
public Test test1() { // 메소드 이름이 bean의 이름(주소값)이 된다
Test t1 = new Test();
return t1;
}
@Bean(name = "test2")
public Test test11() {
Test t1 = new Test();
return t1;
}
@Bean
@Lazy
public Test2 test3() {
Test2 t2 = new Test2();
return t2;
}
@Bean
@Scope("prototype")
public Test3 test4() {
Test3 t3 = new Test3();
return t3;
}
}
package beans;
public class Test3 {
public Test3() {
System.out.println("test3");
}
}
Spring Framework를 사용하여 빈을 초기화하고 소멸하는 방법을 보여주는 예시
package main;
import beans.Test;
import config.BeanClass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String[] args) {
//파일형식이 자바이기 때문에 클래스 바뀜
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(BeanClass.class);
Test t1 = ctx.getBean("test1", Test.class);
ctx.close();
}
}
package config;
import beans.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
@Configuration
//현재 자바 파일이 빈 등록을 하기위한 (객체등록) 자바파일임을 알리는 어노테이션
public class BeanClass {
@Bean(initMethod = "init", destroyMethod = "destroy")
@Lazy
public Test test1() {
return new Test();
}
}
package beans;
public class Test {
public Test() {
System.out.println("test");
}
public void init() {
System.out.println("Test init");
}
public void destroy() {
System.out.println("Test destroy");
}
}
JavaDI - 자바파일로 주입 (생성자를 통한 주입)
package main;
import beans.Test;
import config.BeanClass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(BeanClass.class);
Test t1 = ctx.getBean("test1", Test.class);
System.out.println(t1.getD1());
System.out.println(t1.getD2());
System.out.println(t1.getD3());
}
}
package config;
import beans.Data;
import beans.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//현재 자바 파일이 빈 등록을 하기위한 (객체등록) 자바파일임을 알리는 어노테이션
public class BeanClass {
@Bean
public Test test1() {
return new Test(10, "spring", new Data()); //생성자를 통한 주입
}
}
package beans;
public class Test {
private int d1;
private String d2;
private Data d3;
public Test() {}
public Test(int d1, String d2, Data d3) {
this.d1 = d1;
this.d2 = d2;
this.d3 = d3;
}
public int getD1() {
return d1;
}
public void setD1(int d1) {
this.d1 = d1;
}
public String getD2() {
return d2;
}
public void setD2(String d2) {
this.d2 = d2;
}
public Data getD3() {
return d3;
}
public void setD3(Data d3) {
this.d3 = d3;
}
}
setter를 통한 주입
package main;
import beans.Test;
import config.BeanClass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(BeanClass.class);
Test t1 = ctx.getBean("test2", Test.class);
System.out.println(t1.getD1());
System.out.println(t1.getD2());
System.out.println(t1.getD3());
}
}
package config;
import beans.Data;
import beans.Test;
import beans.Test2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//현재 자바 파일이 빈 등록을 하기위한 (객체등록) 자바파일임을 알리는 어노테이션
public class BeanClass {
@Bean
public Test test2() {
Test t1 = new Test();
t1.setD1(10); //setter 메소드를 통한 주입
t1.setD2("spring");
t1.setD3(new Data());
return t1;
}
}
package beans;
public class Test {
private int d1;
private String d2;
private Data d3;
public Test() {}
public Test(int d1, String d2, Data d3) {
this.d1 = d1;
this.d2 = d2;
this.d3 = d3;
}
public int getD1() {
return d1;
}
public void setD1(int d1) {
this.d1 = d1;
}
public String getD2() {
return d2;
}
public void setD2(String d2) {
this.d2 = d2;
}
public Data getD3() {
return d3;
}
public void setD3(Data d3) {
this.d3 = d3;
}
}
@Value를 활용한 주입
package main;
import beans.Test;
import beans.Test2;
import config.BeanClass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(BeanClass.class);
Test2 t2 = ctx.getBean("test2", Test2.class);
System.out.println(t2.getD1());
System.out.println(t2.getD2());
System.out.println(t2.getD3());
System.out.println(t2.getD4());
}
}
package config;
import beans.Data;
import beans.Data2;
import beans.Test;
import beans.Test2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//현재 자바 파일이 빈 등록을 하기위한 (객체등록) 자바파일임을 알리는 어노테이션
public class BeanClass {
@Bean
public Test test1() {
return new Test();
}
@Bean
public Data data1() {
return new Data();
}
/////////////////// qualifier이름 설정한거랑 똑같은 이름으로 멧도드를 설정해야 자동주입됨
@Bean
public Data2 obj1() {
return new Data2();
}
@Bean
public Data2 obj2() {
return new Data2();
}
@Bean
public Test2 test2() {
return new Test2();
}
}
package beans;
import org.springframework.beans.factory.annotation.Value;
public class Test2 {
private int d1;
private String d2;
private Data3 d3;
private Data4 d4;
public Test2() {}
public Test2(@Value("1") int d1, @Value("spring")String d2, Data3 d3, Data4 d4) {
this.d1 = d1;
this.d2 = d2;
this.d3 = d3;
this.d4 = d4;
}
public int getD1() {
return d1;
}
public void setD1(int d1) {
this.d1 = d1;
}
public String getD2() {
return d2;
}
public void setD2(String d2) {
this.d2 = d2;
}
public Data3 getD3() {
return d3;
}
public void setD3(Data3 d3) {
this.d3 = d3;
}
public Data4 getD4() {
return d4;
}
public void setD4(Data4 d4) {
this.d4 = d4;
}
}
728x90
'Frameworks > Spring' 카테고리의 다른 글
[Spring] MVC (Form요소,태그 / forward/redirect / Request 빈 주입) (2) | 2024.05.08 |
---|---|
[Spring] MVC (추출/ 주입받기 (Mapping / viewResolver)) (2) | 2024.05.08 |
[Spring] @Autowired, @Component를 활용한 자동 구성 (0) | 2024.05.02 |
[Spring] setDI / AutoDI / CollectionDI (0) | 2024.05.02 |
[Spring] 스프링 프레임워크 개념 (0) | 2024.05.01 |