Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

JAVA动态代理

来源: yaerfeng 分享至:
package vo;

public class User {
	private int id;
	private String username;
	private String password;
	
	public User(){
		System.out.println("user对象创建成功");
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
}



UserDao.java

package dao;

import vo.User;

public interface UserDAO {
	void insert(User user);
	
	void test();
}


TaskDao.java

package dao;

public interface TaskDAO {
	void delete(String tid);
}

TaskDaoImpl.java

package dao.imp;

import dao.TaskDAO;

public class TaskDAOIMP implements TaskDAO {

	@Override
	public void delete(String tid) {
		System.out.println("删除任务");
	}

}

UserDaoImpl:

package dao.imp;

import vo.User;
import dao.UserDAO;

public class UserDAOIMP implements UserDAO {
	
	@Override
	public void insert(User user) {
		System.out.println("UserDAOIMP的insert(user)方法被调用");
	}

	@Override
	public void test() {
		System.out.println("UserDAOIMP的test()方法被调用");
		
	}

}

Test.java

package test;

import java.lang.reflect.Proxy;

import proxy.DynaProxy;
import dao.TaskDAO;
import dao.UserDAO;
import dao.imp.TaskDAOIMP;
import dao.imp.UserDAOIMP;

public class Test {

	public static void main(String[] args) {
		
		UserDAOIMP userDAOIMP = new UserDAOIMP();
		
		UserDAO userDAO = (UserDAO)new DynaProxy().createOject(userDAOIMP);
		
		userDAO.test();
		
		System.out.println("*************************");
		
		TaskDAO taskDAO = (TaskDAO)new DynaProxy().createOject(new TaskDAOIMP());
		
		taskDAO.delete("1");
	}

}




Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史