Springの中でBeanの自動組立の6つのモードは、いくつか知っていますか?
Spring の中でBeanの自動組立の6つのモードは、いくつか知っていますか?
Spring IoC容器は自動的に組み立てることができます.したがって、できればSpringを自動的にBenFactoryの内容をチェックすることで、beanの協力者(他の依存されているbean)を指定してくれます.atowireは全部で六種類あります.atowireは単独のbeanに対して設定できるので、atowireを使うbeanがあります.autwireの便利さは、属性やコンストラクタのパラメータの設定を減らしたり消したりすることで、私たちのコンフィギュレーションファイルを減量してくれます.[2] xmlプロファイルでは、autwire属性指定は、<bean/>要素で使用できます.
モード
説明
Default
各beanでatowire=defaultのデフォルト設定は、beansとラベル中のdefault-autwire=「属性値」と同じ設定を採用するという意味です.
On
自動組立を使用しない場合は、ref要素による依存性を指定し、デフォルトで設定する必要があります.
ByNname
属性名に合わせて自動組立します.このオプションを有効にすると、コンテナをチェックして、名前に基づいて属性と完全に一致するビーンを検索して、属性を自動的に組み立てることができます.例えば、bean定義ではatowireをbyに設定します. nameでは、このbeanはマスター属性(setMaster(..)メソッドを同時に提供する)を含んでいます.Springはmasterというbean定義を探して、master属性に組み立てられます.
Bytype
指定された属性タイプと同じビーンが容器に存在する場合、この属性は自動的に組み立てられます.このタイプのbeanが複数存在すると異常を投げ、byType方式で自動組立ができないと指摘しています.マッチするビーンが見つからないと、何事も発生せず、属性も設定されません.このように望んでいないなら、dependency-check=「Objects」を設定することでSpringに異常を投げかけることができます.
コンストラクタ
byTypeの方式と似ていますが、それはコンストラクタパラメータに適用される点が違います.容器にコンストラクタのパラメータタイプと一致するビーンが見つからない場合、異常を投げます.
Antodetect
バーン類の自省機構によって、constructorまたはbyType方式で自動組立を決定します.デフォルトのコンストラクタが見つかったら、byType方式を使います.
私たちはケースで証明します.3つの種類を用意します.
1,defaultとno値default.xmlプロファイル
テストクラス:(junnitテスト)
宣言:
scope=「singleton」 atowire=「no」 />
自動組立を使用しない場合は、ref要素による依存性を指定し、デフォルトで設定する必要があります.
2,byName値のbyname.xmlプロファイル
テストクラス:(junnitテスト)
3,byType値bytype.xmlプロファイル
注意異常:
テストクラス:(junnitテスト)
注意:
byName とby Type
使用中は必ずbeanが初期化できるようにしてください.でないと、bugが発生します.
デフォルトのパラメータなしのコンストラクタがあれば、余分な配置は必要ありません.
パラメータを持つコンストラクタがあるなら、beanの構成でコンフィギュレーションを初期化しなければならないパラメータです. またはbeanにパラメータなしのコンストラクタを追加します.
4, Costructor値のconstructor.xml配置ファイル
テストクラス:(junnitテスト)
5,Antodetect値のantodetect.xmlプロファイル
終了語:
ほとんどの勉強はビーンの自動組立は5つのモードがあると思いますが、詳しくは6つのモードです.彼らはdefaultを無視しがちです. この値の適用は、各beanにおいて、autwire=defaultのデフォルト設定を採用するという意味です.beansとラベル中のdefault-autwire=「属性値」と同じ設定を採用すると、この値は無視できません.
*beanは自動組立を無効にすることができます.xml形式でbeanを配置する場合、「bean/」要素のautwire-candidate属性をfalseに設定します.このような容器は自動組立オブジェクトを検索する時、beanを考慮しないで、つまり他のbean自動組立の候補者として考慮されません.
Spring IoC容器は自動的に組み立てることができます.したがって、できればSpringを自動的にBenFactoryの内容をチェックすることで、beanの協力者(他の依存されているbean)を指定してくれます.atowireは全部で六種類あります.atowireは単独のbeanに対して設定できるので、atowireを使うbeanがあります.autwireの便利さは、属性やコンストラクタのパラメータの設定を減らしたり消したりすることで、私たちのコンフィギュレーションファイルを減量してくれます.[2] xmlプロファイルでは、autwire属性指定は、<bean/>要素で使用できます.
モード
説明
Default
各beanでatowire=defaultのデフォルト設定は、beansとラベル中のdefault-autwire=「属性値」と同じ設定を採用するという意味です.
On
自動組立を使用しない場合は、ref要素による依存性を指定し、デフォルトで設定する必要があります.
ByNname
属性名に合わせて自動組立します.このオプションを有効にすると、コンテナをチェックして、名前に基づいて属性と完全に一致するビーンを検索して、属性を自動的に組み立てることができます.例えば、bean定義ではatowireをbyに設定します. nameでは、このbeanはマスター属性(setMaster(..)メソッドを同時に提供する)を含んでいます.Springはmasterというbean定義を探して、master属性に組み立てられます.
Bytype
指定された属性タイプと同じビーンが容器に存在する場合、この属性は自動的に組み立てられます.このタイプのbeanが複数存在すると異常を投げ、byType方式で自動組立ができないと指摘しています.マッチするビーンが見つからないと、何事も発生せず、属性も設定されません.このように望んでいないなら、dependency-check=「Objects」を設定することでSpringに異常を投げかけることができます.
コンストラクタ
byTypeの方式と似ていますが、それはコンストラクタパラメータに適用される点が違います.容器にコンストラクタのパラメータタイプと一致するビーンが見つからない場合、異常を投げます.
Antodetect
バーン類の自省機構によって、constructorまたはbyType方式で自動組立を決定します.デフォルトのコンストラクタが見つかったら、byType方式を使います.
私たちはケースで証明します.3つの種類を用意します.
public class AddressServiceImpl {
/** */
private String address;
public void setAddress(String address){
this.address=address;
}
}
public class HomeAddressServiceImpl extends AddressServiceImpl {
private String address;
public void setAddress(String address){
this.address=address;
}
public HomeAddressServiceImpl() {
super();
}
public HomeAddressServiceImpl(String address){
this.address=address;
}
}
public class EmpServiceImpl {
/** */
private AddressServiceImpl companyAddress;
public void setCompanyAddress(AddressServiceImpl companyAddress){
this.companyAddress=companyAddress;
}
}
1,defaultとno値default.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="no">
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"
scope="singleton">
<property name="address">
<value> </value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="default" />
</beans>
テストクラス:(junnitテスト)
public class App {
@Test
public void test(){
ApplicationContext ac= new ClassPathXmlApplicationContext("classpath:default.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");
}
}
宣言:
自動組立を使用しない場合は、ref要素による依存性を指定し、デフォルトで設定する必要があります.
2,byName値のbyname.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"
scope="singleton">
<property name="address">
<value> </value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="byName" />
</beans>
テストクラス:(junnitテスト)
public class App {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");
}
}
3,byType値bytype.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"
scope="singleton">
<property name="address">
<value> </value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="byType" />
</beans>
注意異常:
<beanid="addressServiceImpl"class="cn.csdn.service.AddressServiceImpl" scope="singleton"/>
//homeAddressServiceImpl addressServiceImpl, !
<!-- bean , bug :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empServiceImpl' defined in file [D:\Workspaces\MyEclipse 8.6\20110419_01\bin\applicationContext.xml]: Unsatisfied dependency expressed through bean property 'companyAddress': : No unique bean of type [cn.csdn.service.AddressServiceImpl] is defined: expected single matching bean but found 2: [homeAddressServiceImpl, addressServiceImpl]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [cn.csdn.service.AddressServiceImpl] is defined: expected single matching bean but found 2: [homeAddressServiceImpl, addressServiceImpl]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)..........................
-->
テストクラス:(junnitテスト)
public class App {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");
}
}
注意:
byName とby Type
使用中は必ずbeanが初期化できるようにしてください.でないと、bugが発生します.
デフォルトのパラメータなしのコンストラクタがあれば、余分な配置は必要ありません.
パラメータを持つコンストラクタがあるなら、beanの構成でコンフィギュレーションを初期化しなければならないパラメータです. またはbeanにパラメータなしのコンストラクタを追加します.
4, Costructor値のconstructor.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- bean
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl" scope="prototype">
<property name="address">
<value> </value>
</property>
</bean>
<!-- constructor byType
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="constructor"/>
</beans>
テストクラス:(junnitテスト)
public class App {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");
}
}
5,Antodetect値のantodetect.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- bean -->
<bean id="addressServiceImpl" class="cn.csdn.service.AddressServiceImpl" scope="singleton">
<property name="address">
<value> </value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="autodetect"/>
</beans>
テストクラス:(junnitテスト) public class App {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");
}
}
終了語:
ほとんどの勉強はビーンの自動組立は5つのモードがあると思いますが、詳しくは6つのモードです.彼らはdefaultを無視しがちです. この値の適用は、各beanにおいて、autwire=defaultのデフォルト設定を採用するという意味です.beansとラベル中のdefault-autwire=「属性値」と同じ設定を採用すると、この値は無視できません.
*beanは自動組立を無効にすることができます.xml形式でbeanを配置する場合、「bean/」要素のautwire-candidate属性をfalseに設定します.このような容器は自動組立オブジェクトを検索する時、beanを考慮しないで、つまり他のbean自動組立の候補者として考慮されません.