背景
前には、実際には、豆のXMLの注釈の設定とアセンブリまったく同じ原理を豆xml構成を組み立てる方法について話しましたが、また、効果を達成するために同じであるが、それは別のアプローチを使用しています。
この記事だから、我々はまた、インスタンスを置き換えるものではありません、または歌手を注入する段階で、ダンサーは、例えば、組み立てられています。注釈の場合ので、クラス、プロパティ注釈に直接添加Beanクラス名+パスのパッケージを指定する必要が表示されない、アセンブリは、自動です。
名前でアセンブリの自動化
私たちは、豆、豆に名前を付け、組み立て時に、アセンブリの名前を指定します。
1、歌手のクラス定義、および対応する注釈@ComponentにBeanを生成します。
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("liujia") // 通过注解指定Dancer类生成的bean名称为liujia
public class Dancer {
@Value("刘迦") // 通过注解注入姓名
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("daolang") // 通过注解指定Singer类生成的bean名称为daolang
public class Singer {
@Value("刀郎") // 通过注解注入姓名
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ここで再び説明@Component("xxx")
ラベルのアノテーションクラスをスキャンし、豆xxxの容器に指定されたオブジェクトを生成しますが、起動時に注釈、Springコンテナの役割を。あなたは注釈を使用する場合@Component
ラベルを、そして今度は、最初の文字を小文字クラスの名前のBeanを生成します。
図2に示すように、ステージのクラス定義、およびアセンブリそのプロパティの名前
@Autowired
使用中のルールを指定することによって、注射を作り、豆を来る@Qualifier("xxx")
、次のコードをアセンブリの名前XXX Beanを:
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
//舞台类,注意舞台也要生成一个bean
@Component("stage")
public class Stage {
@Autowired//表示自动注入
@Qualifier("daolang")//指定要装配名称为daolang的bean
private Singer singer;
@Autowired
@Qualifier("liujia")//指定要装配名称为liujia的bean
private Dancer dancer;
public Singer getSinger() {
return singer;
}
public void setSinger(Singer singer) {
this.singer = singer;
}
public Dancer getDancer() {
return dancer;
}
public void setDancer(Dancer dancer) {
this.dancer = dancer;
}
}
3、構成XMLファイルには、Beanを開くためにスキャン
注釈コンフィグレーションBeanを使用することを忘れ、またはspring.xmlファイルは次のようにして、あなたはどこ豆袋スキャンを開きたいという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-4.0.xsd">
<context:component-scan
base-package="org.maoge.annotaionassemble" />
</beans>
4、試験
メインクラスを起動し、コンテナからアリーナを取得し、あなたは成功しているアセンブリのステージシンガーやダンサーを見つけます。
package org.maoge.annotationassemble;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"/org/maoge/annotationassemble/spring.xml");
Stage stage = (Stage) context.getBean("stage");
System.out.println(stage.getSinger().getName());
System.out.println(stage.getDancer().getName());
}
}
出力:
刀郎
刘迦
式自動組立により、
より単純なああは、注釈を使用するので、このクラスは、典型的には、(Beanを生成するために、対応するクラス上の音符に適用される)ビーンを生成します。
だから、試合の種類に応じて、基本的に非常に安定してああ、何の問題だけでなく、非常にプロセスです。SSMプロジェクトやSpringBootプロジェクトは、我々は多くをこのように使用しているかどうかを現在、この方法は、非常に、非常に人気のあるべき。
例を見てください:
1、クラスの定義とダンサー歌手のクラス
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component // 未指定命名,所以默认命名应为singer
public class Singer {
@Value("刀郎") // 通过注解注入姓名
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component // 未指定命名,所以默认命名应为dancer
public class Dancer {
@Value("刘迦") // 通过注解注入姓名
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、豆型自動組立段階
増加しないでください@Qualifier
、あまりにも一般的なので、デフォルトでは自動的に、アセンブリの種類に応じています。
package org.maoge.annotationassemble;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Stage {
@Autowired // 按类型自动装配
private Singer singer;
@Autowired // 按类型自动装配
private Dancer dancer;
public Singer getSinger() {
return singer;
}
public void setSinger(Singer singer) {
this.singer = singer;
}
public Dancer getDancer() {
return dancer;
}
public void setDancer(Dancer dancer) {
this.dancer = dancer;
}
}
3、テストの実行
それでも同じXMLとMain.javaは、直接メインのテスト、まだ何の問題を実行します。
概要
注釈を使用する場合は、高度に自動化組立と非常に鋭いです。
この事を組み立て、複雑ではありません、それは他のBeanでBeanプロパティを注入することです。
自動アセンブリ、コンテナによって組み立てられる実装される豆の種類または名前を指定し、ルール(名前/タイプ)自動組立を指定し、自動的に適切なマメビーンプロパティに注入することです。