spring學(xué)習(xí)之創(chuàng)建項目 Hello Spring實例代碼
本文研究的主要是spring學(xué)習(xí)之創(chuàng)建項目 Hello Spring實例代碼,具體如下。
一、創(chuàng)建eclipse項目,引入jar包
1、eclipse創(chuàng)建java project項目 HelloSpring
2、創(chuàng)建lib目錄,加入spring必須的5個jar包
3、選中5個文件,右鍵 -> Build Path -> add to build path
二、編寫spring的hello spring代碼
1、創(chuàng)建包io.spring.beans,并編寫HelloWorld.java
package io.spring.beans;
/**
* @author 胖胖のALEX E-mail:zanbin168@qq.com
* @version 1.0
*/
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.println("hello " + name);
}
}
2、src右鍵 -> 創(chuàng)建spring bean configuration文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="helloWorld" class="io.spring.beans.HelloWorld">
<property name="name" value="大紅"></property>
</bean>
</beans>
3、編寫Main.java
package io.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 胖胖のALEX E-mail:zanbin168@qq.com
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
//1、創(chuàng)建Spring的IOC容器對象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、從IOC容器中獲取Bean實例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//3、調(diào)用hello方法
helloWorld.hello();
}
}
輸出結(jié)果
當(dāng)console內(nèi)打印出紅色spring日志,表示spring應(yīng)用成功
總結(jié)
以上就是本文關(guān)于spring學(xué)習(xí)之創(chuàng)建項目 Hello Spring實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Springboot獲取前端反饋信息并存入數(shù)據(jù)庫的實現(xiàn)代碼
這篇文章主要介紹了Springboot獲取前端反饋信息并存入數(shù)據(jù)庫的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Mybatis-Plus實現(xiàn)自定義SQL具體方法
Mybatis-Plus是Mybatis的一個增強(qiáng)工具,它可以優(yōu)化我們的開發(fā)效率,這篇文章主要介紹了Mybatis-Plus實現(xiàn)自定義SQL,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08

