(三)測試學(xué)習(xí)JavaWeb之Spring IOC

前言

對于傳統(tǒng)的應(yīng)用程序,一般通過new object()來創(chuàng)建對象躲胳,主動(dòng)權(quán)和創(chuàng)建時(shí)機(jī)由自己把控闸溃,而IOC意味著將創(chuàng)建和查找依賴對象的控制權(quán)交給了容器,由容器進(jìn)行注入組合對象疹鳄。對象的獲取由主動(dòng)變?yōu)楸粍?dòng),這就是IOC控制反轉(zhuǎn)的設(shè)計(jì)思想芦岂。


IOC

快速入門

項(xiàng)目工程
pom依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Spring</groupId>
    <artifactId>SpringLearn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
            <spring.version>4.1.4.RELEASE</spring.version>
    </properties>

    <dependencies>

        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

    </dependencies>
</project>
代碼示例

創(chuàng)建HelloWorld接口

package com.demo;

public interface HelloWorld {
    void sayHello();
}

創(chuàng)建接口實(shí)現(xiàn)類

package com.demo;

public class SpringHelloWorld implements HelloWorld {

    public void sayHello() {
        System.out.println("Spring say Hello!");
    }
}

創(chuàng)建service層

package com.demo;

public class HelloWorldService {

    //@Autowired
    //@Qualifier("strutsHelloWorld")
    private HelloWorld hello;
    private String name;

    public void setHelloWorld(HelloWorld hello) {
        this.hello = hello;
    }

    public void setName(String name){
        this.name = name;
    }

    public HelloWorld getHelloWorld() {
        System.out.println(this.name);
        return hello;
    }
}

配置bean文件瘪弓,property的name屬性的值需要與HelloWorldService類的set方法名后半部分名稱一致,比如setHelloWorld方法禽最,則name值為helloWorld腺怯。ref指向創(chuàng)建的對象springHelloWorld,作用是把對象傳遞給HelloWorldService的HelloWorld屬性川无,該屬性必須擁有set方法(上述代碼的setHelloWorld方法)才能注入成功呛占。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!--context:annotation-config /-->

    <!-- 聲明springHelloWorld對象,由spring創(chuàng)建 -->
    <bean id="springHelloWorld"
          class="com.demo.SpringHelloWorld"></bean>
    
    <!-- 聲明helloWorldService對象,由spring創(chuàng)建 -->
    <bean id="helloWorldService"
          class="com.demo.HelloWorldService">
        <!--注入springHelloWorld對象,需要setHelloWorld方法-->
        <property name="helloWorld" ref="springHelloWorld"/>
        <!--注入name變量值懦趋,需要setName方法-->
        <property name="name" value="Tomandy"/>
    </bean>

</beans>

創(chuàng)建測試類

package com.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHelloWorld {

    public static void main(String[] args) {
        //加載配置文件晾虑,默認(rèn)查找classpath路徑下的文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("helloWorldBeans.xml");

        //spring默認(rèn)創(chuàng)建是單實(shí)例的作用域
        HelloWorldService helloWorldService =
                (HelloWorldService) context.getBean("helloWorldService");

        HelloWorld helloWorld = helloWorldService.getHelloWorld();
        helloWorld.sayHello();
    }
}

運(yùn)行測試類,輸出結(jié)果為

"C:\Program Files (x86)\Java\jdk1.8.0_111\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\lib\idea_rt.jar=36108:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\github\SpringLearn\target\test-classes;D:\github\SpringLearn\target\classes;C:\Users\lenovo\.m2\repository\org\springframework\spring-core\4.1.4.RELEASE\spring-core-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-context\4.1.4.RELEASE\spring-context-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-aop\4.1.4.RELEASE\spring-aop-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-beans\4.1.4.RELEASE\spring-beans-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-expression\4.1.4.RELEASE\spring-expression-4.1.4.RELEASE.jar" com.demo.TestHelloWorld
三月 01, 2019 2:13:54 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@197848c: startup date [Fri Mar 01 14:13:54 CST 2019]; root of context hierarchy
三月 01, 2019 2:13:54 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [helloWorldBeans.xml]
Tomandy
Spring say Hello!

Process finished with exit code 0

通過以上示例愕够,可輕松了解IOC的使用了走贪,即通過xml配置文件,spring可以幫助我們創(chuàng)建對象springHelloWorld惑芭,并注入helloWorldService中坠狡,上述代碼使用的setter注入方式,還可以通過構(gòu)造函數(shù)遂跟、注解逃沿、靜態(tài)工廠婴渡、實(shí)例工廠來注入,下面文章再說明凯亮。
除了通過ClassPathXmlApplicationContext去加載spring的配置文件边臼,還可以通過FileSystemXmlApplicationContext去加載,區(qū)別在于前者默認(rèn)查找classpath路徑下的文件假消,后者默認(rèn)查找項(xiàng)目工作路徑柠并,即項(xiàng)目的根目錄,文章《關(guān)于Spring IOC (DI-依賴注入)你需要知道的一切》舉例如下富拗。

//默認(rèn)查找classpath路徑下的文件 
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring/spring-ioc.xml"); 
//多文件,也可傳遞數(shù)組 
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring/spring-ioc.xml","spring/spring-ioc2.xml",.....); 
//默認(rèn)為項(xiàng)目工作路徑 即項(xiàng)目的根目錄 
FileSystemXmlApplicationContext applicationContext= new FileSystemXmlApplicationContext("/src/main/resources/spring/spring-ioc.xml"); 
//也可以讀取classpath下的文件 
FileSystemXmlApplicationContext applicationContext=new FileSystemXmlApplicationContext("classpath:spring/spring-ioc.xml"); 
//使用前綴file 表示的是文件的絕對路徑 
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("file:D:/app.spring.xml"); 
//多文件與ClassPathXmlApplicationContext相同

基于xml的自動(dòng)裝配

上述例子bean文件配置了property屬性臼予,實(shí)際屬于手工裝配,spring也提供了自動(dòng)裝配的方式啃沪,分別有byName,byType,constructor三種方式粘拾,下面通過舉例來說明他們之間的區(qū)別。

  • byName创千,顧名思義就是按照名稱匹配缰雇。根據(jù)bean名稱去和set方法的后半部分名稱匹配,如果一致追驴,則注入成功械哟,否則失敗。
    配置bean文件
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <bean id="helloWorld"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="helloWorldService"
          autowire="byName"
          class="com.demo.HelloWorldService">
    </bean>

</beans>

創(chuàng)建HelloWorldService

package com.demo;

public class HelloWorldService {

    private HelloWorld helloWorld;

    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

byName模式會(huì)根據(jù)beanid helloWorld去和setHelloWorld方法的后半部分名稱(HelloWorld)比對氯檐,如果(首字母轉(zhuǎn)換為大寫)一致則注入成功戒良。

  • byType体捏,即按照類型匹配冠摄。Spring容器會(huì)基于反射查看bean定義的類,然后找到與set方法參數(shù)類型相同的bean來注入几缭,需要通過set方法來實(shí)現(xiàn)河泳。
    配置bean文件,注意用到了autowire屬性年栓。
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <bean id="springHelloWorld"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="helloWorldService"
          autowire="byType"
          class="com.demo.HelloWorldService">
    </bean>

</beans>

創(chuàng)建HelloWorldService

package com.demo;

public class HelloWorldService {

    private HelloWorld helloWorld;

    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

運(yùn)行測試類拆挥,輸出結(jié)果為

Spring say Hello!

當(dāng)存在多個(gè)相同類型的bean時(shí),使用byType方式會(huì)報(bào)錯(cuò)某抓,此時(shí)可以通過autowire-candidate="false"來跳過相同類型的bean纸兔。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <bean id="springHelloWorld"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="springHelloWorld1"
          autowire-candidate="false"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="helloWorldService"
          autowire="byType"
          class="com.demo.HelloWorldService">
    </bean>

</beans>
  • constructor,在該模式下Spring容器同樣會(huì)嘗試找到那些類型與構(gòu)造函數(shù)相同匹配的bean注入否副。當(dāng)存在多個(gè)類型相同bean時(shí)汉矿,按名稱優(yōu)先匹配,如果沒有找到對應(yīng)名稱备禀,則注入失敗洲拇,此時(shí)可以使用autowire-candidate=”false” 過濾來解決奈揍。
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <!--如果存在多個(gè)相同類型的,則按byName來匹配-->
    <bean id="helloWorld"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="helloWorld2"
          autowire-candidate="false"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="helloWorldService"
          autowire="constructor"
          class="com.demo.HelloWorldService">
    </bean>

</beans>
package com.demo;

public class HelloWorldService {

    private HelloWorld helloWorld;

    public HelloWorldService(HelloWorld helloWorld){
        this.helloWorld = helloWorld;
    }

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

依賴注入

除了上文提到的setter注入赋续,還可以通過構(gòu)造函數(shù)男翰、注解、靜態(tài)工廠纽乱、實(shí)例工廠注入蛾绎。

構(gòu)造函數(shù)注入

spring容器會(huì)根據(jù)bean中指定的構(gòu)造函數(shù)參數(shù)來決定調(diào)用那個(gè)構(gòu)造函數(shù)。
創(chuàng)建HelloWorldService

package com.demo;

import java.util.List;
import java.util.Map;

public class HelloWorldService {

    //@Autowired
    //@Qualifier("strutsHelloWorld")
    private HelloWorld hello;
    private String name;
    private List<String> list;
    private Map<String, String> map;


    public HelloWorldService(HelloWorld hello, String name, List<String> list, Map<String, String> map) {
        this.hello = hello;
        this.name = name;
        this.list = list;
        this.map = map;
    }

    public HelloWorld getHelloWorld() {
        System.out.println(this.name);
        for (int i = 0; i < list.size(); i++) {
            System.out.println("第" + i + "個(gè)列表值:" + list.get(i));
        }

        for (String key : map.keySet()) {
            System.out.println("key為:" + key + " value為:" + map.get(key));
        }
        return hello;
    }
}

配置xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!--context:annotation-config /-->

    <bean id="springHelloWorld"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="helloWorldService"
          class="com.demo.HelloWorldService">
        <!--注入鸦列,需要set方法-->
        <!--
        <property name="helloWorld" ref="springHelloWorld"/>
        <property name="name" value="Tomandy"/>
        -->
        <constructor-arg ref="springHelloWorld"/>
        <constructor-arg type="java.lang.String" value="Tomandy"/>
        <constructor-arg>
            <list>
                <value>tom1</value>
                <value>tom2</value>
            </list>
        </constructor-arg>
        <constructor-arg>
            <map>
                <entry key="key1" value="value1">
                </entry>

                <entry key="key2" value="value2">
                </entry>
            </map>
        </constructor-arg>

    </bean>

</beans>

運(yùn)行測試后秘通,輸出結(jié)果如下。

"C:\Program Files (x86)\Java\jdk1.8.0_111\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\lib\idea_rt.jar=42191:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\github\SpringLearn\target\test-classes;D:\github\SpringLearn\target\classes;C:\Users\lenovo\.m2\repository\org\springframework\spring-core\4.1.4.RELEASE\spring-core-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-context\4.1.4.RELEASE\spring-context-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-aop\4.1.4.RELEASE\spring-aop-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-beans\4.1.4.RELEASE\spring-beans-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-expression\4.1.4.RELEASE\spring-expression-4.1.4.RELEASE.jar" com.demo.TestHelloWorld
三月 01, 2019 4:17:54 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@197848c: startup date [Fri Mar 01 16:17:54 CST 2019]; root of context hierarchy
三月 01, 2019 4:17:54 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [helloWorldBeans.xml]
Tomandy
第0個(gè)列表值:tom1
第1個(gè)列表值:tom2
key為:key1 value為:value1
key為:key2 value為:value2
Spring say Hello!

Process finished with exit code 0

注解注入

需在bean文件加上以下內(nèi)容敛熬,注解注入才生效肺稀。

<context:annotation-config />
  • 可以通過@Autowired對成員變量、set方法应民、構(gòu)造函數(shù)進(jìn)行標(biāo)注來實(shí)現(xiàn)依賴注入话原,該注解默認(rèn)按byType匹配,如果需要按byName匹配诲锹,可以通過@Qualifier來指定對象的名稱繁仁,指定的名稱需與bean文件的對象名稱一致,否則會(huì)報(bào)錯(cuò)归园。
    創(chuàng)建HelloWorldService
package com.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class HelloWorldService {

    @Autowired
    @Qualifier("springHelloWorld")
    private HelloWorld helloWorld;

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

配置bean文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <bean id="springHelloWorld"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="helloWorldService"
          class="com.demo.HelloWorldService">
    </bean>

</beans>

運(yùn)行測試類黄虱,輸出結(jié)果如下。

"C:\Program Files (x86)\Java\jdk1.8.0_111\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\lib\idea_rt.jar=47086:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\github\SpringLearn\target\test-classes;D:\github\SpringLearn\target\classes;C:\Users\lenovo\.m2\repository\org\springframework\spring-core\4.1.4.RELEASE\spring-core-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-context\4.1.4.RELEASE\spring-context-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-aop\4.1.4.RELEASE\spring-aop-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-beans\4.1.4.RELEASE\spring-beans-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-expression\4.1.4.RELEASE\spring-expression-4.1.4.RELEASE.jar" com.demo.TestHelloWorld
三月 01, 2019 5:48:05 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@197848c: startup date [Fri Mar 01 17:48:05 CST 2019]; root of context hierarchy
三月 01, 2019 5:48:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [helloWorldBeans.xml]
Spring say Hello!

Process finished with exit code 0

通過以上例子可發(fā)現(xiàn)庸诱,通過標(biāo)注成員變量捻浦,可以省略set方法和構(gòu)造函數(shù),大大簡化了代碼桥爽。

  • @Resource的功能與@Autowired類似朱灿,但不能標(biāo)注構(gòu)造函數(shù)。默認(rèn)按照byName進(jìn)行裝配钠四,名稱可以通過name屬性進(jìn)行指定盗扒,如果沒有指定name屬性,當(dāng)注解寫在成員變量上時(shí)缀去,默認(rèn)取成員變量名進(jìn)行byName查找侣灶,如果注解寫在setter方法上默認(rèn)取屬性名進(jìn)行裝配。 當(dāng)找不到與名稱匹配的bean時(shí)才按照byType進(jìn)行裝配缕碎。
package com.demo;

import javax.annotation.Resource;

public class HelloWorldService {

    // 先按helloWorld去查找bean對象褥影,找不到再byType
    @Resource
    private HelloWorld helloWorld;

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

package com.demo;

import javax.annotation.Resource;

public class HelloWorldService {

    //通過byName查找
    @Resource(name = "springHelloWorld")
    private HelloWorld helloWorld;

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

package com.demo;

import javax.annotation.Resource;

public class HelloWorldService {

    //通過byType查找
    @Resource(type = HelloWorld.class)
    private HelloWorld helloWorld;

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

  • @Autowired和@Resource不適合簡單類型的裝配,如int,String等阎曹∥苯祝可以通過@Value來注入煞檩,該注解一般與properties文件結(jié)合使用,其提了兩種使用方式栅贴,SPEL表達(dá)式和占位符斟湃,舉例如下。
    新建config.properties文件
url=http://10.1.1.1
port=8080
username=Tomandy
password=Tom123

配置bean文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <!--基于占位符方式-->
    <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="location" value="config.properties"/>
    </bean>

    <!--基于SPEL表達(dá)式-->
    <bean id="configSpel" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>config.properties</value>
            </list>
        </property>
    </bean>

    <bean id="springHelloWorld"
          class="com.demo.SpringHelloWorld"></bean>

    <bean id="helloWorldService"
          class="com.demo.HelloWorldService">
    </bean>

</beans>

創(chuàng)建HelloWorldService

package com.demo;

import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;

public class HelloWorldService {

    @Resource(type = HelloWorld.class)
    private HelloWorld helloWorld;

    @Value("${url}")
    private String url;

    @Value("#{configSpel['username']}")
    private String userName;

    public HelloWorld getHelloWorld() {
        System.out.println(this.url);
        System.out.println(this.userName);
        return helloWorld;
    }
}

運(yùn)行測試類檐薯,輸出結(jié)果如下

"C:\Program Files (x86)\Java\jdk1.8.0_111\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\lib\idea_rt.jar=48850:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\github\SpringLearn\target\test-classes;D:\github\SpringLearn\target\classes;C:\Users\lenovo\.m2\repository\org\springframework\spring-core\4.1.4.RELEASE\spring-core-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-context\4.1.4.RELEASE\spring-context-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-aop\4.1.4.RELEASE\spring-aop-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-beans\4.1.4.RELEASE\spring-beans-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-expression\4.1.4.RELEASE\spring-expression-4.1.4.RELEASE.jar" com.demo.TestHelloWorld
三月 01, 2019 6:23:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@197848c: startup date [Fri Mar 01 18:23:41 CST 2019]; root of context hierarchy
三月 01, 2019 6:23:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [helloWorldBeans.xml]
三月 01, 2019 6:23:41 下午 org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [config.properties]
三月 01, 2019 6:23:41 下午 org.springframework.beans.factory.config.PropertiesFactoryBean loadProperties
信息: Loading properties file from class path resource [config.properties]
http://10.1.1.1
Tomandy
Spring say Hello!

Process finished with exit code 0

靜態(tài)工廠凝赛、實(shí)例工廠注入

基于工廠注入的方式參考以下文章《spring的五種依賴注入方式》

@Service及@Repository注解

在bean配置文件加上包掃描路徑坛缕,將自動(dòng)掃描路徑包墓猎,如果類帶了@Service注解,將自動(dòng)注冊到Spring容器赚楚,不需要在bean配置文件定義bean了毙沾,類似的注解還有@Component、@Repository宠页、@Controller左胞,舉例如下。
配置bean文件举户,通過使用@Service注解烤宙,替換了原先的HelloWorldService的bean配置。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- 聲明包掃描 -->
    <context:component-scan base-package="com.demo" />

    <bean id="springHelloWorld"
          class="com.demo.SpringHelloWorld"></bean>

</beans>

創(chuàng)建HelloWorldService

package com.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("helloWorldService")
public class HelloWorldService {

    @Autowired
    private HelloWorld helloWorld;

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

創(chuàng)建測試類

package com.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHelloWorld {


    public static void main(String[] args) {
        //加載配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("helloWorldBeans.xml");

        //spring默認(rèn)創(chuàng)建是單實(shí)例的作用域
        HelloWorldService helloWorldService =
                (HelloWorldService) context.getBean("helloWorldService");

        HelloWorld helloWorld = helloWorldService.getHelloWorld();
        helloWorld.sayHello();
    }
}

運(yùn)行測試類俭嘁,輸出結(jié)果為

"C:\Program Files (x86)\Java\jdk1.8.0_111\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\lib\idea_rt.jar=52126:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\github\SpringLearn\target\test-classes;D:\github\SpringLearn\target\classes;C:\Users\lenovo\.m2\repository\org\springframework\spring-core\4.1.4.RELEASE\spring-core-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-context\4.1.4.RELEASE\spring-context-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-aop\4.1.4.RELEASE\spring-aop-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-beans\4.1.4.RELEASE\spring-beans-4.1.4.RELEASE.jar;C:\Users\lenovo\.m2\repository\org\springframework\spring-expression\4.1.4.RELEASE\spring-expression-4.1.4.RELEASE.jar" com.demo.TestHelloWorld
三月 01, 2019 7:30:09 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@197848c: startup date [Fri Mar 01 19:30:09 CST 2019]; root of context hierarchy
三月 01, 2019 7:30:09 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [helloWorldBeans.xml]
Spring say Hello!

Process finished with exit code 0

參考資料

談?wù)剬pring IOC的理解
關(guān)于Spring IOC (DI-依賴注入)你需要知道的一切

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末躺枕,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子供填,更是在濱河造成了極大的恐慌拐云,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捕虽,死亡現(xiàn)場離奇詭異慨丐,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)泄私,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來备闲,“玉大人晌端,你說我怎么就攤上這事√裆埃” “怎么了咧纠?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長泻骤。 經(jīng)常有香客問我漆羔,道長梧奢,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任演痒,我火速辦了婚禮亲轨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鸟顺。我一直安慰自己惦蚊,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布讯嫂。 她就那樣靜靜地躺著蹦锋,像睡著了一般。 火紅的嫁衣襯著肌膚如雪欧芽。 梳的紋絲不亂的頭發(fā)上莉掂,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音千扔,去河邊找鬼巫湘。 笑死,一個(gè)胖子當(dāng)著我的面吹牛昏鹃,可吹牛的內(nèi)容都是我干的尚氛。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼洞渤,長吁一口氣:“原來是場噩夢啊……” “哼阅嘶!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起载迄,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬榮一對情侶失蹤讯柔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后护昧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體魂迄,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年惋耙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了捣炬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绽榛,死狀恐怖湿酸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情灭美,我是刑警寧澤推溃,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站届腐,受9級(jí)特大地震影響铁坎,放射性物質(zhì)發(fā)生泄漏蜂奸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一硬萍、第九天 我趴在偏房一處隱蔽的房頂上張望扩所。 院中可真熱鬧,春花似錦襟铭、人聲如沸碌奉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赐劣。三九已至,卻和暖如春哩都,著一層夾襖步出監(jiān)牢的瞬間魁兼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來泰國打工漠嵌, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留咐汞,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓儒鹿,卻偏偏與公主長得像化撕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子约炎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • 2.1 我們的理念是:讓別人為你服務(wù) IoC是隨著近年來輕量級(jí)容器(Lightweight Container)的...
    好好學(xué)習(xí)Sun閱讀 2,710評(píng)論 0 11
  • 什么是Spring Spring是一個(gè)開源的Java EE開發(fā)框架植阴。Spring框架的核心功能可以應(yīng)用在任何Jav...
    jemmm閱讀 16,461評(píng)論 1 133
  • 來源:關(guān)于Spring IOC (DI-依賴注入)你需要知道的一切作者:zejian Dao層(AccountDa...
    楊井閱讀 5,333評(píng)論 0 27
  • 本來是準(zhǔn)備看一看Spring源碼的。然后在知乎上看到來一個(gè)帖子圾浅,說有一群**自己連Spring官方文檔都沒有完全讀...
    此魚不得水閱讀 6,933評(píng)論 4 21
  • 之前的學(xué)習(xí)掠手,沒有追問自己。 一方面是自己本來就不是很懂狸捕,怕追問太多自己更加混亂 另一方面是自己追問太多喷鸽,自己的成績...
    大海dahai閱讀 136評(píng)論 0 0