-
component스프링 프레임워크 기초 공부 2020. 5. 26. 17:37
@component 어노테이션을 사용하면 Bean Configuration 파일에 Bean을 등록하지 않아도 자동으로 등록된다.
@Component public class TestBean3{ }
xml 이용방식
Bean에 설정된 설정들을 탐색하기 위해 다음과 같이 작성한다.
<context:component-scan base-package = "kr.co.softcampus.beans"/> <context:component-scan base-package = "kr.co.softcampus.beans"/>
java 방식
Bean에 설정된 설정들을 탐색하기 위해 다음과 같이 작성한다.
@ComponentScan(basePackages = {"kr.co.softcampus.beans", "kr.co.softcampus.bean2})
xml을 이용하여 bean객체 생성
TestBean1 클래스
package kr.co.softcampus.beans; public class TestBean1 { }
TestBean2 클래스
package kr.co.softcampus.beans; public class TestBean2 { }
bean.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" 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.xsd"> <bean class='kr.co.softcampus.beans.TestBean1'/> <bean id='xml2' class='kr.co.softcampus.beans.TestBean2'/> </beans>
main class
package kr.co.softcampus.main; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.beans.TestBean2; import kr.co.softcampus.beans2.TestBean3; import kr.co.softcampus.beans2.TestBean4; import kr.co.softcampus.beans3.TestBean5; import kr.co.softcampus.config.BeanConfigClass; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml"); System.out.println("-------------------------------"); TestBean1 xml1 = ctx1.getBean(TestBean1.class); System.out.printf("xml1 : %s\n", xml1); TestBean2 xml2 = ctx1.getBean("xml2", TestBean2.class); System.out.printf("xml2 : %s\n", xml2); } }
java로 bean 객체생성
TestBean1클래스와 TestBean2클래스는 동일
BeanConfigClass
package kr.co.softcampus.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.beans.TestBean2; import kr.co.softcampus.beans2.TestBean4; @Configuration public class BeanConfigClass { @Bean public TestBean1 java1() { return new TestBean1(); } @Bean public TestBean2 java2() { return new TestBean2(); } }
main class
package kr.co.softcampus.main; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.beans.TestBean2; import kr.co.softcampus.beans2.TestBean3; import kr.co.softcampus.beans2.TestBean4; import kr.co.softcampus.beans3.TestBean5; import kr.co.softcampus.config.BeanConfigClass; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub AnnotationConfigApplicationContext ctx2 = new AnnotationConfigApplicationContext(BeanConfigClass.class); TestBean1 java1 = ctx2.getBean(TestBean1.class); System.out.printf("java1 : %s\n", java1); TestBean2 java2 = ctx2.getBean("java2", TestBean2.class); System.out.printf("java2 : %s\n", java2); ctx2.close(); } }
component 사용
클래스들은 kr.co.softcampus.bean2 패키지 안에 들어가잇다.
TestBean3 클래스
package kr.co.softcampus.beans2; import org.springframework.stereotype.Component; // Bean으로 등록한다. // 이름이 없기 때문에 타입을 통해서 받아 낼수 있다. @Component public class TestBean3 { }
TestBean4 클래스
package kr.co.softcampus.beans2; import org.springframework.stereotype.Component; @Component("bean4") public class TestBean4 { }
java 방식
BeanConfigClass
package kr.co.softcampus.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.beans.TestBean2; import kr.co.softcampus.beans2.TestBean4; @Configuration // 지정된 패키지의 Bean 클래스들의 어노테이션을 분석하여 Bean을 등록하라고 지정한다. @ComponentScan(basePackages = "kr.co.softcampus.beans2") public class BeanConfigClass { }
xml 방식
bean.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" 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.xsd"> <!-- 지정된 패키지 안에 있는 Bean 클래스들의 어노테이션을 분석하도록 지정한다 --> <context:component-scan base-package="kr.co.softcampus.beans2"/> </beans>
main class
java 방식
package kr.co.softcampus.main; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.beans.TestBean2; import kr.co.softcampus.beans2.TestBean3; import kr.co.softcampus.beans2.TestBean4; import kr.co.softcampus.beans3.TestBean5; import kr.co.softcampus.config.BeanConfigClass; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub AnnotationConfigApplicationContext ctx2 = new AnnotationConfigApplicationContext(BeanConfigClass.class); TestBean3 java4 = ctx2.getBean(TestBean3.class); System.out.printf("java4 : %s\n", java4); TestBean4 java5 = ctx2.getBean("bean4", TestBean4.class); System.out.printf("java5 : %s\n", java5); ctx2.close(); } }
xml 방식
package kr.co.softcampus.main; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.beans.TestBean2; import kr.co.softcampus.beans2.TestBean3; import kr.co.softcampus.beans2.TestBean4; import kr.co.softcampus.beans3.TestBean5; import kr.co.softcampus.config.BeanConfigClass; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml"); TestBean3 xml4 = ctx1.getBean(TestBean3.class); System.out.printf("xml4 : %s\n", xml4); TestBean4 xml5 = ctx1.getBean("bean4", TestBean4.class); System.out.printf("xml5 : %s\n", xml5); ctx1.close(); } }
끝으로 Bean과 Component 활용 차이
Bean
개발자가 class의 코드를 수정할 수 없는 경우
같은 클래스 타입의 Bean을 여러 개 등록할 경우
Component
개발자가 class의 코드를 수정할 수 있는 경우
'스프링 프레임워크 기초 공부' 카테고리의 다른 글
spring mariadb 연동방법 (xml방식) (0) 2020.08.07 BeanPostProcessor (0) 2020.05.23 spring jdbc (0) 2020.05.16 xml namespace (0) 2020.05.01 12장 AOP (0) 2020.04.30