스프링의 주요 기능
-> 강한 결합과 약한 결합
서로 관련있는 기능들은 강하게 결합(tightly coupled)하고, 관련이 없는 기능들은 약하게 결합(loosely coupled) 해야 한다
package com.sping.ex02;
public interface PersonService {
// 1. 추상 메소드로 작성
public void sayHello();
}
package com.sping.ex02;
// 2. 구현 클래스 작성
public class PersonServiceImpl implements PersonService {
private String name;
private int age;
// 3. 값 받을 setter 작성
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
// 4. 오버라이드
@Override
public void sayHello() {
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- class : 연결할 파일 경로 지정 -->
<bean id="personService02" class="com.sping.ex02.PersonServiceImpl">
<!-- 넣을 변수명과 값 작성 -->
<property name="name">
<value>홍길순</value>
</property>
<property name="age">
<value>20</value>
</property>
</bean>
</beans>
package com.sping.ex02;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class PersonTest2 {
public static void main(String[] args) {
BeanFactory factory =
new XmlBeanFactory(new FileSystemResource("person.xml"));
// 값을 설정한 id 값 받아오기
PersonService person = (PersonService) factory.getBean("personService02");
// 실행
person.sayHello();
}
}
package com.spring.ex03;
// 생성자에 의한 명시
public interface PersonService {
// 1. 추상 메소드 명시
public void sayHello();
}
package com.spring.ex03;
public class PersonServiceimpl implements PersonService{
private String name;
private int age;
// 기본 생성자
public PersonServiceimpl() {}
// 생성자 만들기
public PersonServiceimpl(String name) {
this.name = name;
}
public PersonServiceimpl(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public void sayHello() {
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
}
}
<bean id="personService03" class="com.spring.ex03.PersonServiceimpl">
<!-- 생성자 구문. 태그 속에 value로 값 보냄 -->
<constructor-arg value="이순신" />
<!-- 위 아래 어느 모양이라도 상관 없다 -->
<constructor-arg value="23"></constructor-arg>
</bean>
package com.spring.ex03;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class PersonTest {
public static void main(String[] args) {
// xml에 작성한 값 가져오기(bean 자료 생성)
BeanFactory factory =
new XmlBeanFactory(new FileSystemResource("person.xml"));
PersonService person = (PersonService) factory.getBean("personService03");
person.sayHello();
}
}
만약 xml 에서<constructor-arg value="이순신" />
이것만 값을 보내도 나이 없이 이름만 잘 출력된다.
이런 식으로(근데 속성은 있고 값이 없으니 안 예쁘다)
3번 xml을 이런식으로 작성해주고
<bean id="personService03" class="com.spring.ex03.PersonServiceimpl">
<!-- 생성자 구문. 태그 속에 value로 값 보냄 -->
<constructor-arg value="이순신" />
<constructor-arg value="33"></constructor-arg>
</bean>
<!-- id 값은 고유 넘버. 겹치면 안된다 -->
<bean id="personService03.1" class="com.spring.ex03.PersonServiceimpl">
<constructor-arg value="이수영" />
</bean>
4번을 이런식으로 작성할 수도 있다.
package com.spring.ex03;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class PersonTest {
public static void main(String[] args) {
// xml에 작성한 값 가져오기(bean 자료 생성)
BeanFactory factory =
new XmlBeanFactory(new FileSystemResource("person.xml"));
PersonService person = (PersonService) factory.getBean("personService03");
person.sayHello();
System.out.println();
PersonService person1 = (PersonService) factory.getBean("personService03.1");
person1.sayHello();
}
}
기본적으로 Spring의 ApplicationContext 구현은 초기화 프로세스에서 모든 싱글톤 빈을 생성 및 설정하게 된다.
어떤 이유로 특정 bean이 늦게 초기화되기를 원한다면 lazy-init 을 사용하여 조정할 수 있다.
lazy-init 은 "true", "false", "default" 세 가지의 옵션을 가진다. default 는 spring 의 기본 동작에 맞게 bean 을 생성하며 기본 동작은 false 이다. true 로 설정할 경우 나중에 Bean 을 생성하게 된다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="firstBean" class="com.spring.ex05.First" lazy-init="false" />
<bean id="secondBean" class="com.spring.ex05.Second" lazy-init="true" />
<bean id="thirdBean" class="com.spring.ex05.Third" lazy-init="default" />
</beans>
"First.java"
package com.spring.ex05;
public class First {
public First() {
System.out.println("First 생성자 호출");
}
}
"Second.java"
package com.spring.ex05;
public class Second {
public Second() {
System.out.println("Second 생성자 호출");
}
}
"Third.java"
package com.spring.ex05;
public class Third {
public Third() {
System.out.println("Third 생성자 호출");
}
}
package com.spring.ex05;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class LazyTest {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("lazy.xml");
System.out.println("SecondBean 얻기");
context.getBean("secondBean");
}
}
분명 secondBean을 getBean 해주었음에도 lazy 설정에서true
즉 나중에 Bean을 생성하겠다 해주었기 때문에 출력결과가 위와 같이 나오는 모습을 확인 할 수 있었다.