春の依存性注入依存性注入(DI)

春はドキュメントの問題を読み取ることができません表示されます:

ここに画像を挿入説明理由:システムが来るパッケージ化プラグインのみ、コードパッケージすることができ見つけるために、インターネットに、その後、パッケージのアイテムに頼ることができなくローカルXSDファイルをロードすること、およびことはできませんが、それは誤りを発見されることはありません、プロジェクトを開始することはできません。
ソリューション
1、新しいパッケージングプラグインのpom.xmlタグ<ビルド>の紹介:

<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
      </plugin>
 </plugins>

2、で<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>

遅延ロード(レイジーINIT)

SpringIOCコンテナは、私たちが一度に開始、しかし、我々はgetBeanの再初期化時に、時には私たちは、豆の初期化遅延時間のいくつかをしたいすべてのデフォルトのコンフィグレーションBeanを初期化します。
この場合には、遅延読み込みを使用する必要があり、上記遅延-INIT Beanプロパティを追加して、プロパティ値は、TRUE、FALSE、デフォルトとすることができます。デフォルトはfalseです。

<bean id="ssdi" class="com.lanou3g.spring.service.StudentServiceImpl"
     init-method="myInit" destroy-method="myDestroy" lazy-init="true">
</bean>

また、グローバルな豆遅延ロードのデフォルト値を設定することができます

<beans default-lazy-init="true">
    <!-- 下面配置的所有bean默认都会开启懒加载 -->
</beans>

注意設定のインポートxml設定

注釈を通じてコン​​テキストを取得します。

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(StudentServiceImpl.class);

コメントのXML構成を使用して導入することができる@ImportResource

@Configuration("ssdi")
@ComponentScan(basePackages = "com.lanou3g.spring")
@Scope("prototype") // 作用域,单例或者非单例
@ImportResource("applicationContext.xml")

Beanのnameプロパティ

getBean中豆で識別されるビーンIDを付加することに加えて、名前は、添加してもよく、名前は、同時に複数の値を設定することができ、そして異なるBeanの名前は同じではありません。

<bean id="student" name="stu,student1,sd1" class="com.lanou3g.spring.bean.Student">
        <constructor-arg name="nickName" value="三哥" />
        <constructor-arg name="sname" value="张三" />
        <constructor-arg name="fruit" ref="banana" />
</bean>

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

実施形態によって外部REF Beanを導入することなく、しかし、直接噴射豆内部様式

<bean id="outer" class="...">
    <!-- setter方式用内部bean的方式注入 -->
    <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>
  	
  	<!-- 构造参数也支持用内部匿名bean的方式注入 -->
    <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>

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

<リスト/>、<セット/ことで >、<マップ/>、<小道具/> 射出JavaのListのタグ、セット、地図、プロパティコレクション型の属性が
異なる値の型を設定するように、値型の種類を変更することができます

<bean id="teacher" class="com.lanou3g.spring.bean.Teacher">		
    <property name="someList">
        <list>
          	<value>a list element followed by a reference</value>
          	<value type="java.lang.Integer">55</value>
          	<ref bean="myDataSource" />
        </list>
    </property>
    <property name="someMap">
        <map>
          	<entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
    </property>
    <property name="someSet">
        <set>
            <value>just some string</value>
            <ref bean="myDataSource" />
        </set>
    </property>
    <property name="adminEmails">
        <props>
            <prop key="administrator">[email protected]</prop>
            <prop key="support">[email protected]</prop>
            <prop key="development">[email protected]</prop>
        </props>
    </property>
</bean>

nullの場合、空の文字列型の属性値を注入します

使用<NULL />タグがNULLで注入され、注入空の文字列時の値が空であります

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

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

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

プロパティの割り当てが25歳に必要、JinSaiSaiクラスはyinSaiSaiプロパティがあり、yinSaiSaiプロパティはまた、年齢プロパティが含まれていますがあります

<bean id="saiSai" class="com.lanou3g.spring.bean.JinSaiSai">
	<property name="yinSaiSai">
            <bean class="com.lanou3g.spring.bean.YinSaiSai" />
    </property>
    <property name="yinSaiSai.age" value="25" />
</bean>

外部ファイルのプロパティの注入性値

インジェクションプロパティファイルには、ツールを属性PropertyPlaceholderConfigurer

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

<bean id="jdbcConf" class="com.lanou3g.spring.bean.JDBCConf">
        <property name="url" value="${jdbc.url}" />
        <property name="driver" value="${jdbc.driver.className}" />
        <property name="userName" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
</bean>

jdbc.properties

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

プロパティの名前空間のpまたはcを注入することにより、

まず豆スキーマのpとCを追加

<beans ...
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
      ...>

そしてp <プロパティ>、<コンストラクタ、引数>タグを使用して、Cの代わりに、

<!-- 通过p命名空间来注入属性 -->
<!--<bean id="yunjie1" class="com.lanou3g.spring.bean.YunJie">
        <property name="sname" value="云姐" />
    </bean>-->
    <!-- 等效于上面的配置 -->
    <bean id="yunjie1" class="com.lanou3g.spring.bean.YunJie" p:sname="云姐" />

    <!-- 通过c命名空间来注入构造参数 -->
    <!--<bean id="yunjie2" class="com.lanou3g.spring.bean.YunJie">
        <constructor-arg name="sname" value="雲杰" />
    </bean>-->
    <!-- 等效于上面的配置 -->
    <bean id="yunjie2" class="com.lanou3g.spring.bean.YunJie" c:sname="雲杰" />

上記より簡潔使用して、それは執筆時点であるPの名前空間は、(IDEA、STSなど)自動プロンプトのIDEを使用する必要がある、またはそれを書いている時にのみだけ見つけるために実行するために、スペルミスに簡単です。

おすすめ

転載: blog.csdn.net/csdn_hmt/article/details/91897758