Spring5(二)

Spring5

六.bean的自动装配

是Spring满足bean依赖的一种方式
Spring会在上下文中自动寻找,并自动给bean装配属性

在Spring中有三种装配的方式

  • 在xml中显示的配置
  • 在java中显示配置
  • 隐式的自动装配bean[重要]

1.环境搭建

一个人有两个动物

public class Dog {
    public void shout(){
        System.out.println("wang~");
    }
}
public class Cat {
    public void shout(){
        System.out.println("miao~");
    }
}
public class People {
    private Cat cat;
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.mnm.pojo.Cat"/>
    <bean id="dog" class="com.mnm.pojo.Dog"/>

    <bean id="people" class="com.mnm.pojo.People">
        <property name="name" value="莫诺蒙"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>
</beans>
import com.mnm.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}
---------------结果---------------
miao~
wang~

2.ByName自动装配

在这里插入图片描述
修改beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.mnm.pojo.Cat"/>
    <bean id="dog" class="com.mnm.pojo.Dog"/>

    <!--会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid 注意:区分大小写-->
    <bean id="people" class="com.mnm.pojo.People" autowire="byName">
        <property name="name" value="莫诺蒙"/>
    </bean>
</beans>

测试

import com.mnm.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}
---------------结果---------------
miao~
wang~

3.ByType自动装配

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="Cat" class="com.mnm.pojo.Cat"/>
    <bean id="Dog" class="com.mnm.pojo.Dog"/>

    <!--会自动在容器上下文中查找,和自己对象属性类型相同的bean方,必须保证这个类型全局唯一-->
    <bean id="people" class="com.mnm.pojo.People" autowire="byType">
        <property name="name" value="莫诺蒙"/>
    </bean>
</beans>

测试

import com.mnm.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}
---------------结果---------------
miao~
wang~

小结:

  • ByName我们需要保证所有bean的id唯一,而且这个bean需要和自动注入的属性的set方法的值一致!
  • ByType的时候,需要保证所有bean的Class唯一,并且这个bean需要和自动注入的属性的类型一致!

4.使用注解实现自动装配

JDK1.5支持的注解,Spring从2.5开始支持注解
官网的话:
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.

要使用注解须知:

  • 导入约束,context约束
  • 配置注解的支持:
<context:annotation-config/>
<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启注解支持-->
    <context:annotation-config/>
    <bean id="cat" class="com.mnm.pojo.Cat"/>
    <bean id="dog" class="com.mnm.pojo.Dog"/>
    <bean id="people" class="com.mnm.pojo.People"/>
</beans>
import org.springframework.beans.factory.annotation.Autowired;

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

测试:

import com.mnm.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}
---------------结果---------------
miao~
wang~

也可以写在set方法上
在这里插入图片描述
甚至可以在有注解的条件下将set方法去除掉,前提是你这个自动装配的属性在IOC(spring)容器中存在,默认按类型ByType 如果有重复值则按ByName 否则报错
在这里插入图片描述
科普:

@nullable 字段标记了这个注解,说明这个字段可以为null
@Autowired(required = false) 如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
@Qualifier(value=***) 去配合@Autowired的使用,指定一个唯一的bean对象注入 相当于将@Autowired的配置改成ByName

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;

public class People {
    /*如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空*/
    @Autowired(required = false)
    /*如果自动装配的环境比较复杂,自动装配无法通过一个注解[@Autowired]完成的时候,我们可以使用
    * @Qualifier(value=***) 去配合@Autowired的使用,指定一个唯一的bean对象注入*/
    @Qualifier(value = "cat2")
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

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

    public Cat getCat() {
        return cat;
    }

    public Dog getDog() {
        return dog;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启注解支持-->
    <context:annotation-config/>
    <bean id="cat" class="com.mnm.pojo.Cat"/>
    <bean id="cat2" class="com.mnm.pojo.Cat"/>
    <bean id="dog" class="com.mnm.pojo.Dog"/>
    <bean id="people" class="com.mnm.pojo.People"/>
</beans>

测试:

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}
---------------结果---------------
miao~
wang~

@Resource(name = “cat2”)
这个注解的作用和@Autowired的作用一样,只不过在jdk11后貌似被删除了

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;

import javax.annotation.Resource;

public class People {
    @Resource(name = "cat2")
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

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

    public Cat getCat() {
        return cat;
    }

    public Dog getDog() {
        return dog;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启注解支持-->
    <context:annotation-config/>
    <bean id="cat1" class="com.mnm.pojo.Cat"/>
    <bean id="cat2" class="com.mnm.pojo.Cat"/>
    <bean id="dog" class="com.mnm.pojo.Dog"/>
    <bean id="people" class="com.mnm.pojo.People"/>
</beans>

测试:

import com.mnm.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}
---------------结果---------------
miao~
wang~

小结:
@Resource 和 @Autowired的区别

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired 默认通过ByType的方式实现,有多个则ByName
  • @Resource 默认通过ByName的方式实现,找不到则ByType

七.使用注解开发

在Spring4之后,要使用注解开发,必须要保证aop的包导入了
在这里插入图片描述
使用注解需要导入context约束,增加注解的支持,和扫描的路径

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
	
	<context:component-scan base-package="com.mnm"/>
    <context:annotation-config/>

</beans>

1.bean

@component:组件,放在类上,说明这个类被Spring管理了.
等价于<bean id=“user” class=“com.mnm.pojo.User”/>

import org.springframework.stereotype.Component;
/*等价于<bean id="user" class="com.mnm.pojo.User"/>
* @Component 组件*/
@Component
public class User {
    public String name = "莫诺蒙";
}

2.属性如何注入

@Value(“xxx”):赋值,等价于:<property name=“name” value=“莫诺蒙”/>

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    /*等价于:<property name="name" value="莫诺蒙"/>*/
    @Value("莫诺蒙")
    public String name;
}

或者

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    public String name;

    @Value("莫诺蒙")
    public void setName(String name) {
        this.name = name;
    }
}

3.衍生的注解

@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层
dao[@Repository]
service[@Service]
controller[@Controller]
这四个注解功能都是一样的,都代表将某个类注册到Spring容器中,装配bean

import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
}
import org.springframework.stereotype.Service;

@Service
public class UserService {
}
import org.springframework.stereotype.Controller;

@Controller
public class UserController {
}

4.自动装配

上面有详细讲过

@AutoWired:自动装配通过类型,名字
@Qualifier(value=“xxx”):指定一个唯一的bean对象注入 相当于将@Autowired的配置改成ByName
@Resource:自动装配通过名字,类型

5.作用域

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
/*标注单例模式:singleton 原型:prototype */
@Scope("singleton")
public class User {
    @Value("莫诺蒙")
    public String name;
}

6.小结

XML与注解

  • XML更加万能,适用于任何场合,维护简单方便
  • 注解不是自己的类使用不了,维护相对复杂

XML与注解最佳实践:

  • XML用来管理bean
  • 注解只负责完成属性的注入
  • 我们在使用的过程中,只需要注意一个问题,必须让注解生效,就需要开启注解的支持

八.使用java的方式配置Spring

我们现在要完全不适用Spring的xml配置,全权交给Java来做

JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能
在这里插入图片描述
在这里插入图片描述

实体类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }
    /*注入初始值*/
    @Value("莫诺蒙")
    public void setName(String name) {
        this.name = name;
    }

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

配置类

import com.mnm.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/*这个也会被Spring托管,注册到容器中,因为他本来就是一个@Component
* @Configuration代表这是一个配置类,就和我们之前看的beans.xml
* */
@Configuration
/*配套@Component使用*/
@ComponentScan("com.mnm.pojo")
@Import(MnmConfig2.class)
/*注意:就是说,@Bean是相当于<bean>标签创建的对象,而我们之前学的@Component是通过spring自动创建的这个被注解声明的对象
* 所以这里去掉@Bean这些东西,然后开启扫描。之后在User头上用@Component即可达到spring自动创建User对象了
* 所以这里相当于有两个User对象被创建了。一个是bean标签创建的(@Bean),一个是通过扫描然后使用@Component,spring自动创建的User对象
* 这里还可以使用@ImportResource(String [ ] )注解来引入其他xml配置文件
* */
public class MnmConfig {
    /*注册一个bean,就相当于我们之前写的一个bean标签,
    这个方法的名字,就相当于bean标签中的id,
    这个方法的返回值,就相当于bean标签中的class属性*/
    @Bean
    public User myUser(){
        return new User();
    }
}
import com.mnm.config.MnmConfig;
import com.mnm.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        //如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig 上下文来获取容器,通过配置类的class对象加载
        ApplicationContext Context = new AnnotationConfigApplicationContext(MnmConfig.class);
        User getUser = Context.getBean("myUser", User.class);
        System.out.println(getUser.getName());
    }
}
---------------结果---------------
莫诺蒙

去掉@bean后
扫描创建的User对象,名称为首字母小写的user,测试类中要稍作修改

import com.mnm.config.MnmConfig;
import com.mnm.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        //如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig 上下文来获取容器,通过配置类的class对象加载
        ApplicationContext Context = new AnnotationConfigApplicationContext(MnmConfig.class);
        User getUser = Context.getBean("user", User.class);
        System.out.println(getUser.getName());
    }
}

这种纯java的配置ifangshi,在SpringBoot中随处可见

九.代理模式

为什么要学习代理模式:
因为这就是SpringAOP的底层!
面试重点:[SpringAOP和SpringMVC]

代理模式的分类:

  • 静态代理
  • 动态代理

在这里插入图片描述

1.静态代理

角色分析:

  • 抽象的角色:一般会使用接口或者抽象类来解决
  • 真实角色:被代理的角色
  • 代理角色:代理真实角色,代理真实角色后,我们一般会做一些附属操作
  • 客户:访问代理对象的人

首先,我们需要建立一个java工程来实现一个例子,从而理解什么是java中的静态代理;

我们需要在项目中新建一个包,然后在包下建一个接口,租房的接口(Rent),紧接着呢,我们在接口中创建一个租房的方法:

package com.mnm.demo01;
//租房
public interface Rent {
    public void rent();
}

这个方法就被称之为抽象方法,接下来呢,我将会去写一个实现类,它们分别是,房东(Landlord)实现类,紧接着,我们需要让这个类实现Rent这个接口,并实现Rent类中定义好的抽象方法:

package com.mnm.demo01;
/*房东*/
public class Landlord implements Rent{
    @Override
    public void rent() {
        System.out.println("房东要出租房子");
    }
}

但是,房东一般没有那么多时间去管理出租这个事务,所以出现了中介,中介帮你实现这些东西
首先,我们需要建一个类Proxy,中介类
刚才房东类实现了一个接口(Rent),那么它的代理类,也必须要去实现Rent,出租的方法实际上还是房东在做

既然需要在代理对象中,需要去调用房东租房的方法,那这个代理对象(Proxy)的代理类中,是不是就需要有房东的属性:

如果直接使用landlord,执行必然是空指针异常,所以,我们需要给这个房东对象使用构造器进行初始化;

package com.mnm.demo01;
/*中介*/
public class Proxy implements Rent{
    private Landlord landlord;

    public Proxy() {
        this.landlord = new Landlord();
    }

    @Override
    public void rent(){
        seeHouse();
        landlord.rent();
        fee();
        contract();

    }

    public void seeHouse(){
        System.out.println("中介带你看房子");
    }

    public void fee(){
        System.out.println("收中介费");
    }

    public void contract(){
        System.out.println("签合同");
    }
}

这个时候,我们再去租房,就只需要经过代理而不需要直面房东了

package com.mnm.demo01;
/*租客*/
public class Client {
    public static void main(String[] args) {
        //你不用接触房东直接租房
        Proxy proxy = new Proxy();
        proxy.rent();
    }
}
---------------结果---------------
中介带你看房子
房东要出租房子
收中介费
签合同

代理模式的好处:

  • 可以使真实角色的操作更加纯粹,不用去关注一些公共的业务
  • 公共的业务就交给代理角色,实现了业务的分工
  • 公共业务发生扩展的时候,方便集中管理

缺点:

  • 一个真实角色就会产生一个代理角色,代码量会翻倍,开发效率变低

2.加深理解

公司现在要为原来的业务代码增加一个日志功能
修改公司原有的业务代码是大忌,这时我们新增一个代理来处理这个功能

公司原有的业务接口

package com.mnm.demo02;
/*抽象方法*/
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void query();
}

业务的实现类,也就是真实对象

package com.mnm.demo02;
/*真实对象*/
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改了一个用户");
    }

    @Override
    public void query() {
        System.out.println("查询了一个用户");
    }
}

这时我们新增一个代理,在真实对象的基础上增加日志的实现

package com.mnm.demo02;

public class UserServiceProxy implements UserService{
    private UserServiceImpl userService;

    public UserServiceProxy() {
        this.userService = new UserServiceImpl();
    }

    @Override
    public void add() {
        log("add");
        userService.add();
    }

    @Override
    public void delete() {
        log("delete");
        userService.delete();
    }

    @Override
    public void update() {
        log("update");
        userService.update();
    }

    @Override
    public void query() {
        log("query");
        userService.query();
    }

    //日志方法
    public void log(String msg){
        System.out.println("[debug]使用了"+msg+"方法");
    }
}

测试:这时我们既没有修改之前的业务代码,也能实现日志输出功能

package com.mnm.demo02;

public class Client {
    public static void main(String[] args) {
        UserServiceProxy userServiceProxy = new UserServiceProxy();
        userServiceProxy.delete();
    }
}
---------------结果---------------
[debug]使用了delete方法
删除了一个用户
..

在这里插入图片描述

3.动态代理

动态代理和静态代理角色一样
动态代理的代理类是动态生成的,不是我们直接写好的!

动态代理也分为两大类

  • 基于接口的动态代理
  • 基于类的动态代理

基于接口:JDK的动态代理[我们使用的]
基于类的:cglib
java字节码实现:javassist

使用前需要了解两个类:Proxy(代理),InvocationHandler(调用处理程序)

InvocationHandler在反射包下
在这里插入图片描述
在这里插入图片描述
且只有一个方法invoke
在这里插入图片描述
在这里插入图片描述
更重要的Proxy
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

动态代理的好处:

  • 可以使真实角色的操作更加纯粹,不用去关注一些公共的业务
  • 公共的业务就交给代理角色,实现了业务的分工
  • 公共业务发生扩展的时候,方便集中管理
  • 一个动态代理类代理的是一个接口,一般就是对应的一类业务
  • 一个动态代理类,可以代理多个类,只要是实现了同一个接口的即可

动态代理实例:
重写租房案例:

依然是租房接口:

package com.mnm.demo03;

public interface Rent {
    public void rent();
}

和要将房子出租的房东:

package com.mnm.demo03;

public class Landlord implements Rent{
    @Override
    public void rent() {
        System.out.println("房东要出租房子");
    }
}

动态代理,通俗点说就是:无需声明式的创建java代理类,而是在运行过程中生成"虚拟"的代理类,被ClassLoader加载。从而避免了静态代理那样需要声明大量的代理类。

package com.mnm.demo03;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/*代理处理程序
* 我们会用这个类,自动生成代理类*/
public class ProxyInvocationHandler implements InvocationHandler {
    /*被代理的接口*/
    private Rent rent;

    public void setRent(Rent rent) {
        this.rent = rent;
    }

    /*生成得到代理对象*/
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(), this);
    }
    /*处理代理实例,并返回结果*/
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        /*动态代理的本质,就是使用反射机制实现*/
        seeHouse();
        Object result = method.invoke(rent, args);
        fare();
        return result;
    }
    public void seeHouse(){
        System.out.println("中介带你看房子");
    }
    public void fare(){
        System.out.println("收中介费");
    }
}

测试类:

package com.mnm.demo03;

public class Client {
    public static void main(String[] args) {
        //真实角色
        Landlord landlord = new Landlord();
        //代理角色,现在没有
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        //通过调用程序处理角色来处理我们要调用的接口对象
        pih.setRent(landlord);
        //这里的proxy就是动态生成的,我们并没有写
        Rent proxy =(Rent) pih.getProxy();
        proxy.rent();
    }
}
---------------结果---------------
中介带你看房子
房东要出租房子
收中介费

4.重写增删改查

不变的增删改查接口

package com.mnm.demo02;
/*抽象方法*/
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void query();
}

实现类也没有任何修改

package com.mnm.demo02;
/*真实对象*/
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改了一个用户");
    }

    @Override
    public void query() {
        System.out.println("查询了一个用户");
    }
}

代理类也只需稍作修改,基本可以当做模板

package com.mnm.demo04;

import com.mnm.demo03.Rent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/*代理处理程序
* 我们会用这个类,自动生成代理类*/
public class ProxyInvocationHandler implements InvocationHandler {
    /*被代理的接口*/
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    /*生成得到代理对象*/
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
    /*处理代理实例,并返回结果*/
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        /*动态代理的本质,就是使用反射机制实现*/
        log(method.getName());
        Object result = method.invoke(target, args);
        return result;
    }
    public void log(String msg){
        System.out.println("执行了"+msg+"方法");
    }
}

测试类:

package com.mnm.demo04;

import com.mnm.demo02.UserService;
import com.mnm.demo02.UserServiceImpl;

public class Client {
    public static void main(String[] args) {
        //真实角色
        UserServiceImpl userService = new UserServiceImpl();
        //代理角色,不存在
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        //设置要代理的对象
        pih.setTarget(userService);
        //动态生成代理类
        UserService proxy =(UserService) pih.getProxy();
        proxy.delete();
    }
}
---------------结果---------------
执行了delete方法
删除了一个用户
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值