
Spring Framework Structure

Spring Framework의 기초인 Core에 대해서 Araboza!
POJO(Plain Old Java Object)
FQCN(Fully Qualified Class Name)
Bean
Annotation
Meta-data
DI(Dependency Injection)
DL(Dependency LookUp)
OGNL(Object-Graph Navigation Language)
JNDI(Java Naming and Directory Interface)
Framework
Objective : 빠른퇴근, 유지보수의 용이함
Point 1: 다른 빈을 사용하고자할때 수정없이 대응할수있다면??
Point 2: 코드수정없이 대응하는방법은??
Point 3: class 간 Coupling Relation에서 Polymorphism 적용 시 Loose Coupling이 발생
**다형화를 적용시키면 약간 유연해진다 여기에 IOC개념이들어가야 완벽**
Point 4:

Spring Framework
Spring Framework의 장점: 다른 Framework 연동이 쉽다, view만 지원하는 프레임워크에비해 MVC를 모두 지원한다
~.xml에 태그 내에 Attribute로 사용할 클래스를 정의
<bean id="ex1" class="spring.service.ex1"/>
IoC Container를 사용하여 DI 수행
<bean id="player1" class="spring.service.Player1">
<property name="ex">
<ref bean="ex1"/> ===> <property name="ex" ref="ex1"/>
</property>
</bean>
<property> 태그는 객체의 속성 값을 설정하거나 의존성을 주입하는 데 사용됨
name은 setter method를 뜻함 ex일경우 setEx와 같음
<bean id="player1" class="spring.service.Player1">
<constructor-arg>
<ref bean="ex1"/> ===> <constructor-arg ref="ex1"/>
</constructor-arg>
</bean>
<constructor-arg>태그는 ref의 객체를 player1의 생성자 인자로 주입함
Bean Container를 추상화한 인터페이스 사용으로 IoC Container 사용
BeanFactory factory =
new XmlBeanFactory(new FileSystemResource("xml FQCN"));
//Bean의 생성, 관리, 검색을 수행하는 인터페이스. ==> Bean Container
//BeanFactory는 IoC 컨테이너의 핵심 역할을 담당.
//Bean의 메타데이터를 읽어와 객체를 생성하고 관리하는 역할.
//getBean을 통해 meta-data에 기술되어있는 객체를 가져오는 LookUp의 과정이 IoC
//전체적인 과정을 통틀어 IoC Container = SpringFramework라고함
ApplicationContext factory =
new ClassPathXmlApplicationContext("xml FQCN");
//ApplicationContext Interface를통해 인스턴스를 pre-loading
<bean id="lifeCycle1"
class="spring.service.LifeCycle01"
init-method="init" <!-- LifeCycle01 내 O/R된 init method 호출 -->
destroy-method="destroy" <!-- LifeCycle01 내 O/R된 destroy method 호 -->
depends-on="lifeCycle02,lifeCycle03" <!--DI 순서 정의-->
scope="singleton" /> <!-- 패턴정의, 싱글톤:최초한번만 생성-->
<bean id="lifeCycle02" class="spring.service.LifeCycle02"/>
<bean id="lifeCycle03" class="spring.service.LifeCycle03"/>
<bean id="lifeCycle04"
class="spring.service.LifeCycle04"
scope="prototype"/>
<!--위의 코드에서 Wiring에 해당하는 부분은 depends-on이다. -->
<!--Wiring: 객체간의 의존성을 설정하고 주입하는 과정-->
개발자정의 및 API 인스턴스 생성
<!--개발자정의 인스턴스 생성의 기존코드-->
<!--User user = new User()
user.setUserId("유저")
user.setAge(10)-->
<!--개발자정의 인스턴스 생성-->
<bean id="user" class="spring.service.User">
<property name="userId" value="유저"/>
<property name="Age" value="20"/>
</bean>
<!--API 인스턴스 생성의 기존코드-->
<!--String password = new String("7777")-->
<!--API 인스턴스 생성-->
<bean id="password" class="java.lang.String">
<construtor-arg value="7777">
</bean>
<!--생성자 주입에의한 인자전달 시 모호한 순서 발생-->
<!--이를 해결하기위해 index or type Attribute사용-->
<!--properties 파일의 내용-->
db.url=jdbc:mysql://localhost:3306/mydb
db.username=myuser
db.password=mypassword
<!--=======================-->
<!-- PropertyPlaceholderConfigurer 빈 등록 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:app.properties</value>
<!--classpath: 는 생략해도되지만 꼭!!! 써주도록하자(가독성)-->
</list>
</property>
</bean>
<!-- 빈 정의 및 프로퍼티 사용 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<!--대치변수의 형태인 ${}로 접근 가능 ==> jsp의 EL과 유사-->
</bean>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- util:properties를 사용하여 정적인 프로퍼티 값 정의 -->
<util:properties id="staticProperties">
<prop key="key1">Value 1</prop>
<prop key="key2">Value 2</prop>
</util:properties>
<!-- util:properties를 이용하여 프로퍼티 값을 빈에 주입 -->
<bean id="staticBean" class="spring.service.MyBean">
<property name="propertyMap" ref="staticProperties" />
</bean>
<!-- context:property-placeholder를 사용하여 외부 프로퍼티 파일 로드 -->
<context:property-placeholder location="classpath:app.properties" />
<!-- context:property-placeholder를 이용하여 프로퍼티 값을 빈에 주입 -->
<bean id="dynamicBean" class="spring.service.MyBean">
<property name="propertyMap">
<map>
<entry key="db.url" value="${db.url}" />
<entry key="db.username" value="${db.username}" />
</map>
</property>
</bean>
</beans>util:properties를 통해 외부 프로퍼티 파일을 로드하고 #{} 표현식을 사용하여 프로퍼티 값을 접근하는 것은 스프링의 SpEL (Spring Expression Language)의 기능. 이는 OGNL (Object-Graph Navigation Language)과는 유사한 개념#{} 표현식을 사용하여 빈의 프로퍼티 값을 참조하거나 연산을 수행할 수 있다.util:properties를 통해 로드한 프로퍼티 값을 #{} 표현식을 사용하여 접근하고 조작하는 것은 SpEL의 기능이며, OGNL과는 개념적으로 유사하지만 다른 기술이다.스키마의 bean root elements에 default-method 작성
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-init-method="init"
default-destroy-method="destroy">
<!-- 빈 정의 -->
<bean id="bean1" class="spring.service.Bean1" />
<bean id="bean2" class="spring.service.Bean2" />
<!-- ... -->