130 likes | 333 Views
@ Autowired. ㅇ XML 을 이용한 명시적 주입 방법 대신 애노테이션을 이용한 주입 방법. ㅇ 자동 와이어링 애노테이션. @ Autowired. config.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"
E N D
@Autowired ㅇXML을 이용한 명시적 주입 방법 대신 애노테이션을 이용한 주입 방법 ㅇ 자동 와이어링애노테이션
@Autowired config.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"> <context:annotation-config/> <bean id="first" class="edu.seowon.context.First" /> <bean id="second" class="edu.seowon.context.Second"> <property name="age"> <value>18</value> </property> </bean> </beans>
@Autowired MainContext.java public class MainContext { public static void main(String[] args) { ClassPathXmlApplicationContextctx = new ClassPathXmlApplicationContext( "edu/seowon/context/config.xml"); Object obj = ctx.getBean("first"); System.out.println(((First)obj).getSecond().getAge()); } }
@Autowired First.java public class First { @Autowired private Second second; public Second getSecond() { return this.second; } }
@Autowired Second.java public class Second { private int age = 0; public Second() {} public void setAge(int age) { this.age = age; } public intgetAge() { return age; } }
Autowired 실습 ㅇ 앞에서 작성한 SpringExam프로젝트에 패키지 추가 ㅇ 패키지 : exam.spring.context ㅇ<context:annotation-config /> 및 Autowired annotation 변환 - bean 패키지 복사 - AndroidPhone @Autowired적용, set 메소드/ 생성자 제거 - config.xml 수정
component-scan ㅇ 기본적으로 @Component, @Repository, @Service, @Controller 스캔 ㅇinclude-filter 와 exclude-filter 를 사용하여 포함할 빈과 포함하지 않을 빈 설정 ㅇuse-default-filters=“false” 속성을 사용시 기본 애노테이션스캔하지 않음 ㅇ 사용 예) <context:component-scan base-package="org.example"> <context:include-filter type="regex" expression=".*Stub.*Repository"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan>
component-scan config.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"> <context:component-scan base-package="edu.seowon.context"> <context:include-filter type="regex“ expression="edu.seowon.context.*" /> </context:component-scan> </beans>
component-scan MainContext.java public class MainContext { public static void main(String[] args) { ClassPathXmlApplicationContextctx = new ClassPathXmlApplicationContext( "edu/seowon/context/config.xml"); First first = (First) ctx.getBean("first"); Second second = first.getSecond(); second.setAge(18); System.out.println(second.getAge()); } }
component-scan First.java public class First { @Autowired private Second second; public Second getSecond() { return this.second; } }
component-scan Second.java public class Second { private int age = 0; public Second() {} public void setAge(int age) { this.age = age; } public intgetAge() { return age; } }
component-scan 실습 ㅇ 패키지 : exam.spring.context ㅇ<context:component-scan /> 및 Autowired annotation 변환 - config.xml 수정