美文网首页程序员ssh
IDEA 使用Maven从零构建Spring基础项目

IDEA 使用Maven从零构建Spring基础项目

作者: 谢随安 | 来源:发表于2017-11-03 10:31 被阅读160次

IDEA和Spring的兼容器是相当之好的,如果使用最新的Spring构建技术大可以直接使用Spring Initializer构建Maven Spring项目。

但是作为一个菜鸡,又只认识列表里的几个包,我要实现个依赖注入应该在Sprign Initializer的组件列表里选啥?

但是Spring Initializer生成的项目已经是SpringBoot项目了,实习公司使用的技术是Spring,并不是SpringBoot,这就很蓝瘦了。

所以需要掌握一下怎么从零构建一个Spring项目,添加项目依赖。

这篇文章的操作不讲原理,对应的原理在另一篇文章【Spring实战】装配Bean讲的很详细。


  1. 创建一个空Maven项目

新建一个项目,在左边菜单栏选择Maven

创建Maven项目

后来的很简单就不啰嗦了。artifact就是项目名,groupId我这里写的是soundsystem(也就是包名)。

  1. 配置pom.xml

创建完项目后就能看到一个pom.xm文件,最开始长这样:

<?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>soundsystem\</groupId>
    <artifactId>LoadBean</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

问题来了,这个问题真是让我懵逼良久,我写个Spring的项目需要在依赖上啥呢?学Spring它会告诉你基础包有一大堆,你根据需要把包放到 lib文件夹里就好了。但是我实现个Spring最简单的提供依赖注入什么依赖呢?找了找也不知道最小依赖是哪个。Spring-Core包尝试了下好像也不对。

假设我要在src/main/java/soundsystem/下实现创建一个接口和一个实现类:

package soundsystem;

public interface CompactDisc {
  void play();
}
package soundsystem;
import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc {

  private String title = "Sgt. Pepper's Lonely Hearts Club Band";  
  private String artist = "The Beatles";
  
  public void play() {
    System.out.println("Playing " + title + " by " + artist);
  }
  
}

这个org.springframework.steroetype是啥包?

查了查试了几下,发现实现依赖注入功能的基础包在一个Spring Context包里。

修改一下pom.xml文件:

<?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>soundsystem\</groupId>
    <artifactId>LoadBean</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
    </dependencies>

</project>

然后就能正常导入包了。

组件默认是不启用的,所以需要通过JavaConfig或者XML启用组件扫描。

这边使用的是XML来启用组件扫描,在 src/main/resources/META-INFO/spring/下创建一个soundsystem.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"
       xmlns:Context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <Context:component-scan base-package="soundsystem" />
</beans>

这样组件扫描就启用了。

  1. 编写测试代码。

创建一个简单的JUnit测试,它会创建Spring上下文,并判断CompactDisc是否成功创建出来:

package soundsystem;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:META-INFO/spring/soundsystem.xml")
public class CDPlayerTest {
    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull(){
        assertNotNull(cd);
    }
}

import包的时候发现没有这些测试依赖包,继续找包,现在需要再在pom.xml文件里添加两个依赖:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.12.RELEASE</version>
            <scope>test</scope>
        </dependency>

运行测试代码,运行成功:

运行结果

我成为了一个可以从零开始用maven写DEMO的小垃圾。

相关文章

网友评论

    本文标题:IDEA 使用Maven从零构建Spring基础项目

    本文链接:https://www.haomeiwen.com/subject/avazpxtx.html