Spring依赖注入Dependency Injection(DI)

Spring出现无法读取文档问题:

在这里插入图片描述原因:系统自带的打包插件只能把代码打包,不能把项目的依赖打包,导致无法加载本地的xsd文件,然后到网上找,但也不能找到就会报错,无法启动项目。
解决方法
1、在pom.xml中<build>标签下引入新的打包插件:

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

2、在<pluginManagement>标签下的<plugins>中添加插件配置:

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

SpringIOC容器默认会在启动的时候初始化我们配置的所有bean,但有时我们想让一些bean延迟初始化的时机,在我们getBean的时候再初始化。
这时就需要使用懒加载,在bean上面添加lazy-init属性,属性值可以是true、false、default。 默认情况是false。

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

也可以设置全局的bean懒加载默认值

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

注解配置导入xml配置

通过注解获取上下文时:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(StudentServiceImpl.class);

在注解中使用@ImportResource可以导入xml配置

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

bean的name属性

除了为bean添加id外,也可以添加name作为bean的标识,在getBean时使用,name可以同时设置多个值,而且不同的bean的name不能相同。

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

注入匿名内部bean

不通过ref方式引入外部的bean,而是直接用内部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>

注入集合类型属性

通过<list/>、<set/>、<map/>、<props/>标签对属性注入java中的List、Set、Map、Properties集合类型
可改变value的type类型,从而设置不同的值类型

<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/>标签,注入空字符串时value的值为空

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

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

注入复合属性值

有一个JinSaiSai类中有一个yinSaiSai属性,yinSaiSai属性中又包含了一个age属性,需要给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>

注入外部properties文件中的属性值

通过PropertyPlaceholderConfigurer工具类注入Properties文件中的属性

<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命名空间注入属性

先在beans中添加p和c的schema

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

使用p和c代替<property>、<constructor-arg>标签

<!-- 通过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命名空间比上面一种更简洁,但是它在编写的时候需要使用支持自动提示的IDE(如IDEA、STS),否则在编写时很容易拼写错误,只能到运行期才发现。

猜你喜欢

转载自blog.csdn.net/csdn_hmt/article/details/91897758