MyEclipse2014快速配置Spring & Spring Testing, Spring AOP简单使用
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简单使用的更多相关文章
- Spring框架-IOC和AOP简单总结
参考博客: https://blog.csdn.net/qq_22583741/article/details/79589910 1.Spring框架是什么,为什么,怎么用 1.1 Spring框架是 ...
- Spring源码解析-AOP简单分析
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等,不需要去修改业务相关的代码. 对于这部分内容,同样采用一个简单的例子和源码来说明. 接口 public ...
- [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 ...
- Spring、Spring Boot和TestNG测试指南 - 使用Spring Boot Testing工具
Github地址 前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目. 在 ...
- Spring、SpringMVC、Spring Boot、Spring Cloud 概念、关系及区别
注:此文章转载于其他大神 一.正面解读: Spring主要是基于IOC反转Beans管理Bean类,主要依存于SSH框架(Struts+Spring+Hibernate)这个MVC框架,所以定位很明确 ...
- JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)
接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...
- spring tx:advice 和 aop:config 配置事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- eclipse快速配置spring相关xml文件头信息
通过spring tools 插件工具来快速配置xml头信息 ctrl +n 创建-----------> 输入spring 选中spring Beann Configuration File ...
- Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务
Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...
随机推荐
- .NET计时器的使用-Stopwatch类
作用: 微软提供的常用于统计时间消耗的类,作为一个固定的API接口供大家使用. 先看代码: using System; using System.Collections.Generic; using ...
- maven私服客户端配置
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...
- 32位JDK和64位JDK
32位和64位系统在计算机领域中常常提及,但是仍然很多人不知道32位和64位的区别,所以本人在网上整理了一些资料,并希望可以与大家一起分享.对于32位和64位之分,本文将分别从处理器,操作系统,JVM ...
- 20145201《Java程序设计》第7周学习总结
20145201 <Java程序设计>第七周学习总结 教材学习内容总结 本周学习了课本第十二.十三章内容,即Lambda.时间与日期 第十二章 Lambda 12.1 认识Lambda语法 ...
- 初识 Zookeeper
云计算越来越流行的今天,单一机器处理能力已经不能满足我们的需求,不得不采用大量的服务集群.服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,这些信息如何推送到各个节点?并且保 ...
- CommStringLib
#include "syswatcher/CommStringLib.h" //#include "String16.h" #undef LOG_TAG #de ...
- python处理数据的风骚操作[pandas 之 groupby&agg]
https://segmentfault.com/a/1190000012394176 介绍 每隔一段时间我都会去学习.回顾一下python中的新函数.新操作.这对于你后面的工作是有一定好处的.本文重 ...
- Centos6.5 安装gitlab 并使用自带的nginx
Centos6.5 安装gitlab 并使用自带的nginx 1.安装依赖 yum -y install policycoreutils openssh-server openssh-clients ...
- 配置可对外链接的Redis
链接服务器的Redis telnet 192.168.1.200 6379 Trying 192.168.1.200... telnet: Unable to connect to remote ho ...
- sklearn学习笔记之岭回归
岭回归 岭回归是一种专用于共线性数据分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息.降低精度为代价获得回归系数更为符合实际.更可靠的回归方法,对病 ...