Welcome everyone

Spring实践 环境搭建

java 汪明鑫 815浏览 0评论

开发环境

一、Spring Boot项目

直接创建Spring Boot项目即可,

Spring Boot就是基于Spring的,是快速使用Spring的一种手段

详见下面的文章

 

二、Spring项目

创建maven项目

点击File -project Structure

在Module选项中 右边的mian下面

1)新建文件夹java,并且改成Source类型

2)新建文件夹resources,并且改成Resources类型

 

 

引入maven依赖:

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>pers.wmx</groupId>
  <artifactId>spring-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>spring-demo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>

    <!-- spring版本号 -->
    <spring.version>4.3.7.RELEASE</spring.version>
    <!-- mybatis版本号 -->
    <mybatis.version>3.2.6</mybatis.version>
    <!-- log4j日志文件管理包版本 -->
    <slf4j.version>1.7.7</slf4j.version>
    <log4j.version>1.2.17</log4j.version>

  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring依赖 -->
    <!-- 1.Spring核心依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- 2.Spring dao依赖 -->
    <!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- 3.Spring web依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- 4.Spring test依赖:方便做单元测试和集成测试 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>spring-demo</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

 

创建Spring 配置文件

 

xml方式配置bean:

 <bean id="book" class="Book"></bean>

 

/**
 * @author wmx
 * @date 2019-10-22
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Book book = applicationContext.getBean("book", Book.class);
        System.out.println(book);
    }
}

 

测试下没问题,说明Spring开发环境搭好

 

 

也可以配置组件扫描

<!-- 配置bean组件扫描 此时会自动扫描base-package包以及子包下面含有@Component注解的bean,给spring容器管理-->
 <context:component-scan base-package="pers.wmx"/>

 

这样就不用每次都配<bean>

/**
 * @author wmx
 * @date 2019-10-22
 */
@Component("bookService")
public class BookService {
    @Autowired
    BookDao bookDao;

    public Book findBook(Integer id){
        Book book = bookDao.findBook(id);
        return book;
    }

}
/**
 * @author wmx
 * @date 2019-10-22
 */
@Component
public class BookDao {

    public Book findBook(Integer id){
        Book book = new Book();
        book.setBookName("Spring实战");
        return book;
    }

}

 

测试:

/**
 * @author wmx
 * @date 2019-10-22
 */
public class Main {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = applicationContext.getBean("bookService", BookService.class);
        System.out.println(bookService.findBook(1).getBookName());


    }
}

 

 

源码环境

想之后好好读一波源码

在本地弄好源码环境,以后自由添加注释,自由提交更方便

在开发环境下引入maven依赖,确实可以看到Spring源码,但是禁止修改

 

从官网 https://github.com/spring-projects/spring-framework

fork到自己仓库

down下来

 

安装gradle(我的环境:mac)
brew install gradle

 

表面gradle安装成功

 

用idea打开项目

看到gradle正在下载一些东西

等他一波

 

这时说明好了

下一步进入idea terminal

输入  ./gradlew :spring-oxm:compileTestJava 进行编译

 

现在舒服了,你有一个可调试、可修改的Spring源码环境

话不多说,先拉一个分支再说

Spring的源码之路太过艰难,我寻思在学习其他技术过程中穿插一些对Spring源码的学习

end…

 

转载请注明:汪明鑫的个人博客 » Spring实践 环境搭建

喜欢 (2)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz