MyEclipse2014高速配置Spring & Spring Testing, Spring AOP简单使用
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 & Spring Testing, Spring AOP简单使用的更多相关文章
- 玩转单元测试之Testing Spring MVC Controllers
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...
- Spring学习记录(十二)---AOP理解和基于注解配置
Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...
- 配置spring的监听器 让spring随项目的启动而启动
<!-- 配置spring的监听器 让spring随项目的启动而启动 --> <listener> <listener-class>org.springframew ...
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...
- Spring学习 6- Spring MVC (Spring MVC原理及配置详解)
百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...
- Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务
Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...
- 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用
2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...
- 关于Spring的xml文档的简单实用配置
Spring的spring.xml文档的配置 最近在写Spring的配置文件时,发现Spring文档的配置其实没必要那么繁琐记忆,网上的很多文章都写得很繁琐,如果所有的东西按照路径去查找,可以很快的帮 ...
- 服务注册发现、配置中心集一体的 Spring Cloud Consul
前面讲了 Eureka 和 Spring Cloud Config,今天介绍一个全能选手 「Consul」.它是 HashiCorp 公司推出,用于提供服务发现和服务配置的工具.用 go 语言开发,具 ...
随机推荐
- 深入理解 C 指针阅读笔记 -- 第五章
Chapter5.h #ifndef __CHAPTER_5_ #define __CHAPTER_5_ /*<深入理解C指针>学习笔记 -- 第五章*/ /*不应该改动的字符串就应该用 ...
- 8.QList QMap QVariant
QList int main1(int argc, char *argv[]) { QApplication a(argc, argv); QList<,,}; mylist << ...
- C#调用mmpeg进行各种视频转换的类实例
本文实例讲述了C#调用mmpeg进行各种视频转换的类.分享给大家供大家参考.具体如下: 这个C#类封装了视频转换所需的各种方法,基本上是围绕着如何通过mmpeg工具来进行视频转换 using Syst ...
- Asp.net Web Api中使用配置Unity
第一步:建立web api,添加unity.webapi. 第二步:在添加了该引用之后,在App_Start中会自动生成UnityConfig.cs文件 第三步:添加数据做测试 第四步:展示效果
- php链接memcache操作
设置值 set key 压缩标识 有效期 长度 set name 0 60 5 hello 压缩标识:用于告诉memcached服务器是否压所后存储数据,目的是为了节省磁盘空间,压所和解压缩会消耗时间 ...
- Java 多线程(二)synchronized和volatile
脏读: 脏读指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个事务也访问这个数据,然后使用了这个数据.总的来说取到的数据是其实是被更改过的,但还没有保存到数 ...
- solarwind之network Atlas
1. 连接密码为空,连接到Orion 2. 连接后如下图 3. 直接拖动节点即可进行绘制地图 4. 查看它的相关属性
- Java通过UUID随机生成36位、32位唯一识别码(唯一字符串)
import java.util.UUID; /** * 通过UUID随机生成36位.32位唯一识别码(唯一字符串) * @author [J.H] * */ public class Test { ...
- node——模块分类,require执行顺序,require注意事项,原理
node.js模块 在node.js开发中一个文件就可以认为是一个模块. 一.node.js模块分类 核心模块Code Module.内置模块.原生模块 fs http path url ... 所有 ...
- python 用法测试
Python 3.5.6 1.js风格的回调测试 def b(): ') def a(fn): if callable(fn): fn() a(b) class Sample: def q(self) ...