Spring学习随笔(2):Eclipse下Spring环境配置+入门项目
2024-08-27 12:42:39
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
package com.spring; import java.text.SimpleDateFormat;
import java.util.Date; public class Performer {
private Instrument ins;
public Performer(Instrument ins){
this.ins=ins; //与Violin紧密耦合
}
public void play(){
ins.play();
}
}
class Record{
private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void starttime(){
System.out.println(df.format(new Date()));
}
public void endtime(){
System.out.println(df.format(new Date()));
}
}
class Violin extends Instrument { public void play() {
System.out.println("Violin music!");
}
}
class Instrument {
void play(){};
}
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的三个包,已在开头给出。
最新文章
- 【BZOJ】4001: [TJOI2015]概率论
- JavaScript 数组
- C# 转换符的重载 显示、隐式转换
- php正规则表达式的语法
- DEV主从表
- 转:如何学习SQL(第二部分:从关系角度理解SQL)
- 【C#学习笔记】Hello World
- easyui表单提交,后台获取不到值
- Java基础知识强化之集合框架笔记62:Map集合之HashMap嵌套HashMap
- 【转】 ubuntu12.04更新源
- Android L(5.0)源码之手势识别GestureDetector
- 20162302 实验一《Java开发环境的熟悉》实验报告
- 到底创建了几个String对象?
- Linux shell编程 -test
- clean exit - waiting for changes before restart
- LeetCode题解之 Find Mode in Binary Search Tree
- 【MAVEN】如何在Eclipse中创建MAVEN项目
- Kudu:支持快速分析的新型Hadoop存储系统
- windows 下,CCXT库的安装
- physical------Collider 组件参考