スプリングフレームワークIOC(反転制御)およびDI(依存性注入)

ドキュメントの読み込みIDEA実行エラー

書かれたMavenプロジェクト時折エラーIDEAを実行しています

分析とBaiduの後、間違った理由:プロジェクトがパッケージに依存しないが、IDEAは、ローカルファイルが、ネットワークのクエリをロードすることはできません。
解決策:プラグインのビルドのタグを追加するには、次の構成コードを追加し、設定ファイルMavenのプロジェクトのpom.xmlを変更します。
<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
      </plugin>
 </plugins>
追加次のコードは、pluginManagementタグに配置されたプラグの先頭に追加されます。
<plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>2.4.3</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
              <configuration>
                <transformers>
                  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <mainClass>com.lanou3g.spring.App</mainClass>
                  </transformer>
                  <transformer
                          implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <resource>META-INF/spring.schemas</resource>
                  </transformer>
                  <transformer
                          implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                    <resource>META-INF/spring.handlers</resource>
                  </transformer>
                </transformers>
              </configuration>
            </execution>
          </executions>
</plugin>

遅延読み込み

コードは、私たちのミスでない場合、起動時にIOCのコンテナのデフォルトは、すべてのBean設定ファイルを初期化しますが、ミスはすぐに私たちがコーディングエラーを除外支援するために、コンテナのヒントをロードすることができ、これは、ほとんどの場合変更することなく、フェイルファストと呼ばれ、我々はプロジェクトを開始する必要がないときには、我々は一般的に、負荷非常に多くのリソースを使用していないコンテナは、このような機能のプロジェクトとして、スタートアップBeanオブジェクトにロードされた特別な時間を必要としない、プロジェクトは無駄な時間に敏感削減されます、あなたは、Beanを設定する必要があります遅延ロードオブジェクト、専用Beanプラス怠惰-INIT属性の対象とされるであろう、それをロードし、その後とき(getBean)それを呼び出すと、属性値がtrueの場合、デフォルトでは次のように一般的に記述していない、偽です。

 <bean id="studentDao" class="com.lanou3g.spring.dao.StudentDaoImpl" lazy-init="true"/>

XMLコンフィギュレーションを紹介する注釈を使用します

複数のプロファイルのケースでは、XMLのコンフィギュレーションファイル追加の設定ファイルでご紹介します次のように、追加の設定ファイルを導入インポートラベルを使用することです。

<import resource="applicationContext.xml"/>

これを行うには、注釈の方法でXML設定ファイルの導入:

@ImportResource("applicationContext.xml")

これら2つのアプローチの役割が同じで、いずれかを把握することができます。

Beanのnameプロパティ

name属性は、共通の豆ではないので、ここで説明するのはあまりないが、現在のBeanオブジェクトへのid属性としての役割は、name属性の値は、複数の名前を作るために設けられてもよいことを除いて、名前へのアクセスを容易にマークされ、カンマ区切りの間、名前の複数のノートは、繰り返すことができず、他のBean名に属性値が繰り返されません。

<bean id="student" name="stu,student1,sd1" class="com.lanou3g.spring.bean.Student">

インジェクション匿名の内部豆

注射匿名の内部ビーンこの方法は、オブジェクトの割り当てに割り当てられたオブジェクトに対応し、一方は新しいオブジェクトを介して2つの方法が、存在すると、次いで一つによってオブジェクトプロパティの割り当て、送信パラメータにオブジェクトのプロパティを設定するパラメータ化コンストラクタであります次のようにオブジェクトによって作成された豆の設定ファイル内のオブジェクトに割り当てられたオブジェクトに割り当てることができ、慣行です。

<bean id="outer" class="...">
    <!-- 像set一样实例化对象的方式用这样的注入方式 -->
    <property name="target">
        <bean class="com.example.Person"> <!-- this is the inner bean -->
            <property name="name" value="Fiona Apple"/>
            <property name="age" value="25"/>
        </bean>
    </property>
  	
  	<!-- 像带参构造函数实例化对象的方式用这样的注入 -->
    <constructor-arg name="target">
        <bean class="com.example.Person"> <!-- this is the inner bean -->
            <property name="name" value="Fiona Apple"/>
            <property name="age" value="25"/>
         </bean>
    </constructor-arg>
</bean>

インジェクションタイプ属性セット

相互リンクされ自然を注入し、割り当てられている、リスト注入法、地図の注入方法を説明していません。

 <!-- lsit注入 -->
<property name="someList">
        <list>
            <!-- 普通元素 -->
          	<value>a list element followed by a reference</value>
            <!-- 引用类型元素 -->
          	<ref bean="myDataSource" />
        </list>
</property>
 <!-- Map注入 -->
<property name="someMap">
        <map>
          	<entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
</property>

そして、ヌルヌル文字列の属性値を注入します

<bean class="ExampleBean">
    <property name="email" value=""/>
</bean>

<bean class="ExampleBean">
    <property name="email">
         <!-- 注意这里注入null必须写这样的标签形式,测试时报空指针,value=“null”不行,编译器把它当字			   符串,长度为4 -->
        <null/>
    </property>
</bean>

インジェクション複合属性値

<bean id="something" class="things.ThingOne">
    <property name="fred.bob.sammy" value="123" />
</bean>

C注入の名前空間の設定パラメータ

C名前空間とオブジェクトのインスタンスと同じように設定された従来の注入法の性質は、唯一の構成ファイルのヘッダー情報を変更する必要が
Cによってすることができます:従来の注入法が簡便であるよりも、プロパティ値を注入します
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         <!-- 这里需要配置 -->
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="beanTwo" class="x.y.ThingTwo"/>
    <bean id="beanThree" class="x.y.ThingThree"/>
    <!-- c命名空间方式注入构造参数 -->
    <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
        c:thingThree-ref="beanThree" c:email="[email protected]"/>
</beans>

P注入の名前空間の設定パラメータ

同じインスタンスとオブジェクトの従来の射出引数コンストラクタの性質及び方法等P名前空間には、専用の設定ファイルのヘッダー情報を変更する必要が
私たちすることができ、P経由:従来の注入法が簡便であるよりも、プロパティ値を注入します
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      <!-- 这里需要配置 -->
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="driver" class="com.demo.Driver" p:car-ref="car" p:dname="张三" />
  <bean id="car" class="com.demo.Car" />
</beans>

PropertyPlaceholderConfigurer注入ツールのプロパティファイルの属性

IOCコンテナが必要にデータベースを変更するとき以来、様々なパラメータの値をより効率的に、データベース接続をロードします開始すると、次の注入モードを使用して、値のみjdbc.propertiesファイルを変更する必要があります。

<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          p:driverClassName="${jdbc.driver.className}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"
          p:maxIdle="${jdbc.maxIdle}"
          p:minIdle="${jdbc.minIdle}"
          p:maxActive="${jdbc.maxActive}"
    />

jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.username=root
jdbc.password=root
jdbc.maxIdle=3
jdbc.minIdle=1
jdbc.maxActive=10

おすすめ

転載: blog.csdn.net/weixin_43311054/article/details/92110965