如何創建Maven項目和Spring IOC例子


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

     我的博客地址:https://www.cnblogs.com/themysteryofhackers/p/12006785.html

     更新時間:2019-12-08

 

一、創建Maven項目

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

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

1575796209(1)

1575796502(1)

1575796608(1)

    

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

 

1575796656(1)

 

1575796755(1)

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

1575797457(1)

 

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

 

1575797707(1)

1575797858(1)

 

      創建好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的項目就完成了。

 

1575801770(1)

 

結尾

     我是一個Java程序員,一個向往技術的小白,以后我會陸續將自己學習到的Java或者其他的知識會以博客的形式分享出來,希望能對大家有幫助。

     喜歡小編的就給我一個關注吧!

     如果有哪些問題、有哪些不妥或者侵犯到您的權益的地方,可以聯系我,我馬上修改。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM