1、简介
将AOP和IOC都是用Annotation方式进行实现的demo
2、xml文件
增加了对AOP自动代理的实现:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
<!-- IOC -->
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
<!-- AOP -->
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<!-- IOC -->
<context:component-scan base-package="com.bjsxt"/>
<!-- AOP -->
<aop:aspectj-autoproxy />
</beans>
/*
aspectj 是专门面向AOP编程的框架,spring采用了他
*/
和xml实现方式配置文件的区别:
1、增加两个<context>配置,如上。
2、增加:@Resource(name="u"),这个在java类中修改,相应的,在xml配置中将注入关系的配置 <bean id="userService" class="service.UserService"> <property name="userDAO" ref="u" /> </bean> 去掉。
3、user类
这个没什么好说的:
package com.bjsxt.model;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4、dao类
package com.bjsxt.dao;
import com.bjsxt.model.User;
public interface UserDAO {
public void save(User user);
}
5、daoimpl类
package com.bjsxt.dao.impl;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("u")
public class UserDAOImpl implements UserDAO {
public void save(User user) {
System.out.println("user saved!");
}
}
6、LogInterceptor类
package com.bjsxt.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
//aspect表示切面,before(a)表示执行 a 这个方法之前先执行下面这段代码
@Aspect
//component的作用是将LogInterceptor进行初始化,这样才能将before()和save()组织在一起,但是并没有@Resource对其进行调用
@Component
public class LogInterceptor {
@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")
//上面这句话一定要加上 void.不然会报错。
public void before(){
System.out.println("Method start ! ");
}
}
execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User)) 这句还可以写成通配符的形式:
execution(public * com.bjsxt.dao . . * . * ( . . ))
表示返回值可以是任何类型的 在 com.bjsxt.dao 包下包括子包的任何类的任何方法。
7、UserService类
package com.bjsxt.service;
import javax.annotation.Resource;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("userService")
public class UserService {
private UserDAO userDAO;
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}
}
8、Test类
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
service.add(new User());
ctx.destroy();
}
}
9、输出结果:
Method start !
user saved!
10、更多@用法
package com.bjsxt.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
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
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.dao..*.*(..))")
public void myMethod(){};
@Before("myMethod()")
public void before(){
System.out.println("Method start ! ");
}
/*
@AfterThrowing("myMethod()")
public void afterThrowing(){
System.out.println(" Throwing Exception ! ");
}
*/
@AfterReturning("myMethod()")
public void afterReturning(){
System.out.println("Method end ! ");
}
@Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Around start!");
pjp.proceed();
System.out.println("Around start!");
}
}