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. CF960G(第一类斯特林数)

    题目 CF960G 做法 设\(f(i,j)\)为\(i\)个数的序列,有\(j\)个前缀最大值的方案数 我们考虑每次添一个最小数,则有:\(f(i,j)=f(i-1,j)+(i-1)*f(i-1,j ...

  2. CSS3飘带状3D菜单

    在线演示 本地下载

  3. ThinkPHP开发博客系统笔记之二

    1. 登陆验证码 当用户登陆的时候我们希望也弹出验证码,有两种方法可以实现:一是再增加一个弹出验证码的dialog,二是和注册共用一个验证码dialog.第一种方法有大量重复代码,所以我们使用第二种方 ...

  4. mysql分库分表(一)

    mysql分库分表 参考: https://blog.csdn.net/xlgen157387/article/details/53976153 https://blog.csdn.net/cleve ...

  5. tomcat常用配置详解和优化方法

    tomcat常用配置详解和优化方法 参考: http://blog.csdn.net/zj52hm/article/details/51980194 http://blog.csdn.net/wuli ...

  6. Spring与CXF整合

    1.首先引入CXF相关jar包以及spring相关jar包,因项目是maven项目,所以直接在pom.xml文件中引入以下依赖即可(以下只是CXF的依赖包,Spring的也要引入,相关的依赖参考我博客 ...

  7. Android -- junit测试框架,logcat获取log信息

    1. 相关概念 白盒测试: 知道程序源代码. 根据测试的粒度分为不同的类型   方法测试 function test         单元测试 unit test                 集成 ...

  8. [转]HTTP协议通信原理

    本文转自<HTTP协议通信原理> 了解HTTP HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则.计算机专家设计出HTTP,使HTTP客 ...

  9. idea结合git使用

    1.下载安装好git.exe2. 2.安装好以后在cmd输入git 3.在idea配置git环境 4.托管项目到码云 5.登录码云官网,地址就是你在码云注册的账号和密码

  10. 百度之星2017初赛A-1005-今夕何夕

    今夕何夕 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...