1 / 13

@ Autowired

@ 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"

joelle
Download Presentation

@ Autowired

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. @Autowired ㅇXML을 이용한 명시적 주입 방법 대신 애노테이션을 이용한 주입 방법 ㅇ 자동 와이어링애노테이션

  2. @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>

  3. @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()); } }

  4. @Autowired First.java public class First { @Autowired private Second second; public Second getSecond() { return this.second; } }

  5. @Autowired Second.java public class Second { private int age = 0; public Second() {} public void setAge(int age) { this.age = age; } public intgetAge() { return age; } }

  6. Autowired 실습 ㅇ 앞에서 작성한 SpringExam프로젝트에 패키지 추가 ㅇ 패키지 : exam.spring.context ㅇ<context:annotation-config /> 및 Autowired annotation 변환 - bean 패키지 복사 - AndroidPhone @Autowired적용, set 메소드/ 생성자 제거 - config.xml 수정

  7. 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>

  8. component-scan

  9. 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>

  10. 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()); } }

  11. component-scan First.java public class First { @Autowired private Second second; public Second getSecond() { return this.second; } }

  12. 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; } }

  13. component-scan 실습 ㅇ 패키지 : exam.spring.context ㅇ<context:component-scan /> 및 Autowired annotation 변환 - config.xml 수정

More Related