Spring學習隨筆(2):Eclipse下Spring環境配置+入門項目

來源:互聯網
上載者:User

標籤:sim   col   ica   generate   exe   class   配置   mat   contex   

1 準備工作

(按需下載)

  • Eclipse 下載:http://www.eclipse.org/downloads/eclipse-packages/ ;
  • Spring 下載:http://repo.spring.io/libs-release-local/org/springframework/spring/  版本自選;
  • Spring aop另需的3個包:http://download.csdn.net/download/lk_lxn/6397895 

2 Spring IDE

help->Eclipse Marketplace->Find"Spring"->install

選擇需要的項目安裝,重啟Eclipse.

安裝完記得重啟

3 匯入所需的包

方法有多種,我貼下我的方法。

項目右鍵->build path->configure build path->Libraries->add Library->User Library->User Librarys

->New->輸入Library name->add External JARs->選擇需要的包

->在add Library處勾選剛才的Lib->finish->ok;

我這種辦法好像比較麻煩.......

 

 

 

4 入門項目

完整目錄

 

Performer.java

 1 package com.spring; 2  3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5  6 public class Performer { 7     private Instrument ins; 8     public Performer(Instrument ins){ 9         this.ins=ins;            //與Violin緊密耦合10     }11     public void play(){12         ins.play();13     }14 }15 class Record{16     private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");17     public void starttime(){18         System.out.println(df.format(new Date()));19     }20     public void endtime(){21         System.out.println(df.format(new Date()));22     }23 }24 class Violin extends Instrument {25 26     public void play() {27         System.out.println("Violin music!");28     }29 }30 class Instrument {31     void play(){};32 }

PerformerMain.java

 

package com.spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class PerformerMain {    public static void main(String[] args) {        // TODO Auto-generated method stub        ApplicationContext apc = new ClassPathXmlApplicationContext("spring.xml");          Performer hello = (Performer) apc.getBean("performer");          hello.play();    }}

 

Spring.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd                           http://www.springframework.org/schema/aop                           http://www.springframework.org/schema/aop/spring-aop.xsd">    <bean id="performer" class="com.spring.Performer">        <constructor-arg ref="violin" />    </bean>    <bean id="violin" class="com.spring.Violin"></bean>    <bean id="record" class="com.spring.Record"></bean>        <aop:config>        <aop:aspect ref="record">            <aop:pointcut expression="execution(* com.spring.Performer.play(..))"  id="play"/>            <aop:before method="starttime" pointcut-ref="play"/>            <aop:after method="endtime" pointcut-ref="play"/>        </aop:aspect>    </aop:config></beans>   

運行結果:

 

 

注意事項:

 xml檔案中使用aop標籤引入標頭檔配置

報錯:

需要AspectJ的三個包,已在開頭給出。

 

Spring學習隨筆(2):Eclipse下Spring環境配置+入門項目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.