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. Node单线程与异步编程的初步理解

    最近学习了一些关于node的单线程与异步的知识,想拿过来和大家分享下: var async = require('async') //并行无关联,等待事件为最长时间请求过程.如以下两个任务执行时间 c ...

  2. Eclipse调试C++(Cocos2dx Android )

    原文链接: http://www.cnblogs.com/zouzf/p/4202537.html 先说windows下的,mac下的在最后 环境:win8.1.java 1.5.Eclipse 4. ...

  3. MySql判断汉字、日期、数字的具体函数

    几个平常用的mysql函数,MySql判断汉字.日期.数字的具体函数分享给大家,具体内容如下 1.判断字符串是否为汉字 返回值:1-汉字 0-非汉字 ? 1 2 3 4 5 6 7 8 9 10 11 ...

  4. Java值传递还是引用传递?

    回顾: 在程序设计语言中,将参数传递分为按值调用和按引用调用.按值调用:表示方法接收的是调用者提供的值.而按引用调用表示方法接收的是调用者提供的变量地址.一个方法可以修改传递引用所对应的变量值,而不能 ...

  5. Kubernetes学习整理

    修改镜像仓库 官方提供的时google源,显然是无法使用的.这里需要改成国内的源 cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kube ...

  6. matplotlib之散点图

    环境:windows系统,anaconda3 64位,python 3.6 1.初认识 基本代码如下: import numpy as np import matplotlib.pyplot as p ...

  7. Kubernetes源码之旅:从kubectl到API Server

    概述: Kubernetes项目目前依然延续着之前爆炸式的扩张.急需能够理解Kubernetes原理并且贡献代码的软件开发者.学习Kubernetes源码并不容易.Kubernetes是使用相对年轻的 ...

  8. 图示:DOM元素各种位置属性

  9. VirtualizingPanel.IsVirtualizing 附加属性

    VirtualizingPanel.IsVirtualizing 附加属性 获取或设置一个值,该值指示此 VirtualizingPanel 正在虚拟化其子集合. 说明: 标准布局系统可以创建项容器并 ...

  10. 案例学习:留言本制作V1、V2

    需求V1: 请设计一个留言本功能的程序,包括普通用户发布和查看留言,管理员登录.查看和删除留言. 实验指导内容详见留言本制作(1) 需求V2: 1.请修改留言本程序的功能,添加普通用户添加登录功能,用 ...