Spring学习笔记-02

8、使用注解开发

在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:annotation-config/>

</beans>
  1. bean
  2. 属性如何注入
// 等价于 <bean id="user" class="com.kuang.pojo.User"/>
// @Component 组件
@Component
public class User {

    public String name;

    // 相当于 <property name="name" value="Wangwang"/>
    @Value("Wangwang")
    public void setName(String name) {
        this.name = name;
    }
}
  1. 衍生的注解
    @Component 有几个衍生注解,在web开发中,会按照mvc三层架构分层
    • dao【@Repository】
    • service 【@Service】
    • controller 【@Controller】
      这四个注解功能都是一样的,都是代表将某个类注册到Spring容器中,装配Bean。
  2. 自动装配注解
	- @Autowired:自动装配通过类型、名字,
	如果@Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx)。
	- @Nullable:字段标记了这个注解,说明这个字段可以为null。
	- @Resource:自动个装配通过名字,类型。
  1. 作用域
// 等价于 <bean id="user" class="com.kuang.pojo.User"/>
// @Component 组件
@Component
@Scope("singleton")
public class User {

    public String name;

    // 相当于 <property name="name" value="Wangwang"/>
    @Value("Wangwang")
    public void setName(String name) {
        this.name = name;
    }
}
  1. 小结
    xml与注解:
  • xml更加万能,适合任何场合,维护简单方便
  • 注解 不是自己类使用不了,维护相对复杂
    xml与注解最佳实践:
  • xml用来管理bean;
  • 注解只负责完成属性的注入
  • 我们在使用的的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持。
	<!-- 指定要扫描的包,这个包下的注解就会生效 -->
    <context:component-scan base-package="com.kuang"/>
    <context:annotation-config/>

9、使用Java的方式配置Spring

我们现在要完全不使用Spring的xml配置了,全权交给Java来做!
JavaConfig是Spring的一个子项目,在 Spring 4 之后,它就成为一个核心功能。
在这里插入图片描述
实体类:

// 这个注解的意思,就是说明这个类被Spring接管,注册到容器中
@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }

    @Value("wonjack") // 属性注入值
    public void setName(String name) {
        this.name = name;
    }

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

配置文件:

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
@ComponentScan("com.kuang.pojo")
@Import(MyConfig2.class)
public class MyConfig {

    // 注册一个bean,就相当于我们之前写的一个bean标签,
    // 这个方法的名字,就相当于bean标签的id属性
    // 这个方法的返回值,就相当于bean标签中的class属性
    @Bean
    public User user() {
        return new User(); // 就是返回要注入的bean的对象!
    }
}

测试类:

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

这种纯Java的配置方式,在SpringBoot中随处可见!

10、代理模式

为什么要学习代理模式?因为这就是SpringAOP的底层!【SpringAOP和SpringMVC】
代理模式的分类:

  • 静态代理
  • 动态代理

10.1 静态代理

角色分析:

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

代码步骤:

  1. 接口
// 租房
public interface Rent {
    public void rent();
}
  1. 真实角色
// 房东
public class Host implements Rent{
    public void rent() {
        System.out.println("房东要出租房子");
    }
}
  1. 代理角色
public class Proxy implements Rent{
    private Host host;

    public Proxy() {
    }

    public Proxy(Host host) {
        this.host = host;
    }

    public void rent() {
        seeHouse();
        host.rent();
        hetong();
        fare();
    }

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

    // 签合同
    public void hetong() {
        System.out.println("签租赁合同");
    }

    // 收中介费
    public void fare() {
        System.out.println("收中介费");
    }
}
  1. 客户端访问代理角色
public class Client {
    public static void main(String[] args) {
        // 房东要租房子
        Host host = new Host();
        // 代理,中介帮房东租房子,但是,代理角色一般会有一些附属操作!
        Proxy proxy = new Proxy(host);

        // 你不用面对房东,直接找中介租房即可!
        proxy.rent();
    }
}

代理模式的好处:

  • 可以使真实角色的操作更加纯粹,不同区关注一些公共的业务
  • 公共业务就交给代理角色,实现了业务的分工
  • 公共业务发生扩展时候,方便集中管理
    缺点:
  • 一个真实角色就会产生一个代理角色;代码量会翻倍,开发效率会变低。

10.2 加深理解

AOP
在这里插入图片描述

10.3 动态代理

  • 动态代理和惊天代理角色一样
  • 动态代理的代理类是动态生成的,不是我们直接写好的。
  • 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
    • 基于接口–JDK动态代理【使用此种形式】
    • 基于类–cglib
    • Java字节码实现:javasist
      需要了解两个类:Proxy:代理,InvocationHandler:调用处理程序
      动态代理的好处:
  • 可以使真实角色的操作更加纯粹,不同区关注一些公共的业务
  • 公共业务就交给代理角色,实现了业务的分工
  • 公共业务发生扩展时候,方便集中管理
  • 一个动态代理类代理的是一个接口,一般就是对应的一类业务
  • 一个动态代理类可以代理多个类,只要是实现了同一个接口即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值