2.Spring学習の道(自動組立、注釈による開発)
16200 ワード
一.Beanの自動アセンブリ
1.1 3種類の組み立て機構(1).xmlには構成(2)が表示する.自動アセンブリ(3)javaでの表示構成
1.2自動組立の二つの操作部品スキャン、自動組立DEMO
1.3プロファイルベースの自動アセンブリByName(名前による自動アセンブリ)
catのbean idをcat 111に変更すると、ポインタが空になり、次のようにまとめられます.クラス内のすべてのsetメソッド名、例えばsetCatが検索され、setを削除して頭文字を小文字にする文字列、すなわちcatが取得されます.2.springコンテナにこの文字列名idのオブジェクトがあるかどうかを探します.3.もしあれば、注入を取り出します.ない場合は、空のポインタ異常を報告します.
byType(タイプ別自動組立)のまとめは以下の通りである:1.userのbean構成を変更する:autowire="byType"2.スプリング容器のタイプが一意でない場合、一意でない異常1.4が報告される.注記ベースの自動アセンブリ1.準備作業、プロファイル導入contextファイルヘッダ
2.注釈サポートを開く
3.コードテスト
二.注釈ベースの開発
2.1 context制約を導入するには
2.2 Beanの実装1.スキャンされたパッケージの構成
2.指定されたパッケージの下に注記と属性注入を追加
注記ベースioc構成とxml構成は、プログラム間の結合を低減する機能が同じであり、構成の形式が異なるだけです.@Componet注記を使用して、管理リソース(すなわちbeanを生成する)アカウントのビジネス層実装クラスを構成します.
アカウントの永続層実装クラス
注記統合に基づいて、コンストレイントをインポートする場合は、springがコンテナを作成するときにスキャンするパケット
注入データは,@Aurowired作用に相当する:自動的にタイプに従って注入される.注記注入プロパティを使用する場合、setメソッドは省略できます.他のbeanタイプにのみ注入できます.複数のタイプが一致している場合、beanのidとして注入するオブジェクト変数名を使用してspringコンテナで検索し、見つけても注入に成功します.見つからないと間違いを報告する.@Qualifierの役割:タイプごとに自動的に注入した上で、Beanのidで注入します.フィールドに入力するときは独立して使用できません.@Autowiredと一緒に使用する必要があります.ただし,メソッドパラメータを注入する場合は,独立して使用できる.属性:value:beanのidを指定します.@Resource役割:Beanのidに直接注入します.他のbeanタイプにのみ注入できます.属性:name:beanのidを指定します.@Valueの役割:基本データ型とString型のデータを入力するプロパティ:value:値を指定して範囲を変更する
@Scopeの役割:beanの役割範囲を指定します.プロパティ:value:範囲の値を指定します.値:singleton prototype request session globalsession
ライフサイクル関連
@PostConstructの役割:初期化方法を指定します.@PreDestroyの役割:破棄方法を指定します.
リファレンス注記ベースの開発
1.1 3種類の組み立て機構(1).xmlには構成(2)が表示する.自動アセンブリ(3)javaでの表示構成
1.2自動組立の二つの操作部品スキャン、自動組立DEMO
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
public class User {
private Cat cat;
private Dog dog;
private String str;
}
1.3プロファイルベースの自動アセンブリByName(名前による自動アセンブリ)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User" autowire="byName">
<property name="str" value="qinjiang"/>
</bean>
</beans>
catのbean idをcat 111に変更すると、ポインタが空になり、次のようにまとめられます.クラス内のすべてのsetメソッド名、例えばsetCatが検索され、setを削除して頭文字を小文字にする文字列、すなわちcatが取得されます.2.springコンテナにこの文字列名idのオブジェクトがあるかどうかを探します.3.もしあれば、注入を取り出します.ない場合は、空のポインタ異常を報告します.
byType(タイプ別自動組立)のまとめは以下の通りである:1.userのbean構成を変更する:autowire="byType"2.スプリング容器のタイプが一意でない場合、一意でない異常1.4が報告される.注記ベースの自動アセンブリ1.準備作業、プロファイル導入contextファイルヘッダ
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
2.注釈サポートを開く
<context:annotation-config/>
3.コードテスト
public class User {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String str;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getStr() {
return str;
}
}
<context:annotation-config/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>
二.注釈ベースの開発
2.1 context制約を導入するには
<?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">
</beans>
2.2 Beanの実装1.スキャンされたパッケージの構成
<!-- -->
<context:component-scan base-package="com.kuang.pojo"/>
2.指定されたパッケージの下に注記と属性注入を追加
@Component("user")
//
public class User {
@Value(" ")
//
public String name;
}
注記ベースioc構成とxml構成は、プログラム間の結合を低減する機能が同じであり、構成の形式が異なるだけです.@Componet注記を使用して、管理リソース(すなわちbeanを生成する)アカウントのビジネス層実装クラスを構成します.
@Component("accountService")
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
}
アカウントの永続層実装クラス
@Component("accountDao")
public class AccountDaoImpl implements IAccountDao {
private DBAssit dbAssit; }
注記統合に基づいて、コンストレイントをインポートする場合は、springがコンテナを作成するときにスキャンするパケット
>
@Component :value: bean id, value , bean id , , @component @Controller,@Service,@Repository, , ,
に、context名空間のコンストレイントを複数インポートする必要があります.注入データは,@Aurowired作用に相当する:自動的にタイプに従って注入される.注記注入プロパティを使用する場合、setメソッドは省略できます.他のbeanタイプにのみ注入できます.複数のタイプが一致している場合、beanのidとして注入するオブジェクト変数名を使用してspringコンテナで検索し、見つけても注入に成功します.見つからないと間違いを報告する.@Qualifierの役割:タイプごとに自動的に注入した上で、Beanのidで注入します.フィールドに入力するときは独立して使用できません.@Autowiredと一緒に使用する必要があります.ただし,メソッドパラメータを注入する場合は,独立して使用できる.属性:value:beanのidを指定します.@Resource役割:Beanのidに直接注入します.他のbeanタイプにのみ注入できます.属性:name:beanのidを指定します.@Valueの役割:基本データ型とString型のデータを入力するプロパティ:value:値を指定して範囲を変更する
:
@Scopeの役割:beanの役割範囲を指定します.プロパティ:value:範囲の値を指定します.値:singleton prototype request session globalsession
ライフサイクル関連
:
@PostConstructの役割:初期化方法を指定します.@PreDestroyの役割:破棄方法を指定します.
リファレンス注記ベースの開発