怎么写Spring程序

本篇内容主要讲解“怎么写Spring程序”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么写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>
  • 在项目的 src 目录下创建一个名为 javapub.rodert 的包,然后在该包中创建一个名为 PersonDao 的接口,并在接口中添加一个 add() 方法
package javapub.rodert;

/**
 * @author wangshiyu rodert
 * @date 2020/7/2 20:13
 * @description
 */
public interface PersonDao {
    public void add();
}
  • 创建接口实现类 PersonDaoImpl

javapub.rodert 包下创建 PersonDao 的实现类 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("执行成功!!!");
    }
}
  • 创建 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>
  • 到现在一个 Spring 程序已经搭建完成,测试一下

新建测试类

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();
    }

}

返回结果:

执行成功!!!

使用 JUnit 测试运行测试方法,运行成功。在程序执行时,对象的创建并不是通过 new 一个类完成的,而是通过 Spring 容器管理实现的。这就是 Spring IoC(控制反转) 容器思想的工作机制。

指 IoC 容器使用 setter 方法注入被依赖的实例。通过调用无参构造器或无参 static 工厂方法实例化 bean 后,调用该 bean 的 setter 方法,即可实现基于 setter 的 DI。

指 IoC 容器使用构造方法注入被依赖的实例。基于构造器的 DI 通过调用带参数的构造方法实现,每个参数代表一个依赖。

到此,相信大家对“怎么写Spring程序”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!