Annotation
11583 ワード
ネームスペースからコンテキストを追加
nameを与える場合はnameを使用します.
LgTV
SonySpeaker
sonyspeakerが動いています.
:appleSpeakerにcomponentを追加すると、autofiredはどのオブジェクトを注入するか分かりません.この場合は限定語を使うことができます.
Sony Speakerからコンポーネントを削除し、xmlファイルでbeanを使用します.
合併も使いやすいです.

<?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:p="http://www.springframework.org/schema/p"
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-4.3.xsd">
<!--
base-package에 설정된 패키지를 포함해서 하부를 모두 scan해서
@Component나 @Component로부터 파생된 어노테이션이 설정된 클래스를
모두 bean으로 생성해라
-->
<context:component-scan base-package="polymorphism"></context:component-scan>
</beans>
package polymorphism;
import org.springframework.stereotype.Component;
//id나 name 미지정시 자동으로 lgTV(앞글자 소문자)로 bean을 요청할 수 있다
@Component
public class LgTV implements TV {
public void powerOn() {
System.out.println("LgTV -- 전원 켠다");
}
public void powerOff() {
System.out.println("LgTV -- 전원 끈다");
}
public void volumeUp() {
System.out.println("LgTV -- 소리 올린다");
}
public void volumeDown() {
System.out.println("LgTV -- 소리 내린다");
}
}
TVUserpackage polymorphism;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class TVUser {
static BeanFactory factory = new BeanFactory();
public static void main(String[] args) {
AbstractApplicationContext factory =
new GenericXmlApplicationContext("applicationContext.xml");
TV tv = (TV)factory.getBean("lgTV");
tv.powerOn();
tv.powerOff();
tv.volumeUp();
tv.volumeDown();
factory.close();
}
}

nameを与える場合はnameを使用します.


自動注入(Autowired)
LgTV


SonySpeaker

sonyspeakerが動いています.

Qualifier
:appleSpeakerにcomponentを追加すると、autofiredはどのオブジェクトを注入するか分かりません.この場合は限定語を使うことができます.

Resource

annotaion+xml


Sony Speakerからコンポーネントを削除し、xmlファイルでbeanを使用します.

合併も使いやすいです.

Reference
この問題について(Annotation), 我々は、より多くの情報をここで見つけました https://velog.io/@jinkyung/Annotationテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol