spring+静态代理

       利用spring框架实现了一个简单的静态代理,在这里做一些记录,把学习的过程记录下来。先讲一讲代理,代理类和被代理的类有共同的父类或父接口,这样在任何使用被代理类对象的地方都可以用代理对象替代。代理类负责请求的预处理、过滤、将请求分派给委托类处理、以及委托类执行完请求后的后续处理,使被代理类只关注本身的逻辑,提高了代码的重用性。静态也就是在程序运行前就已经存在代理类的字节码文件,代理类和委托类的关系在运行前就确定了。 

      实体类:

package com.spriProxy.entity;

public class User {
	private int id;
	private String userName;
	private String passWord;
	public User(String userName){
		this.userName = userName;
	}
        ……//set,get方法
}
    接口:
package com.spriProxy.dao;

import com.spriProxy.entity.User;

public interface UserDao {
	//添加用户
	public void addUser(User user);
	//删除用户
	public void deleteUser(User user);
	//更新用户
	public boolean updateUser(User user);
}
  被代理类:
package com.spriProxy.dao.impl;

import org.springframework.stereotype.Repository;

import com.spriProxy.dao.UserDao;
import com.spriProxy.entity.User;

public class UserDaoImpl implements UserDao {

	@Override
	public void addUser(User user) {
		System.out.println("增加用户……"+user.getUserName());
	}

	@Override
	public void deleteUser(User user) {
		System.out.println("删除用户……");
	}

	@Override
	public boolean updateUser(User user) {
		System.out.println("更新用户……");
		return true;
	}

}
    代理类:
package com.spriProxy.util.impl;

import org.springframework.stereotype.Repository;

import com.spriProxy.dao.UserDao;
import com.spriProxy.entity.User;
import com.spriProxy.util.Level;
import com.spriProxy.util.Logger;

public class UserProxy implements UserDao {
	
	private UserDao userDao;

	//此处注入的Dao是userDaoImpl
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	//调用UserDaoImpl的方法
	@Override
	public void addUser(User user) {
		Logger.info(Level.DEBUGE, "DEBUGE,增加用户前");
		userDao.addUser(user);
		Logger.info(Level.INFO, "INFO,增加用户后");
	}

	@Override
	public void deleteUser(User user) {
	}

	@Override
	public boolean updateUser(User user) {
		return false;
	}

}
代理类和被代理类都实现了UserDao这个接口,在spring.xml配置文件中将实现了UserDao的UserProxy注入到UserSeriveImpl中,利用 接口回调调用addUser方法。

     spring.xml文件:

 <beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                       http://www.springframework.org/schema/beans/spring-beans.xsd 
                       http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="userServiceImpl" class="com.spriProxy.service.impl.UserServiceImpl">
          <property name="userDao" ref="userProxy"></property>
    </bean>
    <bean id="userDaoImpl" class="com.spriProxy.dao.impl.UserDaoImpl"/>
    <bean id="userProxy" class="com.spriProxy.util.impl.UserProxy">
          <property name="userDao" ref="userDaoImpl"></property>
    </bean>
 </beans>
    测试类:

package com.spriProxy.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spriProxy.entity.User;
import com.spriProxy.service.impl.UserServiceImpl;

public class Test {
	public static void main(String[] args) {
		User user = new User("StaticProxy");
	    ApplicationContext context = new ClassPathXmlApplicationContext("com/spriProxy/config/spring.xml");
	    UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl"); 
	    userServiceImpl.addUser(user);
	}
}
   运行结果:

spring+静态代理_第1张图片

你可能感兴趣的:(java,代理,框架)