在IntelliJ IDEA中Spring項目的入門

在此篇博客中,我將簡單的使用IDEA創建簡單的maven項目用於實現spring環境的搭建和一個<bean>對於我們spring的測試

好的,現在開始我們的spring之旅。(*^▽^*)

1.新建項目

file-->new-->project..

填寫groupid和artifictId

maven的使用,可以查看我的文章或者去網絡搜索。。。(學習總是在不斷Google中的)

點擊finish,完成項目創建。如果之前你的maven倉庫沒有下載好maven的依賴包。這個時候要等待maven自動下載好。

這可能需要一定的時間,具體視你的網絡情況,或者百度(Google)maven的使用

創建好的項目一般項目結構缺少。

2.spring依賴

首先,進入pom.xml文件。添加spring所需的依賴

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>com.project</groupId>
  <artifactId>spring</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spring</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.version>4.3.18.RELEASE</spring.version>
  </properties>

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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

  </dependencies>

  <build>
    <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.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.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>

 

3.配置文件

然後,在src下新建resources文件夾(用於存放配置資源文件)。一般創建好的文件夾只是普通的文件夾,這裏我們要將其轉變爲資源文件夾

在resources文件夾下創建application.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">

</beans>

4.編寫代碼測試

在com.project包下新建service包,在新建一個service接口爲IService.java

package com.project.service;

public interface IService {
    //定義一個方法
    void doSome();
}

再書寫一個IServiceImpl.java實現IService接口

package com.project.service.serviceImpl;

import com.project.service.IService;

public class IServiceImpl implements IService {
    //實現方法
    @Override
    public void doSome() {
        System.out.println("do some......");
    }
}

修改application.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 id="IService" class="com.project.service.serviceImpl.IServiceImpl"/>
</beans>

添加了

<bean id="IService" class="com.project.service.serviceImpl.IServiceImpl"/>

其中<bean/>表示一個實體類,id表示類名,class表示該類的具體java文件,因爲spring是一個容器,在配置文件中加入<bean/>,則在項目部署的時候,spring會將該類放入容器中,自動生成對象。

 

創建測試類,用於測試我們的方法

package com.project;

import com.project.service.IService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @org.junit.Test
    public void test() {
    //讀取配置文件,加載裏面的配置等
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
    //通過getBean得到對象,其中IService就是我們剛剛在application.xml中配置的<bean/>中的id
        IService iService = (IService) applicationContext.getBean("IService");
    //得到對象,便執行其方法
        iService.doSome();
    }
}

查看控制檯

好的,到這裏大功告成!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章