這篇文章主要介紹了創建Maven項目和Spring IOC實例過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
把如何創建Maven項目和創建Spring IOC的例子分享給大家,希望能對大家有幫助!
一、創建Maven項目
我用的是Intellij IDEA開發工具創建Maven項目的,打開該軟件后,直接點擊file --->project,如下圖所示,
然后就直接跟著我的圖片的步驟往下走。



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


然后就導入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>

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


創建好Student類后,然后在resources文件夾里面直接創建applicationContext.xml文件,最后在test下的java下創建一個包,在創建一個測試類,具體代碼如下:
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的項目就完成了。

結尾
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。