说明:此处还用到了 Spring 的依赖注入 --> 采用的是annotation方式
Spring配置文件如下:文件名:beans.xml
UserDAO.java代码如下:<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.bjsxt" /> </beans>
UserDAO.java实现类UserDAOImpl.java的代码如下:package com.bjsxt.dao; import java.util.Set; import com.bjsxt.model.User; /** * @Title: UserDAO.java * @Description: * @Copyright: Copyright (c) 2013 * @Company: Cvicse * @author qin_hqing * @date 2014-3-11 * @version 1.0 */ public interface UserDAO { public abstract void save(User user); public abstract void delete(User user); }
UserService.java业务处理类代码package com.bjsxt.dao.impl; import java.util.Set; import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; /** * @Title: UserDAOImpol.java * @Description: * @Copyright: Copyright (c) 2013 * @Company: Cvicse * @author qin_hqing * @date 2014-3-11 * @version 1.0 */ @Component("userDAO") public class UserDAOImpl implements UserDAO { /* (non-Javadoc) * @see com.bjsxt.dao.UserDAO#save(com.bjsxt.model.User) */ private int daoId; private String daoStatus; public int getDaoId() { return daoId; } public void setDaoId(int daoId) { this.daoId = daoId; } public String getDaoStatus() { return daoStatus; } public void setDaoStatus(String daoStatus) { this.daoStatus = daoStatus; } @Override public void save(User user) { System.out.println(" save UserDAOImpl ! " + user.getUsername()); } @Override public void delete(User user) { System.out.println(" delete UserDAOImpl ! " + user.getUsername()); } }
测试类TestUserService.java代码如下:package com.bjsxt.service; import java.util.Set; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; 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; /** * @Title: UserService.java * @Description: * @Copyright: Copyright (c) 2013 * @Company: Cvicse * @author qin_hqing * @date 2014-3-11 * @version 1.0 */ //相当于setter注入XML配置中的bean的id @Component("userService") public class UserService { private UserDAO userDAO ; @PostConstruct public void init(){ System.out.println("init"); } public UserDAO getUserDAO() { return userDAO; } @Resource(name="userDAO") public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void save(User user) { userDAO.save(user); } public void delete(User user) { userDAO.delete(user); } public Set<String> getSets() { return userDAO.getSets(); } @PreDestroy public void destroy(){ System.out.println("destroy"); } }
package com.bjsxt.test; import java.lang.reflect.Proxy; import java.util.Iterator; import java.util.Set; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.dao.LogInterceptor; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; import com.bjsxt.service.impl.UserServiceImpl; /** * @Title: TestUserService.java * @Description: * @Copyright: Copyright (c) 2013 * @Company: Cvicse * @author qin_hqing * @date 2014-3-11 * @version 1.0 */ /** * 动态代理 LogInterceptor * 而Proxy为InvocationHandler实现类动态创建一个符合某一接口的代理实例 */ public class TestUserService { @Test public void testSpring(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserServiceImpl impl = (UserServiceImpl) ctx.getBean("userService"); UserDAO userDAO = impl.getUserDAO(); LogInterceptor logInterceptor = new LogInterceptor(); logInterceptor.setTarget(userDAO); System.out.println(userDAO.getClass().getInterfaces().getClass()); System.out.println(userDAO.getClass().getInterfaces()[0]);
/*newProxyInstance参数含义
* 第一个参数:代理的类加载器,必须和被代理的对象是一个类加载器
* 第二个参数含义:代理对象要实现的那些接口
* 第三个参数:指派方法调用的调用处理程序
* */ UserDAO userServiceProxy = (UserDAO) Proxy.newProxyInstance(userDAO.getClass().getClassLoader(), userDAO.getClass().getInterfaces(), logInterceptor); User user = new User("xiaohei", "123"); userServiceProxy.delete(user); userServiceProxy.save(user); } }
LogInterceptor.java代码如下:
package com.bjsxt.dao; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; /** * @Title: LogInterceptor.java * @Description: * @Copyright: Copyright (c) 2013 * @Company: Cvicse * @author qin_hqing * @date 2014-3-13 * @version 1.0 */ /** * 动态代理 * InvocationHandler是一个接口,可以通过实现该接口定义横切逻辑, * 在并通过反射机制调用目标类的代码,动态将横切逻辑和业务逻辑编织在一 起。 */ public class LogInterceptor implements InvocationHandler { private Object target ; public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } public void beforeMethod(Method method){ System.out.println(method.getName() + " start !"); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { beforeMethod(method); method.invoke(target, args); return null; } }
/* proxy是代理实例,一般不会用到;method是代理实例上的方法,通过它可以发起对目标类的反射调用;
* args是通过代理类传入 的方法参数,在反射调用时使用
* */