2021-05-06 spring笔记

反射

  • 通过反射来获取类的Class对象

    Class cc = Class,forName(“包名”);
    /= 对象名.getClass();
    /=类名.class;
    内置类:类名.class;

  • 一个类在内存中只有一个Class对象
  • 一个类在加载后,类的整个结构都会被封装在Class对象中
  • 只要元素类型和维度一样,就是同一个Class

注解

Java中的注解

内置注解

  • @Override 重写注解
  • @Deprecated 表示不推荐程序员使用,可用
  • @SuppressWarnings(“all”) 表示抑制编译时发生的”all“ 的警告

元注解

  • @Target(value = ElementType.TYPE)表示我们的注解可以用在什么地方
  • @Retention(value = RetentionPolicy.RUNTIME)
    表示在什么地方有效(RUNTIME>CLASS>SOURCE)
  • @Documented 表示是否将我们的注解生成在JAVAdoc中
  • @Inherited 表示子类可以继承父类的注解
    例子
@MyAnnotation()
public class UserDaoIm implements UserDao{
    @Override
    public void add(int a) {
        System.out.println("a = " + a);
    }
}
//表示我们的注解可以用在什么地方
@Target(value = ElementType.TYPE)
//表示在什么地方有效(RUNTIME>CLASS>SOURCE)
@Retention(value = RetentionPolicy.RUNTIME)
//表示是否将我们的注解生成在JAVAdoc中
@Documented
//表示子类可以继承父类的注解
@Inherited
//自定义的注解
@interface MyAnnotation{
    //注解的参数:类型+参数名()
    //default 为默认值
    String changShu() default "";

}

常用依赖

<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.xsd">
<!--    注解的前提-->
    <context:annotation-config/>

@Autowired

Autowired 首先是根据byType 若有两个class相同,则根据byName 即变量名与spring容器中的id名相同
也可以加上 @Qualifier(value = ?)指定容器中id名

java

    @Autowired
    private Address address;
    @Autowired
    private Address address1;

xml

    <bean id="address" class="com.Test3.Address" scope="prototype">
        <property name="addr" value="广州"></property>
    </bean>
    <bean id="address1" class="com.Test3.Address" scope="prototype">
        <property name="addr" value="广东"></property>
    </bean>

@Required

// @Required 和Autowired 相似 @Required(name = ?)和 @Qualifier(value = ?)

@Component

加入扫描过滤器

<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.xsd">
<!--    注解的前提-->
    <context:component-scan base-package="com.Test3"/>
    <context:annotation-config/>
</beans>
@Component
public class Address {

    @Value("hhhh")
    private String addr;

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

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

衍生的注解

@Component有几个衍生注解

  • dao(@Repository)
  • service(@Service)
  • controller(@Controller)
    意思都与component差不多

@Scope(单例模式/·····)

基于 Java 的容器配置

dao类

public class Address {

    @Value("hhhh")
    private String addr;

//    public String getAddr() {
//        return addr;
//    }

//    public void setAddr(String addr) {
//        this.addr = addr;
//    }

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

Config类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration //相当于Bean.xml,内置有@Component
public class Config {

    //创建一个Bean标签 id为getAddress class为Address
    @Bean
    public Address getAddress(){
        return new Address();
    }
}

Main类

        ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        Address address = context.getBean(Address.class);
        System.out.println(address.toString());

AOP

静态代理

角色

  • 虚拟接口
  • 真实被代理人
  • 代理人
  • 客户端

虚拟接口

public interface Hose {
    public void Rent();
}

真实被代理人

public class Host implements Hose{
    @Override
    public void Rent() {
        System.out.println("房东要租房了");
    }
}

代理人

public class Agency {
    private Host host;

    public Agency(){

    }
    public Agency(Host host) {
        this.host = host;
    }
    public void Rent(){
        host.Rent();
        hetong();
    }
    public void hetong(){
        System.out.println("签合同");
    }
}

客户端

public class Client {
    public static void main(String[] args) {
        Host host = new Host();
        Agency agency = new Agency(host);
        agency.Rent();
    }
}

动态代理

  • 动态代理和静态代理的角色一样
  • 利用了反射
  • 动态代理的代理类是动态生成的,不是我们直接写好的
  • 分为两大类:
    • 基于接口–JDK动态代理
    • 基于类–cglib
    • java字节码实现:javasist

基于接口 – JDK动态代理

接口类

public interface UserDao {
    public void add(int a);
}

真实被代理类

public class UserDaoIm implements UserDao{
    @Override
    public void add(int a) {
        System.out.println("a = " + a);
    }
}

处理代理类程序类

public class UserDaoProxy implements InvocationHandler {

    //接口 传入实现接口类的对象,利用多态
    private Object userDao;
    public void setUserDao(Object userDao){
        this.userDao = userDao;
    }
    //得到代理类
    public Object getProxy(){
        //1,被代理类加载器 2,被代理类实现的接口 3,InvocationHandler接口的实现类的对象
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                userDao.getClass().getInterfaces(),this::invoke);
    }

    //处理代理实例,并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        add1();
        //前
        Object result = method.invoke(userDao, args);
        //后
        return result;
    }
    private void add1(){
        System.out.println("加法");
    }
}

客户端

public class main {

    public static void main(String[] args) {
        //创建真实被代理角色
        UserDaoIm userDaoIm = new UserDaoIm();
        //创建处理代理角色程序 没代理类
        UserDaoProxy userDaoProxy = new UserDaoProxy();
        //多态,传入真实角色
        userDaoProxy.setUserDao(userDaoIm);
        //得到代理类实例 动态代理代理的是一个接口
        UserDao userDao = (UserDao) userDaoProxy.getProxy();
        userDao.add(222);
    }
}

基于spring的原生API接口-实现AOP

接口类

public interface Hose {
    public void Rent();
}

被代理类

public class Host implements Hose{
    @Override
    public void Rent() {
        System.out.println("房东要租房了");
    }
}

要增加的方法的类-前

public class Log implements MethodBeforeAdvice {
    //method 要执行的目标对象方法
    //object 参数
    // o 目标对象
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(method.getName()+"==="+o.getClass().getName());
    }
}

要增加的方法的类-后

public class AterLog implements AfterReturningAdvice {
    //o1 目标对象
    //method 目标对象方法
    //o 返回值
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println(o1.getClass().getName()+"=="+method.getName()+"==="+o);
    }
}

spring-xml

<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
    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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="host" class="com.SpringAop.Test01.Host"></bean>
    <bean id="log" class="com.SpringAop.Test01.log.Log"></bean>
    <bean id="aterLog" class="com.SpringAop.Test01.log.AterLog"></bean>
    <aop:config>
<!--        要切入的点 execution(修饰符 列名 参数)-->
        <aop:pointcut id="pointcut" expression="execution(* com.SpringAop.Test01.Host.*(..))"/>
<!--        执行坏绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor>
        <aop:advisor advice-ref="aterLog" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>

客户端

public class Client {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Bans.xml");
        //动态代理代理的是一个接口
        Hose host = context.getBean("host", Hose.class);
        host.Rent();
    }
}

自定义切面实现AOP

切面类

public class Diy {
    public void before(){
        System.out.println("qian");

    }
    public void after(){
        System.out.println("hou");
    }
}

接口,被代理类,客户端与上相似


xml

<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
    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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    注解的前提-->
<!--    <context:component-scan base-package="com.Test3"/>-->
<!--    <context:annotation-config/>-->
    <bean id="host" class="com.SpringAop.Test01.Host"></bean>
    <bean id="log" class="com.SpringAop.Test01.log.Log"></bean>
    <bean id="aterLog" class="com.SpringAop.Test01.log.AterLog"></bean>
    <bean id="diy" class="com.SpringAop.Test01.diy.Diy"></bean>

    <aop:config>
        <aop:aspect ref="diy">
            <aop:pointcut id="pointcut" expression="execution(* com.SpringAop.Test01.Host.*(..))"/>
            <aop:before method="before" pointcut-ref="pointcut"></aop:before>
            <aop:after method="after" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>

使用注解实现AOP

切面注解

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class Diy {
    @Before("execution(* com.SpringAop.Test01.Host.*(..))")
    public void before(){
        System.out.println("qian");

    }
    @After("execution(* com.SpringAop.Test01.Host.*(..))")
    public void after(){
        System.out.println("hou");
    }
}

xml

<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
    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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    注解的前提-->
<!--    <context:component-scan base-package="com.Test3"/>-->
<!--    <context:annotation-config/>-->
    <bean id="host" class="com.SpringAop.Test01.Host"></bean>
    <bean id="log" class="com.SpringAop.Test01.log.Log"></bean>
    <bean id="aterLog" class="com.SpringAop.Test01.log.AterLog"></bean>
    <bean id="diy" class="com.SpringAop.Test01.diy.Diy"></bean>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

其余类与上同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值