码迷,mamicode.com
首页 > 编程语言 > 详细

Spring之AOP静态代理

时间:2015-02-05 01:55:20      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

package com.fxr.spring.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fxr.spring.model.User;
import com.fxr.spring.service.UserService;


@Controller("useraction")//MVC控制层一般使用@Controller
@Scope("prototype")//设置为多例模式
public class UserAction {

	private User u;
	private int id;
	private UserService userservice;
	private List<String>names;
	
	
	public List<String> getNames() {
		return names;
	}




	public void setNames(List<String> names) {
		this.names = names;
	}




	public UserAction(){
		
	}
	
	
	
	
	public User getU() {
		return u;
	}

	public void setU(User u) {
		this.u = u;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public UserService getUserservice() {
		return userservice;
	}

	@Resource
	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}

	public void add(){
		userservice.add(u);
		
		
	}
	
	public void delete(){
		userservice.delete(id);
	}
	
	
	
	
}
=============================================
package com.fxr.spring.dao;

import com.fxr.spring.model.User;

public interface IUserDao {

	public void add(User u);
	
	public void delete(int id);
	
	
}
=============================================
package com.fxr.spring.dao;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.fxr.spring.model.User;

//@Component("userdao")//公共的创建bean的Annotation
@Repository("userdao")//@Repository一般用于DAO的注入

public class UserDao implements IUserDao{

	@Override
	public void add(User u) {
		
		System.out.println("添加"+u);
		
	}

	@Override
	public void delete(int id) {
		System.out.println("删除"+id);
		
	}

}
================================================
package com.fxr.spring.dao;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.fxr.spring.model.User;
import com.fxr.spring.util.Logger;
@Repository("userproxydao")
public class UserProxyDao implements IUserDao{

	private IUserDao user;
	
	
	public IUserDao getUser() {
		return user;
	}

	@Resource
	public void setUser(IUserDao user) {
		this.user = user;
	}

	@Override
	public void add(User u) {
		Logger.info("添加用户");
		System.out.println("添加"+u);
		
	}

	@Override
	public void delete(int id) {
		Logger.info("删除用户");
		System.out.println("删除"+id);
		
	}

	
	
}
============================================
package com.fxr.spring.model;

public class User {

	private int id;
	private String name;
	
	public User(int id,String name){
		this.id = id;
		this.name = name;
	}
	
	public User(){
		
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
	
	
	
	
}
===========================================
package com.fxr.spring.service;

import com.fxr.spring.dao.UserDao;
import com.fxr.spring.model.User;

public interface IUserService {

	public void add(User u);
	public void delete(int id);
	
	
	
	
}
================================================
package com.fxr.spring.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.fxr.spring.dao.IUserDao;
import com.fxr.spring.dao.UserDao;
import com.fxr.spring.model.User;


@Component("userservice")
@Service("userservice")//业务层一般使用@Service

public class UserService implements IUserService{

	private IUserDao userdao;
	
	
	
	
	public IUserDao getUserdao() {
		return userdao;
	}

	@Resource(name="userproxydao")
	public void setUserdao(IUserDao userdao) {
		this.userdao = userdao;
	}

	@Override
	public void add(User u) {
		userdao.add(u);
		
	}

	@Override
	public void delete(int id) {
		userdao.delete(id);
		
	}

}
==============================================
package com.fxr.spring.util;

public class Logger {

	public static void info(String str){
		System.out.println(str);
	}
	
	
}
===============================================
package com.fxr.spring.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fxr.spring.action.UserAction;
import com.fxr.spring.model.HelloWorld;
import com.fxr.spring.model.User;

public class Test {

	private static BeanFactory factory = new ClassPathXmlApplicationContext("bean.xml");
	public static void main(String[] args) {
		
		
//		//创建一个类
//		HelloWorld hello = (HelloWorld) factory.getBean("helloworld");
//		hello.say();
		
		//创建一个类
		UserAction ua1 = (UserAction) factory.getBean("useraction");
		User u = new User(1,"张三");
		ua1.setU(u);
		System.out.println(u);
		ua1.add();
//		UserAction ua2 = (UserAction) factory.getBean("useraction");
//		ua2.add();
	}
	
	
}
=======================================
<?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">
   <!-- 打开Spring的Annotation支持 -->
   <context:annotation-config/>
   <!-- 设定Spring 去哪些包中找Annotation -->
   <context:component-scan base-package="com.fxr.spring"/>
           
</beans>

  

Spring之AOP静态代理

标签:

原文地址:http://www.cnblogs.com/aicpcode/p/4273730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!