※ 복습
특징
간략한 역사, model1 ==> model2
mvc pattern, ejb, struts
경량, IoC, AOP, 컨테이너
javabeans
IoC >> DL, DI
DI : 의존성 주입, 필요한 객체를 외부(컨테이너)에서 주입 받는 것
spring bean : (컨테이너에 등록된)자바 객체
spring bean config(.xml / 어제 작업한 spring core프로젝트의 ioc.xml 파일)
xml기반 bean : bean태그를 직접 선언
class 속성에 클래스명 id를 통해서 이름 참조
annotation bean : context : component-scan을 이용해 패키지 탐색, 탐색된 패키지 내에 @Component @Service
※ 수업
DI를 사용하는게 관리적인 측면에서 용이하다.
IoC 컨테이너 분류 체게
- setter Injection : class 사이의 의존관계를 연결시키기 위해 setter 메소드를 이용하는 방법
- Constructor Injection : class 사이의 의존관계를 연결시키기 위해 생성자를 이용하는 방법
예제) xml기반 bean탐색
ioc5.xml => namespace에 있는 p 체크
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="ioc.ioc5.Programmer" id="programmer" />
<bean class="ioc.ioc5.Designer" id="designer" />
<bean class="ioc.ioc5.Develope" id="develope" p:emp-ref="programmer" />
</beans>
setter의 xml기반으로 탐색
package shape;
import lombok.Data;
import lombok.Setter;
@Data
public class Circle {
private Point point;
private double r;
public String toString() {
return String.format("중점 x : %d, y : %d인 원의 반지름은 %f입니다", point.getX(), point.getY(), r);
}
}
package shape;
import lombok.Data;
import lombok.Setter;
// 중점
@Data
public class Point {
private int x, y;
}
package shape;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("shape.xml");
// Circle circle = new Circle();
// Point point = new Point();
// point.setX(10);
// point.setY(20);
// circle.setPoint(point);
// circle.setR(5);
// System.out.println(circle);
Circle circle = ctx.getBean(Circle.class);
System.out.println(circle);
ctx.close();
// xml 기반의 circle : DL, point : DI (setter) // shape.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="shape.Circle" id="circle" >
<property name="point" ref="point" />
<property name="r" value="5"></property>
</bean>
<bean class="shape.Point" id="point" >
<property name="x" value="10" />
<property name="y" value="20" />
</bean>
</beans>
생성자 기반으로 탐색
package shape2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.Setter;
@Data
@Component
public class Circle {
@Autowired
private Point point;
@Value("5") //문자열로 기본값을 주지만 내부에서 parsing을 함
private double r;
public String toString() {
return String.format("중점 x : %d, y : %d인 원의 반지름은 %f입니다", point.getX(), point.getY(), r);
}
}
package shape2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.Setter;
// 중점
@Data
@Component
public class Point {
@Value("10") // spring을 통한 bean 생성시에 사용되는 값
private int x;
@Value("20")
private int y;
}
package shape2;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("shape2.xml");
Circle circle = (Circle)ctx.getBean("c");
System.out.println(circle);
ctx.close();
// xml 기반의 circle : DL, point : DI (setter) // shape.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:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="shape2" />
<bean class="shape2.Circle" id="c">
<property name="r" value="10" />
<property name="point">
<bean class="shape2.Point">
<property name="x" value="3" />
<property name="y" value="4" />
</bean>
</property>
</bean>
<!-- <bean class="java.lang.String" c:original="abcd">
</bean> -->
<bean class="java.lang.String" id="str">
<constructor-arg value="가나다라" />
</bean>
</beans>
* CGLIB방식, proxy방식
※ SpEL문법
1.
※ ref 태그를 이용한 빈 주입
- ref사용시 자동완성된 bean을 사용하면 된다.
아래 2개는 잘 안씀
1. ref bean
- ref parent (??? 가 상속관계 일때 사용)
2. ref local
※ Collection 주입
- 빈에서 개별 빈이나 값이 아닌 객체의 컬렉션에 접근해야 되는 경우가 있다.
- list, map, set, prop를 사용
1. map
- e
2. prop (properties)
- 개인정보류를 프로퍼티스에서 이원관리 해야함 (db연결 등)
- 제너릭이 String, String 타입으로 고정되어 있다.
-
*
'Spring > Spring' 카테고리의 다른 글
23-03-29 Spring (0) | 2023.03.30 |
---|---|
23-03-23 Spring (0) | 2023.03.23 |
23-03-22 Spring (0) | 2023.03.22 |
23-03-17 Spring (0) | 2023.03.17 |
23-03-16 Spring (0) | 2023.03.16 |