위로
아래
예시1
더보기
// PersonService 인터페이스
package com.spring.ex01;
public interface PersonService {
public void sayHello();
}
// PersonServiceImpl 구현 클래스
package com.spring.ex01;
public class PersonServiceImpl implements PersonService {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
@Override
public void sayHello() {
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
}
}
// PersonTest 메인 클래스
package com.spring.ex01;
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) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("person.xml"));
PersonService person = (PersonService) factory.getBean("personService");
// PersonService person = new PersonServiceImpl();
person.sayHello();
}
}
<!-- person.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="personService" class="com.spring.ex01.PersonServiceImpl">
<property name="name">
<value>홍길동</value>
</property>
</bean>
<!--
<bean id="personService1" class="com.spring.ex02.PersonServiceImpl">
<constructor-arg value="이순신"/>
</bean>
<bean id="personService2" class="com.spring.ex02.PersonServiceImpl">
<constructor-arg value="손흥민"/>
<constructor-arg value="23"/>
</bean>
-->
</beans>
예시2
더보기
//PersonService 인터페이스
package com.spring.ex02;
public interface PersonService {
public void sayHello();
}
//PersonServiceImple 구현 클래스
package com.spring.ex02;
public class PersonServiceImpl implements PersonService {
private String name;
private int age;
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);
}
}
//PersonTest2 메인 클래스
package com.spring.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"));
PersonService person1 = (PersonService) factory.getBean("personService1");
person1.sayHello();
System.out.println();
PersonService person2 = (PersonService) factory.getBean("personService2");
person2.sayHello();
}
}
<!-- person.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
<bean id="personService" class="com.spring.ex01.PersonServiceImpl">
<property name="name">
<value>홍길동</value>
</property>
</bean>
-->
<bean id="personService1" class="com.spring.ex02.PersonServiceImpl">
<constructor-arg value="이순신"/>
</bean>
<bean id="personService2" class="com.spring.ex02.PersonServiceImpl">
<constructor-arg value="손흥민"/>
<constructor-arg value="23"/>
</bean>
</beans>
예시3
더보기
// MemberDAO 인터페이스
package com.spring.ex03;
public interface MemberDAO {
public void listMembers();
}
// MemberDAOImpl 구현 클래스
package com.spring.ex03;
public class MemberDAOImpl implements MemberDAO {
@Override
public void listMembers() {
System.out.println("listMembers 메소드 호출");
System.out.println("회원정보를 조회합니다.");
}
}
// MemberService 인터페이스
package com.spring.ex03;
public interface MemberService {
public void listMembers();
}
//MemberServiceImple 구현 클래스
package com.spring.ex03;
public class MemberServiceImpl implements MemberService {
private MemberDAO memberDAO;
public void setMemberDAO(MemberDAO memberDAO) {
this.memberDAO = memberDAO;
}
@Override
public void listMembers() {
memberDAO.listMembers();
}
}
// MemberTest1 메인 클래스
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 MemberTest1 {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("member.xml"));
MemberService service = (MemberService) factory.getBean("memberService");
service.listMembers();
}
}
<!-- member.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="memberService" class="com.spring.ex03.MemberServiceImpl">
<property name="memberDAO" ref="memberDAO"/>
</bean>
<bean id="memberDAO" class="com.spring.ex03.MemberDAOImpl"/>
<!-- MemberDAOImpl memberDAO = new MemberDAOImpl() 이랑 같은 말 -->
</beans>