溫馨提示×

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

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

IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟

發(fā)布時(shí)間:2020-07-03 09:27:12 來(lái)源:億速云 閱讀:1124 作者:清晨 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

第一步:在IDEA點(diǎn)擊new -> project

IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟

左側(cè)選擇Maven,直接點(diǎn)擊Next。第一次使用IDEA的朋友,頂部還要選擇Project SDK路徑,就是Java的安裝路徑。

IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟

這里隨便填一下之后點(diǎn)擊Next

IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟

選擇項(xiàng)目存放路徑,或者保持默認(rèn),點(diǎn)擊Finish,來(lái)到工程頁(yè)面之后,在項(xiàng)目文件夾上右鍵并選擇Add Framework Support

IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟

在這個(gè)頁(yè)面找一下Spring,打鉤。點(diǎn)擊OK

IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟

完成后,會(huì)下載Spring的jar包,并存放在項(xiàng)目的lib目錄下。

下載完成之后創(chuàng)建一個(gè)簡(jiǎn)單的項(xiàng)目。

IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟

四個(gè)Java文件一個(gè)appContext.xml我貼在這里

public class Student {

  private String name;
  private Homework homework;
  public Student(){}
  public Student(String name, Homework homework) {
    this.name = name;
    this.homework = homework;
  }

  public void doHomeWork(){
    System.out.println(homework.getContent());
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Homework getHomework() {
    return homework;
  }

  public void setHomework(Homework homework) {
    this.homework = homework;
  }
}
public class Homework {

  private String content;

  public Homework(){}
  public Homework(String content) {
    this.content = content;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }
}
public class Homework {

  private String content;

  public Homework(){}
  public Homework(String content) {
    this.content = content;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }
}
import java.util.Date;

public class CheckNowTime {

  public void beforDoHomework(){
    System.out.println(new Date(System.currentTimeMillis()));
  }
}
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
    Student student = context.getBean(Student.class);

    System.out.println(student.getName()+"準(zhǔn)備做作業(yè)了");
    student.doHomeWork();

    context.close();

  }
}
<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  <!-- 讓Spring管理 Student bean  -->
  <bean id="student" class="demo1.Student">
    <property name="name" value="小明"></property>
    <property name="homework" ref="homework"></property>
  </bean>

  <!-- 讓Spring管理Homework bean-->
  <bean id="homework" class="demo1.Homework">
    <property name="content" value="how to calc 3+2 &#63;"></property>
  </bean>

  <!-- 切面定義-->
  <bean id="checktime" class="demo1.CheckNowTime"></bean>
  <aop:config>
    <aop:aspect ref="checktime">
      <aop:pointcut id="dohomework" expression="execution(* *.doHomeWork(..))"/>
      <aop:before pointcut-ref="dohomework" method="beforDoHomework"></aop:before>
    </aop:aspect>
  </aop:config>
</beans>

因?yàn)槔又惺褂肧pring AOP,所以還需要引入一個(gè)jar包。我的pom.xml如下:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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.example</groupId>
  <artifactId>test</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>
  </dependencies>

</project>

然后你就可以運(yùn)行自己的Spring項(xiàng)目啦,main方法在App類里面。

以上是IDEA+Maven創(chuàng)建Spring項(xiàng)目的方法步驟的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向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