春的IoC(制御の反転)2

最初の部分:SpringのIoC(制御の反転)
は、下の比喩的なイメージIoC、つまりデカップリングです。
ここに画像の説明を挿入します
IoCこれはSpringフレームワークのコアコンテンツであり、さまざまな方法で実装されますIoCXML構成または注釈を使用できます。Spring反転制御実装依存注射です(Dependency Injection, DI)。
さあ、ぜひ体験してみてください。

1.XML実現するIoC

パス:
ここに画像の説明を挿入します
Hello.javaコンテンツ:

package com.weizu;

public class Hello {
    
    
    private String name;

    public Hello() {
    
    
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "Hello{" +
                "name='" + name + '\'' +
                '}';
    }
}

次に、beans.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" class="com.weizu.Hello">
        <property name="name" value="Xiao wang."/>
    </bean>

</beans>

最後に、テストを使用します。

import com.weizu.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myTest {
    
    

    @Test
    public void Test(){
    
    
        // create and configure beans
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.getName());
    }
}

テスト結果:
ここに画像の説明を挿入します
対応する公式ドキュメント:https//docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-instantiation
上記では、オブジェクトのXML読み込みに使用する方法を学習しました。自動ホスティングのためにBean、私たちは改善の最後の記事になることができますSpring
つまり、beans.xmlファイル内のDaoレイヤーによってロードされるデータソースを指定ます

2.ケース2

プロジェクト構造
ここに画像の説明を挿入します
UserDao.java

public interface UserDao {
    
    
    void getUser();
}

UserMySQLImpl .java

public class UserMySQLImpl implements UserDao{
    
    
    public void getUser() {
    
    
        System.out.println("MySQL impl.");
    }
}

UserOracleImpl.java

public class UserOracleImpl implements UserDao{
    
    
    public void getUser() {
    
    
        System.out.println("Oracle impl.");
    }
}

UserService.java

public interface UserService {
    
    
    void getUserService();
}

UserServiceImpl.java

public class UserServiceImpl implements UserService{
    
    

    private UserDao userDao;

    // set注入
    public void setUserDao(UserDao dao){
    
    
        this.userDao = dao;
    }

    public void getUserService() {
    
    
        userDao.getUser();
    }
}

次に、beans.xmlファイルを定義して、ロードする必要のあるdaoレイヤーデータソースを定義します

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="mySqlDao" class="com.dao.UserMySQLImpl"/>
    <bean id="oracleDao" class="com.dao.UserOracleImpl"/>

    <bean id="userService" class="com.service.UserServiceImpl">
        <property name="UserDao" ref="mySqlDao"/>
    </bean>
</beans>

次に、テストでテストします。

public class myTest {
    
    

    @Test
    public void Test(){
    
    
        // create and configure beans
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.getUserService();
    }
}

結果:
ここに画像の説明を挿入します
つまり、xml単純なデータソースオブジェクトを実行して、Springロード方法決定に必要なものを指定できます。newオブジェクトの作成に使用する必要がなくなり、コードの結合度を下げることができます。覚えておいてください:ApplicationContextそしてClassPathXmlApplicationContext
プログラムを変更せずに、構成ファイルを変更するだけです。割り当てと管理の
目的がSpring来ます。


ビデオアドレス:https//www.bilibili.com/video/BV1WE411d7Dv?p = 5&spm_id_from = pageDriver

おすすめ

転載: blog.csdn.net/qq_26460841/article/details/115033703