春(1)

1.春とは?
ここに画像の説明を挿入

Spring —ビジネスロジック層と他の層の間の疎結合の問題を解決するjavaSE / EEのワンストップオープンソースフレームワーク。
1.階層化
2.javaSE / EEワンストップ
3.オープンソース
4.ビジネスロジック層と他の層の間の疎結合の問題を解決し、インターフェイス指向プログラミングのアイデアのフレームワーク全体を解決します。
疎結合の問題-低結合、高凝集度の原理
結合-クラスと
凝集度の間の接続として単純に理解されます-特定の機能を実装するプログラムのコレクションとして単純に理解されます。
メリット:全身を動かす必要がありません。エラーと修正はどこにありますか?
目的:コードの再利用性を向上させ、メンテナンスを容易にします。
2.スプリング構造
ここに画像の説明を挿入

上記はSpringによって公式に提供された構造図です。
下から上へのテスト[テスト]、コアコンテナ、aop [アスペクト指向プログラミング]、Web、データアクセス
1.
テストセクションには、spring-testという1つのモジュールしかありません。.jar:スプリングテスト、junitおよびモックテスト機能を提供します
。2。コアコンテナには4つのモジュールが含まれます
。spring-core:IoCおよびDIの最も基本的な実装の依存関係の注入-beans
:BeanファクトリおよびBeanアセンブリ
spring-context:spring'sコンテキスト、つまり、IoCコンテナ
spring-expression:Spring
式言語3.aop [アスペクト指向]部分には4つのモジュールが含まれています
spring-aop:アスペクト指向プログラミング
spring-aspects:統合されたAspectJ
spring-instrument:クラスレベルを提供しますサーバーのツールサポートとClassLoaderレベルの実装spring-instrument-tomcat:tomcat
4の機器実装。webパーツには4つのモジュールが含ま
れています
。spring -web:ファイルアップロードなどの基本的なWeb関数spring-webmvc:mvc実装
spring-webmvc-portlet:ポートレットベースのmvc実装
spring-struts:支柱との統合、推奨されません、spring4
5を提供しなくなりました。データアクセス部分には5つのモジュールが含まれています
spring-jdbc:jdbcサポート
spring-tx:トランザクション制御
spring-orm:オブジェクトリレーショナルマッピング、統合ormフレームワーク
spring-oxm:オブジェクトxmlマッピング
spring-jms:javaメッセージサービス
3. Springの利点
1.便利なデカップリングと簡素化開発: Springはスーパーファクトリ(スーパーコンテナ)であり、オブジェクトの作成と依存関係をSpringファクトリに渡して管理でき
ます。2。AOPプログラミング:Springは、操作の監視、認証の検証、その他の操作を簡単に実行できるアスペクト指向のプログラミングを提供します。プログラム上
3.トランザクションの宣言:手動プログラミングなしで、トランザクションの管理を完了するために構成する必要があるだけです
4.便利なテスト:Springはjunit4をサポートし、Springアノテーションを介してプログラムをテストでき
ます5.さまざまなフレームワークの便利な統合:Springはサポートしますさまざまなオープンソースフレームワーク統合。例(struts、Hibernate、MyBatiesなど)6。JavaEEAPI
の使用の難しさを軽減する:Springは、JavaEE開発で非常に難しいAPIをカプセル化して、APIアプリケーションの開発の難しさを軽減します。4.Spring
のコアテクノロジー
1.IoC(制御の反転):Javaオブジェクトを作成および保守する権利は、管理および保守のためにSpringファクトリに引き渡されます。
2.DI(依存性注入):Javaクラスの依存オブジェクトを別のJavaクラスにすばやく追加します。
3. AOP(アスペクト指向プログラミング)、動的エージェントに基づく機能拡張方法[独自のプログラムにいくつかのシステム要件処理を追加[ログ管理、データセキュリティチェック...]]。
4.トランザクション管理に関連する操作。
5.Springはフレームワークの他のレイヤーを統合/管理します[Spring統合WebレイヤーSpringMVC / Spring統合データアクセスレイヤーMyBatis] {SSM}
5. SpringのIoC(制御の反転
はIoC(制御の反転)制御を使用しません)

public  class  Student{
    
    
	public  void  getStuInfo(){
    
    
		System.out.println(“Student类的实例方法”)}
}

テストクラスのStudentクラスのgetStuInfoにアクセスします

public class  TestMain{
  public static  void  main(String  args){
      Student  stu=new Student();
      stu.getStuInfo();
	}
}

上記のプログラムのStudentオブジェクトは開発者自身が作成したものであり、このオブジェクトを自分で管理する必要があります。
IoC(制御の
反転)を使用する場合1.Mavneプロジェクトを作成します
2.プロジェクト構造を改善します3.Pom.xml importSpring
依存関係パッケージ

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>

ここに画像の説明を挿入

4.Javaクラスを作成します

package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

5.リソースディレクトリにSpring構成ファイル[applicationcontext.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">
        <!-- 配置bean -->
        <!-- 通知Sping容器创建类对象 -->
        <!--class属性:指定创建对象的java类【包名+类名】-->
        <!--id属性:指定对象名称-->
        <bean id="stu" class="com.wangxing.spring.bean.Student"></bean>
</beans>

6.テスト

package com.wangxing.spring.bean.test;
import com.wangxing.spring.bean.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {
    public static void main(String[] args) {
        //得到Spring容器对象
        ApplicationContext ac=new 			ClassPathXmlApplicationContext("applicationcontext.xml");
        //从Spring容器中得到创建Student类对象
        Student student= (Student)ac.getBean("stu");
        student.getStuInfo();
    }
}

上記のテストコードでは、newがなく、オブジェクトを自分で作成しなかったことを示していますが、Studentクラスのインスタンスメソッドを呼び出しました。
では、Studentクラスのオブジェクトを作成したのは誰ですか?
回答:Springは、Studentクラスのオブジェクトを作成するのに役立ちました。
Springは、作成されるのがStudentクラスのオブジェクトであることをどのように認識しますか?
回答:Springの構成ファイルで構成します

<bean id="stu" class="com.wangxing.spring.bean.Student"></bean>

この要素は、SpringにStudentクラスのオブジェクトを作成するように指示します。

Springによって作成されたStudentクラスオブジェクトはどこに保存されますか?
回答:Springコンテナに格納されているので、Springコンテナオブジェクトを取得し、getBeanメソッドを介して必要なオブジェクトを取得するだけで済みます。
IoC(制御の反転)-オブジェクトの作成と保守の権利を開発者自身からSpringに移すプロセスは、IoC(制御の反転)です。Spring 1
で構成ファイルとコアオブジェクトを解析します。Springでの構成
ファイル
名:applicationcontext.xml
お勧めします場所:通常はresourcesディレクトリにあります
内容:

<?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 -->
        <!-- 通知Sping容器创建类对象 -->
        <!--class属性:指定创建对象的java类【包名+类名】-->
        <!--id属性:指定对象名称-->
        <bean id="stu" class="com.wangxing.spring.bean.Student"></bean>
</beans>

<?xml version="1.0" encoding="UTF-8"?>-xml
<beans></beans>ファイルを表します-Spring構成ファイルのルート要素

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="stu" class="com.wangxing.spring.bean.Student"></bean>クラスオブジェクト
クラス属性を作成するようにSpingコンテナに通知します。作成されたオブジェクトのjavaクラスを指定します[パッケージ名+クラス名]
id属性:指定オブジェクト名。将来Springコンテナから必要になるオブジェクトは、この属性値によって異なります。
ルート要素がBeanであり、Bean要素がオブジェクトの作成に使用されるのはなぜですか?
Beanの意味は、プログラム内のコンポーネントの意味です。Sptingは、作成するインターフェイスを持つオブジェクト/クラスオブジェクトはSpring自体のコンポーネントであると考えているため、Bean要素を使用して作成する要素を表します。Springは作成できるためです。複数のコンポーネントなので、ルート要素はBeanです。
2.SptingコンテナオブジェクトSptingコンテナオブジェクト
-Sptingによって作成されたオブジェクトを格納および保守するために使用されます。
Sptingコンテナオブジェクト
1.ApplicationContextサブインターフェイスはBeanFactory2
へのインターフェイス
です。BeanFactoryインターフェイスはApplicationContextインターフェイスオブジェクト/オブジェクトを作成しました。BeanFactoryインターフェイス
1.newClassPathXmlApplicationContext( "Spring Profile")CLASSPATH ---- [{resourcesディレクトリパスclass}] Spring構成ファイルの下にApplicationContextインターフェースオブジェクト/ BeanFactoryインターフェースオブジェクトを作成します。

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
BeanFactory ac=new ClassPathXmlApplicationContext("applicationcontext.xml");

2.new FileSystemXmlApplicationContext( "Spring構成ファイル");-システムパス[絶対パスとして理解]の下でSpring構成ファイルを検索して、ApplicationContextインターフェイスオブジェクト/ BeanFactoryインターフェイスオブジェクトを作成します。[非推奨]

ApplicationContext ac=new FileSystemXmlApplicationContext("F:\20200728\IdeaProjects\TestSpringDemo1\src\main\resources\applicationcontext.xml");
BeanFactory ac=new FileSystemXmlApplicationContext("F:\\20200728\\IdeaProjects\\TestSpringDemo1\\src\\main\\resources\\applicationcontext.xml");

Sptingコンテナオブジェクトから必要なクラスオブジェクトを取得しますか?
このSptingコンテナオブジェクトによって提供されるメソッドgetBean( "")を使用して、必要なオブジェクトを取得します。
6. Beanのインスタンス化4つの方法
1.パラメーターの構築方法がない(開発で最も一般的に使用される)

package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

Spring構成ファイル

<bean id="stu" class="com.wangxing.spring.bean.Student"></bean>

テストコード

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
//从Spring容器中得到创建Student类对象
Student student= (Student)ac.getBean("stu");
student.getStuInfo();

2. Beanをインスタンス化する静的ファクトリメソッド-クラスに静的メソッドを記述します。このメソッドはBeanオブジェクトを返します(メソッドにBeanオブジェクトを作成します)。

package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

静的ファクトリ

package com.wangxing.spring.bean;
public class StaticFactryMethodClass {
    //创建静态方法
    public  static  Student getStudentObj(){
        return  new Student();
    }
}

Spring構成ファイル

<bean id="stu" class="com.wangxing.spring.bean.StaticFactryMethodClass" factory-method="getStudentObj"></bean>

テストコード

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
Student student=(Student)ac.getBean("stu");
student.getStuInfo();

3. Beanをインスタンス化するインスタンスファクトリメソッド—クラスにインスタンスを書き込むメソッド。このメソッドはBeanオブジェクトを返します(Beanオブジェクトはメソッドで作成されます)。

package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

インスタンスファクトリ

package com.wangxing.spring.bean;
public class FactoryMethodClass {
    //创建实例方法
    public    Student getStudentObj(){
        return  new Student();
    }
}

Spring構成ファイル

<!-- 实例工厂方法配置 -->
<!-- 1.创建实例工厂对象 -->
<bean id="factoryMethodClass" class="com.wangxing.spring.bean.FactoryMethodClass"></bean>
<!-- 2.配置由实例工厂创建Student对象-->
<bean id="stu" factory-bean="factoryMethodClass" factory-method="getStudentObj"></bean>

テストコード

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
Student student=(Student)ac.getBean("stu");
student.getStuInfo();

4.Beanをインスタンス化するためのFactoryBeanインターフェースメソッド

package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

FactoryBeanインターフェースを実装するサブクラス

package com.wangxing.spring.bean;
import org.springframework.beans.factory.FactoryBean;
public class FactoryBeanSunClass implements FactoryBean<Student> {
    @Override
    public Student getObject() throws Exception {
        return new Student();
    }
    @Override
    public Class<?> getObjectType() {
        return Student.class;
    }
}

Spring構成ファイル

 <!-- 配置FactoryBean接口实现 -->
<bean id="stu" class="com.wangxing.spring.bean.FactoryBeanSunClass"></bean>
测试代码
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
Student student=(Student)ac.getBean("stu");
student.getStuInfo();

7.FactoryBeanインターフェースとBeanFactoryインターフェースの違い
BeanFactoryインターフェース(Sptingコンテナー)は、Spting [管理]
FactoryBeanインターフェースによって作成されたオブジェクトを格納および保守するために使用されます-------------------使用されますJavaオブジェクトを作成する【作成】

おすすめ

転載: blog.csdn.net/guoguo0717/article/details/109730078