Spring学习随笔(2):Eclipse下Spring环境配置+入门项目

1 准备工作

(按需下载)

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环境配置+入门项目

时间: 2024-08-26 22:49:20

Spring学习随笔(2):Eclipse下Spring环境配置+入门项目的相关文章

Hadoop2.x eclipse下编程环境配置

这个过程,是针对如何在eclipse中编写hadoop2.x对应的MapReduce程序,然后打包并上传到hadoop集群执行的过程. 1. 下载hadoop2.x的tar包,解压到某个目录下(注: 不是源码包,是安装包) 2. 将hadoop2x-eclipse插件导入eclipse: (1) 编译插件.插件源码地址 https://github.com/winghc/hadoop2x-eclipse-plugin (2) 把编译后的hadoop2x-eclipse*.jar包拷贝到eclip

junit --Eclipse下的环境搭建

大体的步骤 创建project 添加junit包 创建一个待测试的类JDemo.java 为JDemo.java创建一个Junit测试用例JDemoTest.java 运行测试 1. 创建一个project  :[new]->[project]   (以下截图是操作过程的主要步骤,并非所有) 2. 然后在project的名字上,右键,打开属性窗口,通过[Java build path]->[library]->[Add library]添加junit的包. 添加的时候,我们选择junit

Spring学习系列(四)——Spring中的国际化

1.Java中的国际化国际化(Internationalization)简称 i18n,简单来说就是不同国家/地区/语言的用户,访问同一个程序,得到对应的本地化资源. 首先要确定国家/地区/语言(Locale类) 然后根据不同的Locale获取对应的本地化资源(locale敏感的). 而本地化资源相关的主要有两种:ResourceBundle和Format抽象类的子类.①Jdk为ResourceBundle抽象类提供了两个子类(ListResourceBundle和PropertyResourc

Mac OS10.9.2下的eclipse C++开发环境配置

需要的软件: 1.eclipse C/C++版本 2.Xcode 3.gdb 1.安装eclipse任意一个C/C++版本 2.安装Xcode,Xcode中有C++的编译器 3.安装gdb 官网下载地址:ftp://sourceware.org/pub/gdb/releases/ 下载之后运行打开Terminal的gdb的解压后的目录下,输入以下命令安装: ./Configure make make install 4.因为直接用gdb调试的话,会出现没有权限的情况,因此需要制作一个keycha

(转)Maven学习总结(七)——eclipse中使用Maven创建Web项目

孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(七)——eclipse中使用Maven创建Web项目 一.创建Web项目 1.1 选择建立Maven Project 选择File -> New ->Project,如下图所示: 在New窗口中选择 Maven -> Maven Project.点击[next]如下图所示: 1.2 选择项目路径 根据项目的实际情况选择项目的存放目录,也可以选择[Use default Workspace location]默认工作空间.如下图

Ant学习-001-ant 基础知识及windows环境配置

一.Ant 概要基础知识 Apache Ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发,用以构建应用,或结合其他开源测试工具例如 git.TestNG等搭建持续集成测试开发环境,从而高效.高质量的构建产品. Ant 有以下一些优点: 跨平台,可移植.Ant 是 Java 语言编写的,所以有很好的跨平台性和可移植性,无论是在 windows.Linux,还是 mac. 操作简单. Ant 是由一个内置任务和可选任务组成的,运行时需要一个构

(转载)关于ubuntu下java环境配置及其运行

转载地址:http://blog.sina.com.cn/s/blog_6730a3aa0100vsiy.html 其实不需要怎么配置,直接到ubuntu软件中心里面搜jre就行了,安装上. 在终端输入:sudo apt-get install openjdk-6-jdk 再在ubuntu软件中心里面搜索eclipse下载安装即可. 不安装eclipse也行,不过运行什么的都要在终端了..... 下面就运行试试: 1.直接终端运行: 在桌面建立一个文本文档命名为test.java,使用其他编辑器

Eclipse下安装及配置maven项目管理工具

①eclipse下maven插件安装. 本地maven安装.环境变量配置完成后,打开eclipse,点击eclipse菜单栏Help->Eclipse Marketplace搜索关键字maven,找到插件Maven Integration for Eclipse 并点击安装即可.安装完成后需要重启eclipse. ②eclipse下maven插件配置. 1.依次执行Window -> Preferences -> Maven -> Installation -> Add 进行

ubuntu14.04下php环境配置过程

ubuntu14.04下php环境配置过程 这几天重装了下系统,需要重新配置PHP的开发环境,这里做下记录. 安装MySQL sudo apt-get install mysql-server mysql-client 安装过程中会要求输入数据库root账户的密码,输入密码即可.安装完成后,测试是否成功 mysql -u root -p 出现如下欢迎信息证明安装成功. 安装Apache服务器 sudo apt-get install apache2 安装完成,在浏览器里输入 http://loc