1.新建项目

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

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

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

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

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

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.<span style="width: auto; height: auto; float: none;" id="2_nwp"><a target=_blank style="text-decoration: none;" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=211ff60146ba710b&k=spring&k0=spring&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b71ba461f61f21&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D992207%2Ehtml&urlid=0" id="2_nwl"><span style="font-size:12px;color:#0000ff;width:auto;height:auto;float:none;">spring</span></a></span>framework.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.<span style="width: auto; height: auto; float: none;" id="1_nwp"><a target=_blank style="text-decoration: none;" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=211ff60146ba710b&k=spring&k0=spring&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b71ba461f61f21&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D992207%2Ehtml&urlid=0" id="1_nwl"><span style="font-size:12px;color:#0000ff;width:auto;height:auto;float:none;">spring</span></a></span>framework.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 & Spring Testing, Spring AOP简单使用的更多相关文章

  1. Spring框架-IOC和AOP简单总结

    参考博客: https://blog.csdn.net/qq_22583741/article/details/79589910 1.Spring框架是什么,为什么,怎么用 1.1 Spring框架是 ...

  2. Spring源码解析-AOP简单分析

    AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等,不需要去修改业务相关的代码. 对于这部分内容,同样采用一个简单的例子和源码来说明. 接口 public ...

  3. [Spring Unit Testing] Spring Unit Testing with a Java Context

    For example, we want to test against a implemataion: package com.example.in28minutes.basic; import o ...

  4. Spring、Spring Boot和TestNG测试指南 - 使用Spring Boot Testing工具

    Github地址 前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目. 在 ...

  5. Spring、SpringMVC、Spring Boot、Spring Cloud 概念、关系及区别

    注:此文章转载于其他大神 一.正面解读: Spring主要是基于IOC反转Beans管理Bean类,主要依存于SSH框架(Struts+Spring+Hibernate)这个MVC框架,所以定位很明确 ...

  6. JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)

    接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...

  7. spring tx:advice 和 aop:config 配置事务

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. eclipse快速配置spring相关xml文件头信息

    通过spring tools 插件工具来快速配置xml头信息 ctrl +n 创建-----------> 输入spring 选中spring Beann Configuration File ...

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

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

随机推荐

  1. rpm命令,yum命令大全

    rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组合 ...

  2. CSS 一个完整的例子

    My first web page What this is A simple page put together using HTML. I said a simple page put toget ...

  3. FAQ | 是什么导致MySQL数据库服务器磁盘I/O高(本文章来自知数堂)

    FAQ | 是什么导致MySQL数据库服务器磁盘I/O高 2016-12-26 叶金荣 老叶茶馆 0.导读 有个MySQL服务器的磁盘I/O总有过高报警,怎么回事? 本文约1500字,阅读时间约10分 ...

  4. 高亮显示QSS文件

    转[作者:一去丶二三里 博客地址:http://blog.csdn.net/liang19890820] 简述 语法高亮是文本编辑器用来显示文本的,特别是源代码,根据不同的类别来用不同的颜色和字体显示 ...

  5. log4j日志的配置--Debug

    ############################### 日志记录器定义 ################################ 日志输出级别 OFF.DEBUG.INFO.WARN. ...

  6. NOIP 合唱队形

    描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的身高分别为T1,T2,…, ...

  7. What's the difference between UTF-8 and UTF-8 without BOM?

    https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom ...

  8. EF Code-First 学习之旅 EntityTypeConfiguration<TEntity>

    之前我们配置的实体都都在OnModelCreating方法中,如果有很多实体的话,OnModelCreating方法管理很麻烦 我们可以用单独的类来管理配置,继承EntityTypeConfigura ...

  9. 5分钟理解Centos7防火墙firewalld

    版权声明:本内容为原创内容,转载请声明出处. 原文地址:http://www.excelib.com/article/287/show firewalld简介 Centos7中默认将原来的防火墙ipt ...

  10. cmake的使用方法

    4. CMakeLists.txt剖析4.1 cmake_minimum_required命令 cmake_minimum_required (VERSION 2.6) 规定cmake程序的最低版本. ...