最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年12月10日 14:48:40   作者:黑客之謎  
這篇文章主要介紹了創(chuàng)建Maven項(xiàng)目和Spring IOC實(shí)例過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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

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

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

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

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

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

然后就導(dǎo)入Spring IOC的項(xiàng)目依賴,可以去這個(gè)網(wǎng)站查找Maven依賴查找。然后在pom.xml文件先導(dǎo)入下面的依賴。

<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>

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

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

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 ">
  <!-- 把一個(gè)對(duì)象放進(jìn)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="中國(guó)"></property>
  </bean>
  <!-- 把另一個(gè)對(duì)象也放到spring容器中,對(duì)象的名字不能重復(fù),否則運(yùn)行會(huì)報(bào)錯(cuò) -->
  <!-- 用property設(shè)置對(duì)象的屬性,那該對(duì)象要有setter方法,還要有一個(gè)無參數(shù)的構(gòu)造方法 -->
  <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="中國(guó)"></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);
  }
}

最后,直接運(yùn)行程序,這樣一個(gè)簡(jiǎn)單的Spring IOC結(jié)合Maven的項(xiàng)目就完成了。

結(jié)尾

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

红桥区| 通河县| 东海县| 凉城县| 济源市| 洞口县| 西安市| 威宁| 罗源县| 大田县| 泌阳县| 兴国县| 息烽县| 大方县| 百色市| 永泰县| 平凉市| 藁城市| 达日县| 西和县| 象山县| 金堂县| 巴林左旗| 新宁县| 邢台市| 安远县| 湘西| 子洲县| 苏尼特右旗| 尉犁县| 绥芬河市| 昌吉市| 海原县| 启东市| 曲水县| 灵寿县| 清水河县| 金溪县| 芮城县| 庆城县| 九寨沟县|