Spring学习随笔(2):Eclipse下Spring环境配置+入门项目
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring学习随笔(2):Eclipse下Spring环境配置+入门项目,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2895字,纯文字阅读大概需要5分钟。
内容图文
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 } 11publicvoid play(){ 12 ins.play(); 13 } 14} 15class Record{ 16private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 17publicvoid starttime(){ 18 System.out.println(df.format(new Date())); 19 } 20publicvoid endtime(){ 21 System.out.println(df.format(new Date())); 22 } 23} 24class Violin extends Instrument { 2526publicvoid play() { 27 System.out.println("Violin music!"); 28 } 29} 30class Instrument { 31void 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的三个包,已在开头给出。
原文:http://www.cnblogs.com/zmmi/p/7944283.html
内容总结
以上是互联网集市为您收集整理的Spring学习随笔(2):Eclipse下Spring环境配置+入门项目全部内容,希望文章能够帮你解决Spring学习随笔(2):Eclipse下Spring环境配置+入门项目所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。