栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring动态代理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring动态代理

 

 

需要实现原始类相同的接口,里面还有原始类,方法里面改的调用原始类的方法

 

 

 

 

 

 

OrderService接口:

package com.itheima;

public interface OrderService {
    public void showOrder();
}

 OrderServiceImpl类:

package com.itheima;

public class OrderServiceImpl implements OrderService{
    @Override
    public void showOrder() {
        System.out.println("OrderServiceImpl.showOrder");
    }
}

OrderServiceProxy代理类:

package com.itheima;

public class OrderServiceProxy implements OrderService {
    private OrderServiceImpl orderService= new OrderServiceImpl();

    @Override
    public void showOrder() {
        System.out.println("--------日志文件--------");
        orderService.showOrder();

    }
}

User类:

package com.itheima;

public class User {
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + ''' +
                ", password='" + password + ''' +
                '}';
    }
}

UserService接口:

package com.itheima;

public interface UserService {
    public void regist(User user);

    public boolean login(String name, String password);
}

log注解:

package com.itheima;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//原注解
@Target(ElementType.METHOD) //指注解应用到哪里,用到方法上写METHOD 用在类上写TYPE
@Retention(RetentionPolicy.RUNTIME) //表示注解在什么时候起作用
public @interface log {
}

 

UserServiceImpl类:

package com.itheima;

public class UserServiceImpl implements UserService {

    @log  //加注解
    @Override
    public void regist(User user) {
        System.out.println("UserServiceImpl.regist 业务运算 +DAO");
        //throw new RuntimeException("测试异常");
    }

    @Override
    public boolean login(String name, String password) {
        System.out.println("UserService.login");
        return true;
    }
}

UserServiceProxy代理类:

package com.itheima;

public class UserServiceProxy implements UserService {
    //代理类需要引入原始类
    private UserServiceImpl userService=new UserServiceImpl();

    @Override
    public void regist(User user) {
        System.out.println("----日志方法-----");
        //需要调用原始类的方法
        userService.regist(user);
    }

    @Override
    public boolean login(String name, String password) {
        System.out.println("----日志方法-----");
        //需要调用原始类的方法
        userService.login(name, password);
        return false;
    }
}

applicationContext.xml配置文件:








    

    







    

    


   


    

    
    


    


    


    










    







   


    

    
        
        
        
    



applicationContext1.xml:








    

    







    


   


   

    
        
        
        
    



测试类TestProxy:

package com.itheima;

import com.itheima.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestProxy {
    //测试静态代理的方法
    @Test
    public void test1(){
        UserService userService=new UserServiceProxy();
        userService.login("suns","123456");
        userService.regist(new User());

        System.out.println("================");
        OrderService orderService=new OrderServiceProxy();
        orderService.showOrder();
    }
    //测试动态代理的方法,所有方法作为切入点,所有方法有额外功能
    
    @Test
    public void test2(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        boolean result= userService.login("suns","123456");
        System.out.println(result);
        userService.regist(new User());
    }
    //测试动态代理的方法,如果上面所有方法作为切入点,这个方法也有额外方法
    @Test
    public void test3(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        OrderService userService=(OrderService)ctx.getBean("orderService");

        userService.showOrder();
    }
//只让login方法作为切入点测试:其他方法没有额外功能
    
    @Test
    public void test4(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        boolean result= userService.login("suns","123456");

        userService.regist(new User());
    }

    @Test
    public void test5(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        userService.login("suns","123456");
        userService.regist(new User());
    }

    
    @Test
    public void test6(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        userService.login("suns","123456");
        userService.regist(new User());
    }
    
    @Test
    public void test7(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        userService.login("suns","123456");
        userService.regist(new User());
    }


    
    @Test
    public void test8(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        userService.login("suns","123456");
        userService.regist(new User());
    }



    
    @Test
    public void test9(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext1.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        userService.login("suns","123456");
        userService.regist(new User());
    }

    
    @Test
    public void test10(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext1.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        userService.login("suns","123456");
        userService.regist(new User());
    }

    
    @Test
    public void test11(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext1.xml");
        UserService userService=(UserService)ctx.getBean("userService");

        userService.login("suns","123456");
        userService.regist(new User());
    }

}

test1测试结果:

2.

 

3.

 

4.

 

5.

 

6.

 

7.

 

8.

 

9.

 

10 

 

11.

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890606.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号