Spring IOCの紹介と使用

IOCとBeanの概要

IOCはDIとも呼ばれます。コンストラクターパラメーター、仮パラメーター、およびプロパティを使用して、オブジェクトインスタンスを設定します。このプロセスでBeanを作成すると、コンテナはこれらの依存関係を挿入します。Bean自体は、クラスの直接構築を使用して、依存関係のインスタンス化または場所を制御します。Beanの作成方法が逆なので、次のように呼び出されます制御の反転(IoC)。端的に言えば、オブジェクトは過去にnewを通じて作成されましたが、現在は新しいものではなく、オブジェクトはクラスの構築を通じて直接注入されます。

org.springframework.beansそして、org.springframework.contextIOCコンテナの中核です。BeanFactoryインターフェースは、オブジェクトを高レベルで構成する機能を提供します。ApplicationContextはBeanFactoryのサブインターフェースです。その追加機能には、AOP機能、メッセージとリソースの処理、イベントの伝播、およびその他の機能が含まれます。これは、父親が貧しい農家で息子がそうであるように、BeanFactoryを完全に置き換えることができます。 Gao Fushuai; WebApplicationContextなどの特定のコンテナーは、豊富なWebアプリケーション機能を提供します。

春には、オブジェクトはアプリケーションのスケルトンであり、IOCコンテナーによって管理されます。IOCコンテナーはBeanとも呼ばれ、Beanのインスタンス化、アセンブリ、およびそのライフサイクルはIOCコンテナーによって管理されます。Beanとそれらの相互依存関係は、構成メタデータで構成され、IOCコンテナーによって管理および使用されます。

2つのコンテナーの概要

org.springframework.context.ApplicationContext実際、これは、Beanのインスタンス化、アセンブリ、および構成を担当するIOCコンテナーを表しています。IOCコンテナーはどのようにBeanを構成および管理しますか?構成メタデータを使用してBeanを管理するための指示を取得します。では、構成メタデータとは何ですか?構成メタデータは、実際にはXML、Javaアノテーション、およびJavaコードです。

Springは、少量のApplicationContext構成を通じてそのまま使用できます。通常、単一のアプリケーションがClassPathXmlApplicationContextまたはFileSystemXmlApplicationContextのインスタンスを作成します。XMLは従来の構成メタデータ形式ですが、少量のXML表示宣言を使用してJavaアノテーションまたはJavaコードメタデータ形式をサポートすることもできます。多くのアプリケーションシナリオでは、1つではなく多くのIOCコンテナーが作成されます。

オブジェクトと構成メタデータが完了すると、ApplicationContextが初期化および作成され、次に示すように、システムまたはアプリケーションを完全に実行できます。

ここに画像の説明を挿入

3つの初期構成メタデータ

ばね構成はビーンのうちの1つまたは複数の最小値を必要とする、ベースのXML構成は、<bean>のトップレベル要素が必要<beans>内部を、対応する構成は、Java @Bean(上記方法)に基づいて、アノテーション(クラスについて上)@Configurationを注釈されています。<bean>一意の識別子ID豆を表し、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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>
复制代码

4つのインスタンス化されたコンテナ

文字列形式の1つ以上のリソースパスをApplicationContextに提供します。ApplicationContextは、このリソースパスを介してこれらの外部構成メタデータをロードします。

ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
复制代码

<property>属性名素子の特性のJavaBean、REF点は他の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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- services -->

    <bean id="iocService" class="com.youku1327.ioc.service.IocService">
        <property name="iocDao" ref="iocDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>
复制代码

5つのアセンブリxml

実際の開発では、ビジネスレイヤーとロジックレイヤーが分離しています。つまり、XML構成Beanの結合度が高すぎます。複数のMXLデカップリングを定義する必要がありますが、1つのXMLで別のXMLのBeanを参照する方法何ですか?<import/>要素を通じて他のxmlからBeanをロードできます。外部xmlを導入する場合、次の例のように、これは現在のxmlの相対パスです。services.xmlは現在のxmlと同じディレクトリにあり、message.xmlは現在のxmlディレクトリのサブディレクトリにあります。

<beans>
    <import resource="services.xml"/>
    <import resource="resources/message.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>
复制代码

シックスユースコンテナ

ApplicationContextは、さまざまなBeanと依存関係のレジストリを維持する高レベルのファクトリです。このインターフェースのT getBean(String name、Class requiredType)メソッドを使用して、Beanインスタンスを取得します。

6.1 pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>

    </dependencies>
复制代码

6.2 dao

/**
 * @Author lsc
 * @Description <p>ioc dao </p>
 * @Date 2019/10/29 20:04
 */
public class IocDao {
}
复制代码

6.3サービス


/**
 * @Author lsc
 * @Description <p> </p>
 * @Date 2019/10/29 20:03
 */
public class IocService {

    private IocDao iocDao;

    private String name;


    public IocDao getIocDao() {
        return iocDao;
    }

    public void setIocDao(IocDao iocDao) {
        this.iocDao = iocDao;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
复制代码

6.4 dao.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.xsd">

    <!-- services -->

    <bean id="iocDao" class="com.youku1327.ioc.dao.IocDao">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>
复制代码

6.5 services.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.xsd">

    <import resource="dao.xml"/>

    <!-- services -->

    <bean id="iocService" class="com.youku1327.ioc.service.IocService">
        <property name="iocDao" ref="iocDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>
复制代码

6.6アプリケーション

/**
 * @Author lsc
 * @Description <p> 初始ioc</p>
 * @Date 2019/10/29 22:22
 */
public class Application {

    public static void main(String[] args) {
        // 创建和配置 beans
        ApplicationContext context =
                new ClassPathXmlApplicationContext(new String[] {"services.xml", "dao.xml"});
        // 获得配置的实例
        IocService service = context.getBean("iocService", IocService.class);
        // 使用配置的实例
        service.setName("youku1327");
        System.out.println(service.getName());
    }
}
复制代码

6.7出力

ここに画像の説明を挿入

7つの参考資料と公会計

ソースコードは、パブリックアカウントに対応する記事の最後にあります。

春のリファレンス

ここに画像の説明を挿入

おすすめ

転載: juejin.im/post/5e970168f265da47d2025aba