Autowired
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

@ Autowired PowerPoint PPT Presentation


  • 129 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Autowired

@Autowired

ㅇXML을 이용한 명시적 주입 방법 대신 애노테이션을 이용한 주입 방법

ㅇ 자동 와이어링애노테이션


Autowired

@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

@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

@Autowired

First.java

public class First {

@Autowired

private Second second;

public Second getSecond() {

return this.second;

}

}


Autowired

@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

Autowired 실습

ㅇ 앞에서 작성한 SpringExam프로젝트에 패키지 추가

ㅇ 패키지 : exam.spring.context

ㅇ<context:annotation-config /> 및 Autowired annotation 변환

- bean 패키지 복사

- AndroidPhone @Autowired적용, set 메소드/ 생성자 제거

- config.xml 수정


Autowired

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>


Autowired

component-scan


Autowired

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>


Autowired

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());

}

}


Autowired

component-scan

First.java

public class First {

@Autowired

private Second second;

public Second getSecond() {

return this.second;

}

}


Autowired

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;

}

}


Autowired

component-scan 실습

ㅇ 패키지 : exam.spring.context

ㅇ<context:component-scan /> 및 Autowired annotation 변환

- config.xml 수정


  • Login