欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

創(chuàng)建Maven項目和SpringIOC實例過程解析

這篇文章主要介紹了創(chuàng)建Maven項目和Spring IOC實例過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

創(chuàng)新互聯建站專注于定興網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供定興營銷型網站建設,定興網站制作、定興網頁設計、定興網站官網定制、小程序制作服務,打造定興網絡公司原創(chuàng)品牌,更為您提供定興網站排名全網營銷落地服務。

把如何創(chuàng)建Maven項目和創(chuàng)建Spring IOC的例子分享給大家,希望能對大家有幫助!

一、創(chuàng)建Maven項目

我用的是Intellij IDEA開發(fā)工具創(chuàng)建Maven項目的,打開該軟件后,直接點擊file --->project,如下圖所示,

然后就直接跟著我的圖片的步驟往下走。

創(chuàng)建Maven項目和Spring IOC實例過程解析

創(chuàng)建Maven項目和Spring IOC實例過程解析

創(chuàng)建Maven項目和Spring IOC實例過程解析

到了這一個就創(chuàng)建好了Maven項目了,然后開發(fā)工具會在右下角提示下圖的信息,直接點擊自動導入就好。

創(chuàng)建Maven項目和Spring IOC實例過程解析

創(chuàng)建Maven項目和Spring IOC實例過程解析

然后就導入Spring IOC的項目依賴,可以去這個網站查找Maven依賴查找。然后在pom.xml文件先導入下面的依賴。

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.12.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>

創(chuàng)建Maven項目和Spring IOC實例過程解析

導入依賴后就創(chuàng)建包,創(chuàng)建包是為了更好的去管理Java類,創(chuàng)建好包之后就直接創(chuàng)建類,創(chuàng)建包和類的命名遵從Java命名規(guī)范即可。

創(chuàng)建Maven項目和Spring IOC實例過程解析

創(chuàng)建Maven項目和Spring IOC實例過程解析

創(chuàng)建好Student類后,然后在resources文件夾里面直接創(chuàng)建applicationContext.xml文件,最后在test下的java下創(chuàng)建一個包,在創(chuàng)建一個測試類,具體代碼如下:

Student.java

package com.zzx.entity;

public class Student {
  private Integer id;
  private String name;
  private Integer age;
  private Integer sex;
  private String address;
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getAge() {
    return age;
  }
  public void setAge(Integer age) {
    this.age = age;
  }
  public Integer getSex() {
    return sex;
  }
  public void setSex(Integer sex) {
    this.sex = sex;
  }
  public String getAddress() {
    return address;
  }
  public void setAddress(String address) {
    this.address = address;
  }
  @Override
  public String toString() {
    return "Student{" +
        "id=" + id +
        ", name='" + name + '\'' +
        ", age=" + age +
        ", sex=" + sex +
        ", address='" + address + '\'' +
        '}';
  }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    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 ">
  <!-- 把一個對象放進Spring容器 -->
  <bean name="s1" class="com.zzx.entity.Student">
    <property name="id" value="1"></property>
    <property name="name" value="小紅"></property>
    <property name="age" value="18"></property>
    <property name="sex" value="2"></property>
    <property name="address" value="中國"></property>
  </bean>
  <!-- 把另一個對象也放到spring容器中,對象的名字不能重復,否則運行會報錯 -->
  <!-- 用property設置對象的屬性,那該對象要有setter方法,還要有一個無參數的構造方法 -->
  <bean name="s2" class="com.zzx.entity.Student">
    <property name="id" value="2"></property>
    <property name="name" value="小白"></property>
    <property name="age" value="16"></property>
    <property name="sex" value="1"></property>
    <property name="address" value="中國"></property>
  </bean>
</beans>

Test01.java

package com.zzx.ioc;

import com.zzx.entity.Student;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
  @Test
  public void studentTest(){
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    Student s1 = applicationContext.getBean("s1", Student.class);
    Student s2 = applicationContext.getBean("s2", Student.class);
    System.out.println(s1);
    System.out.println(s2);
  }
}

最后,直接運行程序,這樣一個簡單的Spring IOC結合Maven的項目就完成了。

創(chuàng)建Maven項目和Spring IOC實例過程解析

結尾

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯。

文章名稱:創(chuàng)建Maven項目和SpringIOC實例過程解析
當前路徑:http://chinadenli.net/article16/geoegg.html

成都網站建設公司_創(chuàng)新互聯,為您提供ChatGPT品牌網站設計建站公司做網站企業(yè)網站制作微信公眾號

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

手機網站建設