动态代理与Maven基础



提示:以下是本篇文章正文内容,下面案例可供参考

一、动态代理

InvocationHandler接口 中实现invoke()方法
Method类
Proxy.newProxyInstance(ClassLoader loader, Class<?>[]	interfaces, InvocationHandler h)

实现动态代理:
目标对象及接口:

public interface UsbSell {
    float sell(int amount);
}

public class UsbKingFactory implements UsbSell {
    @Override
    public float sell(int amount) {
        System.out.println("目标类sell方法");
        return 85.0F;
    }
}

handler代码:

public class mySellHandler implements InvocationHandler {
    private Object target = null;
    public mySellHandler() {
    }
    //传入动态代理目标
    public mySellHandler(Object target) {
        this.target = target;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object res = null;
        res = method.invoke(target,args);
        if (res != null){
            Float price = (Float)res;
            res = price + 25;
        }
        System.out.println("返回一个优惠券");
        return res;
    }
}

使用代理类代码

public static void main(String[] args) {
        //1.创建目标对象
        UsbSell factory = new UsbKingFactory();
        //2.创建调用处理器
        InvocationHandler handler = new mySellHandler(factory);
        //3.创建代理对象
        UsbSell proxy = (UsbSell) Proxy.newProxyInstance(factory.getClass().getClassLoader(),
                factory.getClass().getInterfaces(),
                handler);
        //4.通过代理执行方法
        float price = proxy.sell(1);
        System.out.println("通过代理对象,调用方法:"  +  price);
    }

总结图:
请添加图片描述

个人理解:handler相当于一个回调函数,当代理调用方法时,就时给回调函数传参,让它利用反射机制去调用目标类的方法

二、Maven基础

1.Maven生命周期及约定目录结构

清理、编译、测试、报告、打包、安装、部署

生命周期对应常用命令:
在这里插入图片描述

目录结构:
在这里插入图片描述

2.pom.xml常用配置参数

在这里插入图片描述

3.坐标GAV

在这里插入图片描述

4.依赖使用(dependency)及依赖返回

maven中央仓库:https://mvnrepository.com/

a.依赖使用

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.11</version>
</dependency>

使用时将依赖坐标添加到pom.xml的dependencies中

b.依赖范围

在这里插入图片描述

5. Maven常用设置

a.全局变量

在 Maven 的 pom.xml 文件中,<properties>用于定义全局变量,POM 中通过${property_name}的形式引用变量的值。

定义全局变量:

<properties>
 <spring.version>4.3.10.RELEASE</spring.version>
</properties>

引用:

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>${spring.version}</version>
</dependency>

b.指定资源位置

src/main/java 和 src/test/java 这两个目录中的所有*.java 文件会分别在 comile 和 test-comiple 阶段被编译,
编译结果分别放到了 target/classes 和 targe/test-classes 目录中,但是这两个目录中的其他文件都会被忽略掉,
如果需要把 src 目录下的文件包放到 target/classes 目录,作为输出的 jar 一部分。需要指定资源文件位置。

使用:

<build>
	<resources>
		 <resource>
			 <directory>src/main/java</directory><!--所在的目录-->
				 <includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
				 <include>**/*.properties</include>
				 <include>**/*.xml</include>
			 </includes>
			 <!--filtering 选项 false 不启用过滤器, *.property 已经起到过滤的作用了 -->
			 <filtering>false</filtering>
		 </resource>
	 </resources>
</build>

6. Maven多模块管理

I.创建一个空工程

II.创建一个Maven模块

maven父工程必须遵守以下两点要求:
    1.packaging标签的文本内容必须设置为pom
    2.把src删除掉

父模块pom文件配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>chong.jiang.maven</groupId>
    <artifactId>maven_parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0</version>

	<!--子模块-->
    <modules>
        <module>maven_son_01</module>
        <module>maven_web_son_02</module>
    </modules>

    <!--统一让子模块的pom文件继承父工程的pom文件-->
    <!--maven父工程必须遵守以下两点要求:
        1.packaging标签的文本内容必须设置为pom
        2.把src删除掉
    -->
    <!--父统一管理依赖的版本号-->
    <properties>
        <!--自定义标签名称-->
        <!--通常管理依赖版本号的标签名称由 = 模块名称(artifactId) + 字段version-->
        <junit-version>4.11</junit-version>
    </properties>
    <!--父工程要加强子模块的所有依赖-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit-version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!--统一编译版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>15</source>
                    <target>15</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

III.子模块创建

在这里插入图片描述
子模块的pom文件配置

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>maven_son_01</artifactId>
    <groupId>chong.jiang.maven</groupId>
    <version>1.0.0</version>
  </parent>
  <artifactId>maven_son_01_son_01</artifactId>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <!--声明式依赖,子模块的版本号需要去继承父模块的版本号-->
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
  </dependencies>
  <build>
  </build>
</project>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值