新建一個最簡單的spring應用

確保安裝了eclipse,另外有web server如tomcat或jetty等

1.新建一個web項目,名字隨意取

2.導入所需jar包


另外還需  commons-logging 這個jar包,若用日誌的話導入log4j(若使用需要log4j.properties這文件)

3.創建applicationcontext.xml文件  位置隨意放 我放在src目錄下

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="hello" class="com.liu.pojo.Hello" >
<property name="message" value=" world"></property>     <!--  這個是通過set方法注入的 確保有setter方法以及一個空的構造函數 -->
</bean>
</beans>

4.創建一個pojo類 也就是普通的javabean類 

public class Hello implements HelloImpl{
private String message;

public Hello(){

}
public Hello(String message){
this.message=message;
}
    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }
    public String excute(){
        return "Hello"+getMessage();
    }
}

5.創建一個測試類  

public class test {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
HelloImpl hello = (Hello) ctx.getBean("hello");
System.out.println(hello.excute());

}
}

6.輸出hello world即成功

當然這只是個超級簡單的入門例子 僅僅體現了通過xml注入簡單屬性這個特性  

繼續學習!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章