溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么寫Spring程序

發(fā)布時(shí)間:2021-11-01 14:50:31 來(lái)源:億速云 閱讀:136 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“怎么寫Spring程序”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“怎么寫Spring程序”吧!

<?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>javapub.rodert</groupId>
    <artifactId>firstSpringProject</artifactId>
    <version>1.0-SNAPSHOT</version>    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

    </dependencies>
</project>
  • 在項(xiàng)目的 src 目錄下創(chuàng)建一個(gè)名為 javapub.rodert 的包,然后在該包中創(chuàng)建一個(gè)名為 PersonDao 的接口,并在接口中添加一個(gè) add() 方法
package javapub.rodert;

/**
 * @author wangshiyu rodert
 * @date 2020/7/2 20:13
 * @description
 */
public interface PersonDao {
    public void add();
}
  • 創(chuàng)建接口實(shí)現(xiàn)類 PersonDaoImpl

javapub.rodert 包下創(chuàng)建 PersonDao 的實(shí)現(xiàn)類 PersonDaoImpl

package javapub.rodert;

/**
 * @author wangshiyu rodert
 * @date 2020/7/2 20:14
 * @description
 */
public class PersonDaoImpl implements PersonDao {
    public void add() {
        System.out.println("執(zhí)行成功?。。?quot;);
    }
}
  • 創(chuàng)建 Spring 配置文件

Spring 配置文件是整合 Spring 的核心

<?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 id="personDao" class="javapub.rodert.PersonDaoImpl"/>

</beans>
  • 到現(xiàn)在一個(gè) Spring 程序已經(jīng)搭建完成,測(cè)試一下

新建測(cè)試類

package javapub.rodert;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author wangshiyu rodert
 * @date 2020/7/2 20:15
 * @description
 */
public class PersonDaoTest {

    @Test
    public void test1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        PersonDao personDao = (PersonDao) applicationContext.getBean("personDao");
        personDao.add();
    }

}

返回結(jié)果:

執(zhí)行成功!?。?br/>

使用 JUnit 測(cè)試運(yùn)行測(cè)試方法,運(yùn)行成功。在程序執(zhí)行時(shí),對(duì)象的創(chuàng)建并不是通過(guò) new 一個(gè)類完成的,而是通過(guò) Spring 容器管理實(shí)現(xiàn)的。這就是 Spring IoC(控制反轉(zhuǎn)) 容器思想的工作機(jī)制。

指 IoC 容器使用 setter 方法注入被依賴的實(shí)例。通過(guò)調(diào)用無(wú)參構(gòu)造器或無(wú)參 static 工廠方法實(shí)例化 bean 后,調(diào)用該 bean 的 setter 方法,即可實(shí)現(xiàn)基于 setter 的 DI。

指 IoC 容器使用構(gòu)造方法注入被依賴的實(shí)例?;跇?gòu)造器的 DI 通過(guò)調(diào)用帶參數(shù)的構(gòu)造方法實(shí)現(xiàn),每個(gè)參數(shù)代表一個(gè)依賴。

到此,相信大家對(duì)“怎么寫Spring程序”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI