Spring @Autowired Annotation
@Autowired on Setter Methods:
Example
TextEditor.java: setSpellChecker()에 @Autowired annotation이 사용되었다.
package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { private SpellChecker spellChecker; @Autowired public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public SpellChecker getSpellChecker( ) { return spellChecker; } public void spellCheck() { spellChecker.checkSpelling(); } }
SpellChecker.java:
package com.tutorialspoint; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling(){ System.out.println("Inside checkSpelling." ); } }
MainApp.java:
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); TextEditor te = (TextEditor) context.getBean("textEditor"); te.spellCheck(); } }
Beans.xml: textEdiotr bean 정의부분에서 spellChecker를 인자로 넘기기 위한 <property>가 제거되었다.
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean without constructor-arg --> <bean id="textEditor" class="com.tutorialspoint.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
실행 결과는 아래와 같다.
Inside SpellChecker constructor. Inside checkSpelling.
@Autowired on Properties:
setter함수를 제거하기 위해 속성(property>에 @Autowired annotation을 사용할 수 있다. <property>를 사용하여 auto wired 된 속성의 값을 전달할 때, Spring은 자동적으로 전달된 값(value) 또는 참조(reference)를 속성(property)에 할당한다.(assign)
[003] HelloWorld Example 강좌를 참조하여 프로젝트를 생성한다.
TextEditor.java: spellChecer 속성(property)에 @Autowired가 선언되었고, setSpellChecker()함수가 없음을 알 수 있다.
package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { @Autowired private SpellChecker spellChecker; public TextEditor() { System.out.println("Inside TextEditor constructor." ); } public SpellChecker getSpellChecker( ){ return spellChecker; } public void spellCheck(){ spellChecker.checkSpelling(); } }
SpellChecker.java:
package com.tutorialspoint; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling(){ System.out.println("Inside checkSpelling." ); } }
Beans.xml: 여기서도 또한 textEditor bean정의에서 spellChecker를 전달하기 위한 <property>가 없다.
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.tutorialspoint.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
실행 결과는 아래와 같다.
Inside TextEditor constructor. Inside SpellChecker constructor. Inside checkSpelling.
@Autowired on Constructors:
TextEditor.java: 생성자(TextEditor)에 @Autowired annotation이 사용되었다.
package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { private SpellChecker spellChecker; @Autowired public TextEditor(SpellChecker spellChecker){ System.out.println("Inside TextEditor constructor." ); this.spellChecker = spellChecker; } public void spellCheck(){ spellChecker.checkSpelling(); } }
Beans.xml: 이전 예제와는 다르게 이 예제의 경우 생성자를 이용한 DI의 구현이므로 일반적인 Beans.xml인 경우, textEditor bean의 정의에서 <constructor-arg>를 이용하여 spellChecker를 생성자의 인자로 전달하여야 하지만, textEditor의 생성자에 @Autowired를 사용함으로서 제거되었다.
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean without constructor-arg --> <bean id="textEditor" class="com.tutorialspoint.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
실행 결과는 아래와 같다.
Inside TextEditor constructor. Inside SpellChecker constructor. Inside checkSpelling.
@Autowired with (required=false) option
package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; public class Student { private Integer age; private String name; @Autowired(required=false) public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } @Autowired public void setName(String name) { this.name = name; } public String getName() { return name; } }