Java中Spring 基本配置學習筆記

來源:互聯網
上載者:User


1.下載 spring 架構 http://www.springsource.org/download/community

2.將spring lib目錄下的這些包拷貝到我們應用的lib中

commons-logging.jar
spring-beans-4.0.0.M1.jar
spring-context-4.0.0.M1.jar
spring-core-4.0.0.M1.jar
spring-expression-4.0.0.M1.jar


3.建立一個 bean 類

 代碼如下 複製代碼

package org.Rudiment.spring;

public class PersonService
{

    private String name;
   
    public void setName(String name)
    {
        this.name = name;
    }
   
    public void info()
    {
        System.out.println("此人的名為" + name);
    }
}


4.建立一個測試的類

 代碼如下 複製代碼

import org.Rudiment.spring.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SpringTest
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonService p = ac.getBean("personService", PersonService.class);
        p.info();
    }

}


5.建立Spring的設定檔(applicationContext.xml),這個檔案放在classes下面,如果在myeclipse中做開發,那直接放在src下面(IDE會幫我們移到classes下面)

 代碼如下 複製代碼

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

        <!-- 配置我們的 PersonService 類 -->
        <bean id="personService" class="org.Rudiment.spring.PersonService">
            <property name="name" value="wawa" />
        </bean>
       
</beans>


至此我們的spring就算配置完成了。
有一個比較難弄的就是。spring 架構中貌似沒有提供設定檔的模版,所以我這個配置文檔也是QQ好友發給我,然後修改的 - -!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.