最初の部分:SpringのIoC(制御の反転)
は、下の比喩的なイメージIoC
、つまりデカップリングです。
IoC
これはSpring
フレームワークのコアコンテンツであり、さまざまな方法で実装されますIoC
。XML
構成または注釈を使用できます。で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