spring項目搭建

導包

包的分類
這裏寫圖片描述
在 spring-framework-4.0.0.RELEASE 包內
這裏寫圖片描述
日誌文件
這裏寫圖片描述

書寫配置文件

位置命名隨意,最好放在src下,最好叫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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd ">

<bean name="user" class="bean.User" ></bean>

</beans>

添加約束

選擇 spring-framework-4.0.0.RELEASE\schema\beans 內最新的。
這裏寫圖片描述

添加命名空間
這裏寫圖片描述
這裏寫圖片描述

創建一個對象

package bean;
public class User {
    private String name;
    private Integer age;
    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;
    }
}

代碼測試

package bean;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {
    @Test
    public void fun1(){
        //創建容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //索取user對象
        User u = (User)ac.getBean("user");
        System.out.println(u);
    }
}

輸出user對象

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