###Spring容器快速入門案例:

1 創(chuàng)建maven web工程

(勾選Create a simple project) ==> next ==> 配置Group id 和Artifact ID

打包方式用war

解決web工程的兩個問題:

  1. jdk版本過低問題:

解決辦法:在pox.xml中添加jdk編譯插件即可;

<build>  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <configuration>  
                <source>1.8</source>
                <target>1.8</target>  
            </configuration>  
        </plugin>  
    </plugins>  
</build>

2 .丟失web.xml的問題;

解決方案是:創(chuàng)建一個web.xml文件即可眯搭;內(nèi)容如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>01_helloWorld</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

然后做一下maven更新操作

3 導(dǎo)入依賴的jar包

在pom.xml中添加我們的core container核心依賴

<dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
         <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.2.4.RELEASE</version>
    </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
    </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
    </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.4.RELEASE</version>
    </dependency>
</dependencies>

所有依賴的jar包如下:

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.4.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

IOC與DI的快速入門

創(chuàng)建一個UserDao的接口(interface)

public interface UserDao {

    public void sayHello();
}

創(chuàng)建一個UserDaoImpl的實現(xiàn)類:

public class UserDaoImpl implements UserDao {
    private String userName;    
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    @Override
    public void sayHello() {
        System.out.println("hello  world");
    }
}

配置Spring容器

創(chuàng)建一個applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <!-- bean definitions here -->
    <bean id="userDao" class="com.itcast.demo.UserDaoImpl">
        <property name="userName" value="張三"></property>
    </bean>
    
</beans>

注意配置schema:配置方法:
拷貝 http://www.springframework.org/schema/beans/spring-beans.xsd 在eclipes的偏好設(shè)置操作错负;

創(chuàng)建測試類:

    @Test
    public void textName() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao bean = (UserDao) context.getBean("userDao");
        bean.sayHello();
    }

測試結(jié)果成功啦

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市操禀,隨后出現(xiàn)的幾起案子褂策,更是在濱河造成了極大的恐慌,老刑警劉巖颓屑,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件斤寂,死亡現(xiàn)場離奇詭異,居然都是意外死亡揪惦,警方通過查閱死者的電腦和手機遍搞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來器腋,“玉大人溪猿,你說我怎么就攤上這事∪宜” “怎么了诊县?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長措左。 經(jīng)常有香客問我依痊,道長,這世上最難降的妖魔是什么怎披? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任胸嘁,我火速辦了婚禮,結(jié)果婚禮上钳枕,老公的妹妹穿的比我還像新娘缴渊。我一直安慰自己,他們只是感情好鱼炒,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布衔沼。 她就那樣靜靜地躺著,像睡著了一般昔瞧。 火紅的嫁衣襯著肌膚如雪指蚁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天自晰,我揣著相機與錄音凝化,去河邊找鬼。 笑死酬荞,一個胖子當(dāng)著我的面吹牛搓劫,可吹牛的內(nèi)容都是我干的瞧哟。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼枪向,長吁一口氣:“原來是場噩夢啊……” “哼勤揩!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起秘蛔,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤陨亡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后深员,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體负蠕,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年倦畅,在試婚紗的時候發(fā)現(xiàn)自己被綠了遮糖。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡滔迈,死狀恐怖止吁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情燎悍,我是刑警寧澤敬惦,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站谈山,受9級特大地震影響俄删,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜奏路,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一畴椰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鸽粉,春花似錦斜脂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至儡首,卻和暖如春片任,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蔬胯。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工对供, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人氛濒。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓产场,卻偏偏與公主長得像鹅髓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子京景,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

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