开篇

之前,在用spring编码调试的时候,有时候发现被自动注入的对象是原始类的对象,有时候是代理类的对象,那什么时候注入的原始类对象呢,有什么时候注入的是代理类的对象呢?心里就留下了这个疑问。后来再次看spring aop的时候变有了大胆的想法。

案例

先添加springboot依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
添加测试的类
  • 添加Service1
package jfound.service;
public interface DemoService {
}
package jfound.service.impl;
import jfound.service.DemoService;
import org.springframework.stereotype.Service;
@Service
public class DemoServiceImpl implements DemoService {
}
  • 添加Service2
package jfound.service;
public interface Demo2Service {
void asyncDemo();
}
package jfound.service.impl;
import jfound.service.Demo2Service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class Demo2ServiceImpl implements Demo2Service {
@Override
@Async
public void asyncDemo() {
System.out.println("Demo2Service:" + Thread.currentThread().getName());
}
}
  • 添加Service3
package jfound.service.impl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class Demo3Service {
@Async
public void asyncDemo() {
System.out.println("Demo3Service:" + Thread.currentThread().getName());
}
}
  • Application
package jfound.proxycheck;
import jfound.service.Demo2Service;
import jfound.service.DemoService;
import jfound.service.impl.Demo3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import javax.annotation.PostConstruct; @SpringBootApplication
@EnableAsync
public class CheckApplication {
@Autowired
private DemoService demoService;
@Autowired
private Demo2Service demo2Service;
@Autowired
private Demo3Service demo3Service;
@PostConstruct
public void init() {
System.out.println("------------");
System.out.println("DemoService:"+demoService.getClass().getName());
System.out.println("Demo2Service:"+demo2Service.getClass().getName());
System.out.println("Demo3Service:"+demo3Service.getClass().getName());
System.out.println("------------");
demo2Service.asyncDemo();
demo3Service.asyncDemo();
System.out.println("CheckApplication:"+Thread.currentThread().getName());
}
public static void main(String[] args) {
SpringApplication.run(CheckApplication.class);
}
}
代码描述
  • 添加了3个service,DemoServiceDemo2Service是接口,有实现类。Demo3Service是没有接口,只有单一的类
  • Demo2ServiceDemo3ServiceasyncDemo()方法上有@Async注解
  • CheckApplication方法上有 @EnableAsync,用来开启异步
运行结果
------------
DemoService:jfound.proxycheck.service.impl.DemoServiceImpl
Demo2Service:com.sun.proxy.$Proxy37
Demo3Service:jfound.proxycheck.service.impl.Demo3Service$$EnhancerBySpringCGLIB$$b4ca4e7c
------------
Demo2Service:SimpleAsyncTaskExecutor-1
CheckApplication:main
Demo3Service:SimpleAsyncTaskExecutor-2

结果可以看出DemoService是被注入的是原始类的对象,Demo2Service被注入的对象是jdk代理的对象,Demo3Service被注入的对象是cglib的代理对象

将注入的demo2Service改为实现类注入
@Autowired
private Demo2ServiceImpl demo2Service;

运行结果如下:

***************************
APPLICATION FAILED TO START
*************************** Description: The bean 'demo2ServiceImpl' could not be injected as a 'jfound.service.impl.Demo2ServiceImpl' because it is a JDK dynamic proxy that implements:
jfound.service.Demo2Service Action: Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

上面错误描述的是demo2ServiceImpl是实现Demo2Service接口的一个jdk动态代理,不能直接被注入

强制使用cglib

修改CheckApplication中的 @EnableAsync如下

@EnableAsync(proxyTargetClass = true)

运行结果如下:

------------
DemoService:jfound.proxycheck.service.impl.DemoServiceImpl
Demo2Service:jfound.proxycheck.service.impl.Demo2ServiceImpl$$EnhancerBySpringCGLIB$$c39af2f2
Demo3Service:jfound.proxycheck.service.impl.Demo3Service$$EnhancerBySpringCGLIB$$b074e2af
CheckApplication:main
Demo2Service:SimpleAsyncTaskExecutor-1
Demo3Service:SimpleAsyncTaskExecutor-2

上面结果是Demo2ServiceDemo3Service被注入的都是cglib代理类

结论

spring很多功能都是通过aop来实现,如果事务,缓存注解,异步、还有一些自定义的aop等等,而aop是通过动态代理来实现的,spring主要用到的动态代理有jdk的动态代理和cglib。

  • Spring 在没有使用aop的时候自动注入的时候是原始类型对象
  • 在发生aop的时候,若代理对象有实现接口,则默认会使用jdk动态代理
  • 在发生aop的时候,若代理对象没有实现接口,则默认会使用cglib动态代理
  • jdk动态代理必须有实现接口
  • 可以强制使用cglib来做spring动态代理

微信关注我,发现更多java领域知识

Spring注入的对象到底是什么类型的更多相关文章

  1. spring学习——注入静态对象属性

    spring注入静态对象属性时,因为虚拟机类加载问题,直接在属性上使用@Autowired 是不可以的.需要在属性对应的set方法上@Autowired,并且,set方法不能定义为static. 1. ...

  2. 拦截器通过Spring获取工厂类,注入bean对象

    // 这里需要注意一点,我们在拦截器内无法通过SpringBean的方式注入LoggerJPA,我只能通过另外一种形式. /** * 根据传入的类型获取spring管理的对应dao * @param ...

  3. Spring注入属性、对象

    对Category和Product注入属性,并且对Product对象,注入一个Category对象 一.新建项目 二.导包 三.新建Category类 package com.yyt.pojo; pu ...

  4. 没有纳入spring管理的类如何注入spring管理的对象

    spring 如何在普通类中调用注入的对象? spring 在Thread中注入@Resource失败,总为null~解决 springmvc 注入总是空指针异常? 以上的几个问题就是我在项目中遇到的 ...

  5. 1.JSON 转换对象失败问题 2.spring注入失效

    今天做项目中将一个json 字符串转换为对象,但结果怎么都转换不了!——————最后发现问题,原来是因为这个类我给他添加了带参数的构造器!导致转换失败! 在添加一个无参的构造器就好了! 第二个:今天调 ...

  6. 谈谈Spring中的对象跟Bean,你知道Spring怎么创建对象的吗?

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 推荐阅读: Spring官网阅读 | 总结篇 Spring杂 ...

  7. Spring注入中byType和byName的总结

    1.首先,区分清楚什么是byType,什么是byName. <bean id="userServiceImpl" class="cn.com.bochy.servi ...

  8. 一个Java对象到底占用多大内存?

    最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...

  9. 一个Java对象到底占用多大内存

    在网上搜到了一篇博客讲的非常好,里面提供的这个类也非常实用: import java.lang.instrument.Instrumentation; import java.lang.reflect ...

随机推荐

  1. Clustered和Nonclustered Indexes 各自得特点和区别及长短处

    1 簇索引 簇索引对表的物理数据页中的数据按列进行排序然后再重新存储到磁盘上即簇索 引与数据是混为一体的它的叶节点中存储的是实际的数据由于簇索引对表中的数据一 一进行了排序因此用簇索引查找数据很快但由 ...

  2. mybatis源码学习(三):MappedStatement的解析过程

    我们之前介绍过MappedStatement表示的是XML中的一个SQL.类当中的很多字段都是SQL中对应的属性.我们先来了解一下这个类的属性: public final class MappedSt ...

  3. jQuery学习(三)

    事件 on方法可以将一个事件绑定在jQuery对象上,当你的操作触发了这些事件时,便会调用你所绑定的函数. 例如,给某个超链接绑定点击事件. <head> <meta http-eq ...

  4. Java——HTTP超详细总结

    HTTP协议概述 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...

  5. H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分

    H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...

  6. P2765 魔术球问题 网络流二十四题重温

    P2765 魔术球问题 知识点::最小点覆盖 这个题目要拆点,这个不是因为每一个球只能用一次,而是因为我们要求最小点覆盖,所以要拆点来写. 思路: 首先拆点,然后就是开始建边,因为建边的条件是要求他们 ...

  7. Elasticsearch系列---Term Vector工具探查数据

    概要 本篇主要介绍一个Term Vector的概念和基本使用方法. term vector是什么? 每次有document数据插入时,elasticsearch除了对document进行正排.倒排索引 ...

  8. 【BIM】BIMFACE中实现电梯实时动效

    背景 在运维场景中,电梯作为运维环节重要的一部分是不可获缺的,如果能够在三维场景中,将逼真的电梯效果,包括外观.运行状态等表现出来,无疑是产品的一大亮点.本文将从无到有介绍如何在bimface中实现逼 ...

  9. C# 基础至集合-数组、List<T>、ArrayList、LinkedList、HashMap的一些区别

    1:数组 ]; //赋值 strs[] = "; strs[] = "; //修改 strs[] = "burg"; //删除 没法删除 除非转化为可变数组li ...

  10. Try-Catch包裹的代码异常后,竟然导致了产线事务回滚!

    导读:​一段被try-catch包裹后的代码在产线稳定运行了200天后忽然发生了异常,而这个异常竟然导致了产线事务回滚.这期间究竟发生了什么?日常在项目过程中该如何避免事务异常?就在这个时候,老板拿着 ...