Spring dependency injection Dependency Injection (DI)

Spring appears unable to read the documentation issues:

Here Insert Picture DescriptionThe reason : The system comes packaged plug-ins can only be the code package, can not rely on packaged items, making it impossible to load a local xsd file, and then to the Internet to find, but it will not be found error, can not start the project.
Solution :
1, the introduction of new packaging the plug-in pom.xml <build> tag:

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

2, at <pluginManagement> tag <plugins> add plug-in configuration:

 <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>

Lazy loading (lazy-init)

SpringIOC container will initialize all the default configuration bean we start at the time, but sometimes we want some of the bean initialization delay time, when we re-initialization of getBean.
In this case it is necessary to use lazy loading, adding lazy-init bean properties above, property values can be true, false, default. The default is false.

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

You can also set global bean lazy load defaults

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

Notes configuration import xml configuration

Obtain context through annotation:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(StudentServiceImpl.class);

In the comment may be introduced using the xml configuration @ImportResource

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

bean's name property

In addition to adding bean id, the name may be added, as identified in the bean during getBean, name can simultaneously set a plurality of values, and different bean's name is not the same.

<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>

Injection anonymous inner bean

Without introducing external ref bean by embodiments, but the direct injection internal bean manner

<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>

Injection type attribute set

By <list />, <set / >, <map />, <props /> tag in the injection java List, Set, Map, Properties collection type attribute
can change the type of value type, so as to set a different value types

<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>

Injecting null, empty string type attribute value

Use <null /> tag is injected null, value upon injection empty string is empty

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

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

Injection composite attribute value

There is a JinSaiSai class has a yinSaiSai property, yinSaiSai property also contains an age property, property assignment needs to age 25

<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>

Injecting property value of the external file properties

Injection Properties file attributes tools 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

By injecting property namespace p or c

First add p and c of the schema in beans

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

And c instead of using p <property>, <constructor-arg> tag

<!-- 通过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 namespace using a more concise than the above, but it is at the time of writing requires the use of automatic prompts IDE (such as IDEA, STS), or at the time of writing it is easy to spelling errors, only to run only to find.

Guess you like

Origin blog.csdn.net/csdn_hmt/article/details/91897758