創(chuàng)建Maven項(xiàng)目和Spring IOC實(shí)例過(guò)程解析
這篇文章主要介紹了創(chuàng)建Maven項(xiàng)目和Spring IOC實(shí)例過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(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è)無(wú)參數(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í)有所幫助,也希望大家多多支持腳本之家。
- Spring IOC和DI實(shí)現(xiàn)原理及實(shí)例解析
- SpringIOC DI循環(huán)依賴實(shí)例詳解
- Spring IOC和aop的原理及實(shí)例詳解
- Spring為IOC容器注入Bean的五種方式詳解
- 簡(jiǎn)單了解SPRINGIOC的底層原理演變過(guò)程
- 關(guān)于SpringBoot獲取IOC容器中注入的Bean(推薦)
- 淺談Spring IoC容器的依賴注入原理
- 淺談spring ioc的注入方式及注入不同的數(shù)據(jù)類型
- 簡(jiǎn)單實(shí)現(xiàn)Spring的IOC原理詳解
- Spring IOC原理詳解
- 模仿Spring手寫一個(gè)簡(jiǎn)易的IOC
相關(guān)文章
手寫java性能測(cè)試框架的實(shí)現(xiàn)示例
這篇文章主要為大家介紹了java實(shí)現(xiàn)性能測(cè)試框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07詳解springMVC之與json數(shù)據(jù)交互方法
本篇文章主要介紹了詳解springMVC之與json數(shù)據(jù)交互方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05java中的xxxable和xxxator使用及說(shuō)明
這篇文章主要介紹了java中的xxxable和xxxator使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸
這篇文章主要介紹了SpringSceurity實(shí)現(xiàn)短信驗(yàn)證碼登陸,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06詳解Java的Spring框架下bean的自動(dòng)裝載方式
這篇文章主要介紹了Java的Spring框架下bean的自動(dòng)裝載方式,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法
這篇文章主要介紹了idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05