準備學習sPRing,每天下班學一點
環境準備
eclipse mar 4.5.1:下載地址
Spring STS 4.5.1插件: 下載地址(可以去http://spring.io/tools/sts/all下載最新版本)
maven 3.3.9(由于我準備使用maven創建spring依賴):下載地址
a.添加環境變量 M2_HOME=C:/java/apache-maven-3.3.3
path中追加 %M2_HOME%/bin;
mvn 常用指令:http://www.49028c.com/holly/archive/2013/06/15/3137041.html
修改mvn中央庫為oschina: http://maven.oschina.net/help.html
修改eclipse中mvn設置
a.打開eclipse 新建maven 工程
b.編輯pom.xml文件
打開spring 官網http://projects.spring.io/spring-framework/,找到相關配置,添加到pom.xml中,等待eclipse從oschina下載對應jar和source-jar
下載完成結果
至此,基于maven的spring環境搭建完成
a.介紹工程結構
HelloWord 類:為即將聲明的bean
Main類:為測試方法
applicationContext.xml:spring bean 定義文件
b.編寫相關代碼
HelloWord
public class HelloWord{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void sayHello() { System.out.println("hello:"+name); }}
Application.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 id="baseModel" class="com.liangpeng.spring.beans.HelloWord"> <property name="name" value="Spring"></property> </bean></beans>
Main
public class Main{ public static void main(String[] args) { @SuppressWarnings ("resource") ApplicationContext ioc =new ClassPathXmlApplicationContext("ApplicationContext.xml"); HelloWord hello =ioc.getBean(HelloWord.class); hello.sayHello(); }}
運行Main,會發現spring通過反射在ioc容器中實例化HelloWord,并將hello的name設置為Spring
十一月 18, 2015 8:31:06 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Wed Nov 18 20:31:06 CST 2015]; root of context hierarchy十一月 18, 2015 8:31:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [ApplicationContext.xml]hello:Spring
新聞熱點
疑難解答