1.新建项目

2.右击项目,如图,利用myeclipse自己主动导入spring

3.在弹出的对话框中一直next到最后,在最后的页面中勾选Spring Testing,完毕.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

4.在src下的applicationContext.xml里,点击namespaces,勾选aop和context

5.在上图的底部分别进入aop和context界面,

5.1在aop界面右击beans,加入<aop:aspectj-autoproxy>,这个的作用是启用aspectj类型的aop功能.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

5.2 在context界面右击beans,加入<context:component-scan>,并在右边的elemen details识图中填写base-package和选择annotation-config

base-package的作用是,让spring自己主动扫描存在注解的包并注冊为spring beans.(我这里的包均以glut开头)

annotation-config的作用大概是启用注解.(doc里面大致的意思......)

6.创建UserService接口,UserServiceImp实现类还有MyTest測试类.

文件夹结构:

package glut.service;

public interface UserService {
void login(String username);
}
package glut.serviceImp;

import org.springframework.stereotype.Service;

import glut.service.UserService;

@Service
public class UserServiceImp implements UserService { @Override
public void login(String username) {
// TODO Auto-generated method stub
System.out.println(username + " has login");
} }
package glut.test;

import javax.annotation.Resource;

import glut.service.UserService;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //启用Spring JUnit
@RunWith(SpringJUnit4ClassRunner.class)
//载入指定的配置文件
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class MyTest {
@Resource
private UserService userService; @Test
public void test() {
userService.login("leo");
}
}

測试结果:

7.接下来測试AOP功能,新建一个切面类

package glut.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; //声明为切面
@Aspect
//切面也要作为component识别
@Component
public class MyAspect {
//execution(随意类型 glut..随意包.随意类(随意參数)
@Pointcut("execution(* glut..*.*(..))")
public void myPointCut() {
}; //调用poincut指定的方法前运行beforeMethod
@Before("myPointCut()")
public void beforeMethod() {
System.out.println("This is before method");
} //调用poincut指定的方法后运行afterMethod
@After("myPointCut()")
public void afterMethod() {
System.out.println("This is after method");
}
}

最后再看一次測试结果:

MyEclipse2014,你值得拥有.

MyEclipse2014高速配置Spring &amp; Spring Testing, Spring AOP简单使用的更多相关文章

  1. 玩转单元测试之Testing Spring MVC Controllers

    玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...

  2. Spring学习记录(十二)---AOP理解和基于注解配置

    Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...

  3. 配置spring的监听器 让spring随项目的启动而启动

    <!-- 配置spring的监听器 让spring随项目的启动而启动 --> <listener> <listener-class>org.springframew ...

  4. Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)

    参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...

  5. Spring学习 6- Spring MVC (Spring MVC原理及配置详解)

    百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...

  6. Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务

    Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...

  7. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

  8. 关于Spring的xml文档的简单实用配置

    Spring的spring.xml文档的配置 最近在写Spring的配置文件时,发现Spring文档的配置其实没必要那么繁琐记忆,网上的很多文章都写得很繁琐,如果所有的东西按照路径去查找,可以很快的帮 ...

  9. 服务注册发现、配置中心集一体的 Spring Cloud Consul

    前面讲了 Eureka 和 Spring Cloud Config,今天介绍一个全能选手 「Consul」.它是 HashiCorp 公司推出,用于提供服务发现和服务配置的工具.用 go 语言开发,具 ...

随机推荐

  1. 使用 python 读写中文json

    读写中文json )   输出中文的json. 通过使用 ensure_ascii=False,输出原有的语言文字.indent參数是缩进数量. 更改写文件格式 将上一步导出的 string 直接写文 ...

  2. Codeforces 570D TREE REQUESTS dfs序+树状数组

    链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...

  3. PHP 7给我震撼

    看了一些php7(ng)的讨论,目前还没有去下beta版尝试编译. 作为一个phper,一直都有关注php本身语言的发展.以前在jumei工作的时候就听罗sir谈到过php ng,性能将超过faceb ...

  4. Android圆角Tag控件的另类实现

    一般的圆角标签控件都是用xml设置shape做实现.可是假设我们想要做一个更加强大通用的的圆角控件,不须要使用者去关心圆角,仅仅设置背景就能够了. 应该怎么实现呢?这个就须要把背景先设置成图片,然后再 ...

  5. bzoj3998: [TJOI2015]弦论(SAM+dfs)

    3998: [TJOI2015]弦论 题目:传送门 题解: SAM的入门题目(很好的复习了SAM并加强Right集合的使用) 其实对于第K小的字符串直接从root开始一通DFS就好,因为son边是直接 ...

  6. 前后端分离之接口登陆权限token

    随着业务的需求普通的springmvc+jsp已经不能满足我们的系统了,会逐渐把后台和前端展示分离开来,下面我们就来把普通的springmvc+jsp分为 springmvc只提供rest接口,前端用 ...

  7. FFmpeg 移植 Android

    近期项目需要解析苹果的HLS流媒体协议,而FFmpeg从0.11.1“Happiness”版本开始,才增加了对HLS协议的支持.目前网上关于FFmpeg编译移植的文章有很多,但大多都是对旧版本的说明. ...

  8. JQuery 登录窗口的布局

    <!-- Button trigger modal --><button type="button" class="btn btn-primary bt ...

  9. 洛谷P3327 [SDOI2015]约数个数和(莫比乌斯反演)

    题目描述 设d(x)为x的约数个数,给定N.M,求 \sum^N_{i=1}\sum^M_{j=1}d(ij)∑i=1N​∑j=1M​d(ij) 输入输出格式 输入格式: 输入文件包含多组测试数据.第 ...

  10. 用Latex做介绍自己和团队科研的网页

    最近实验室师妹用网上的一些模板改了改做了几个网页.感觉还可以.但是实际上总觉得好像和韩家炜.周志华他们的页面差点什么. 最近找论文时发现奥地利的hornik老先生页面居然latex做的,然后找到了下面 ...