Spring循环依赖测试,日志解析。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Bof_jangle/article/details/82629698

此处定义A B两个接口,并进行互相依赖。

A

package xyz.jangle.service;

public interface A {
	
	void testA();

}

AImpl

package xyz.jangle.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.jangle.service.A;
import xyz.jangle.service.B;

@Service
public class AImpl implements A {
	
	@Autowired
	private B b;

	@Override
	public void testA() {
		System.out.println("aaa");
		b.testB();
	}
	

}

B

package xyz.jangle.service;

public interface B {
	
	void testB();

}

BImpl

package xyz.jangle.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.jangle.service.A;
import xyz.jangle.service.B;

@Service
public class BImpl implements B {
	@Autowired
	private A a;

	@Override
	public void testB() {
		System.out.println("bbb");
//		a.testA();
	}
	
	

}

启动日志:

  2018-09-11 13:46:05  [ localhost-startStop-1:2227 ] - [ DEBUG ]  Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@162e29a: defining beans [AImpl,BImpl,userServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,executeIntercept,org.springframework.aop.config.internalAutoProxyCreator,dataSource,sqlSessionFactory,org.mybatis.spring.mapper.MapperScannerConfigurer#0,txManager,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,userMapper]; root of factory hierarchy
  2018-09-11 13:46:05  [ localhost-startStop-1:2228 ] - [ DEBUG ]  Creating shared instance of singleton bean 'AImpl'
  2018-09-11 13:46:05  [ localhost-startStop-1:2228 ] - [ DEBUG ]  Creating instance of bean 'AImpl'
  2018-09-11 13:46:05  [ localhost-startStop-1:2230 ] - [ DEBUG ]  Creating shared instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2230 ] - [ DEBUG ]  Creating instance of bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2232 ] - [ DEBUG ]  Eagerly caching bean 'org.springframework.transaction.config.internalTransactionAdvisor' to allow for resolving potential circular references
  2018-09-11 13:46:05  [ localhost-startStop-1:2244 ] - [ DEBUG ]  Creating shared instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
  2018-09-11 13:46:05  [ localhost-startStop-1:2245 ] - [ DEBUG ]  Creating instance of bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
  2018-09-11 13:46:05  [ localhost-startStop-1:2245 ] - [ DEBUG ]  Skipping currently created advisor 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2274 ] - [ DEBUG ]  Found AspectJ method: public java.lang.Object xyz.jangle.aop.ExecuteIntercept.aroundExecute(org.aspectj.lang.ProceedingJoinPoint)
  2018-09-11 13:46:05  [ localhost-startStop-1:2276 ] - [ DEBUG ]  Found AspectJ method: public void xyz.jangle.aop.ExecuteIntercept.beforeExecute()
  2018-09-11 13:46:05  [ localhost-startStop-1:2277 ] - [ DEBUG ]  Found AspectJ method: public void xyz.jangle.aop.ExecuteIntercept.afterExecute()
  2018-09-11 13:46:05  [ localhost-startStop-1:2277 ] - [ DEBUG ]  Found AspectJ method: public void xyz.jangle.aop.ExecuteIntercept.afterReturnExecute()
  2018-09-11 13:46:05  [ localhost-startStop-1:2285 ] - [ DEBUG ]  Eagerly caching bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' to allow for resolving potential circular references
  2018-09-11 13:46:05  [ localhost-startStop-1:2291 ] - [ DEBUG ]  Skipping currently created advisor 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2292 ] - [ DEBUG ]  Skipping currently created advisor 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2490 ] - [ DEBUG ]  Finished creating instance of bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
  2018-09-11 13:46:05  [ localhost-startStop-1:2491 ] - [ DEBUG ]  Finished creating instance of bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2495 ] - [ DEBUG ]  Registered injected element on class [xyz.jangle.service.impl.AImpl]: AutowiredFieldElement for private xyz.jangle.service.B xyz.jangle.service.impl.AImpl.b
  2018-09-11 13:46:05  [ localhost-startStop-1:2495 ] - [ DEBUG ]  Eagerly caching bean 'AImpl' to allow for resolving potential circular references
  2018-09-11 13:46:05  [ localhost-startStop-1:2499 ] - [ DEBUG ]  Processing injected method of bean 'AImpl': AutowiredFieldElement for private xyz.jangle.service.B xyz.jangle.service.impl.AImpl.b
  2018-09-11 13:46:05  [ localhost-startStop-1:2501 ] - [ DEBUG ]  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2502 ] - [ DEBUG ]  Creating shared instance of singleton bean 'userMapper'
  2018-09-11 13:46:05  [ localhost-startStop-1:2502 ] - [ DEBUG ]  Creating instance of bean 'userMapper'
  2018-09-11 13:46:05  [ localhost-startStop-1:2503 ] - [ DEBUG ]  Eagerly caching bean 'userMapper' to allow for resolving potential circular references
  2018-09-11 13:46:05  [ localhost-startStop-1:2521 ] - [ DEBUG ]  Invoking afterPropertiesSet() on bean with name 'userMapper'
  2018-09-11 13:46:05  [ localhost-startStop-1:2521 ] - [ DEBUG ]  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2522 ] - [ DEBUG ]  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2532 ] - [ DEBUG ]  Finished creating instance of bean 'userMapper'
  2018-09-11 13:46:05  [ localhost-startStop-1:2533 ] - [ DEBUG ]  Creating shared instance of singleton bean 'BImpl'
  2018-09-11 13:46:05  [ localhost-startStop-1:2533 ] - [ DEBUG ]  Creating instance of bean 'BImpl'
  2018-09-11 13:46:05  [ localhost-startStop-1:2533 ] - [ DEBUG ]  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2535 ] - [ DEBUG ]  Registered injected element on class [xyz.jangle.service.impl.BImpl]: AutowiredFieldElement for private xyz.jangle.service.A xyz.jangle.service.impl.BImpl.a
  2018-09-11 13:46:05  [ localhost-startStop-1:2535 ] - [ DEBUG ]  Eagerly caching bean 'BImpl' to allow for resolving potential circular references
  2018-09-11 13:46:05  [ localhost-startStop-1:2538 ] - [ DEBUG ]  Processing injected method of bean 'BImpl': AutowiredFieldElement for private xyz.jangle.service.A xyz.jangle.service.impl.BImpl.a
  2018-09-11 13:46:05  [ localhost-startStop-1:2538 ] - [ DEBUG ]  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2538 ] - [ DEBUG ]  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:05  [ localhost-startStop-1:2551 ] - [ DEBUG ]  Creating implicit proxy for bean 'AImpl' with 0 common interceptors and 5 specific interceptors
  2018-09-11 13:46:06  [ localhost-startStop-1:2553 ] - [ DEBUG ]  Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [xyz.jangle.service.impl.AImpl@6b5912f2]
  2018-09-11 13:46:06  [ localhost-startStop-1:2557 ] - [ DEBUG ]  Returning eagerly cached instance of singleton bean 'AImpl' that is not fully initialized yet - a consequence of a circular reference
  2018-09-11 13:46:06  [ localhost-startStop-1:2557 ] - [ DEBUG ]  Autowiring by type from bean name 'BImpl' to bean named 'AImpl'
  2018-09-11 13:46:06  [ localhost-startStop-1:2558 ] - [ DEBUG ]  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:06  [ localhost-startStop-1:2558 ] - [ DEBUG ]  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
  2018-09-11 13:46:06  [ localhost-startStop-1:2560 ] - [ DEBUG ]  Creating implicit proxy for bean 'BImpl' with 0 common interceptors and 5 specific interceptors
  2018-09-11 13:46:06  [ localhost-startStop-1:2561 ] - [ DEBUG ]  Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [xyz.jangle.service.impl.BImpl@72389dee]
  2018-09-11 13:46:06  [ localhost-startStop-1:2562 ] - [ DEBUG ]  Finished creating instance of bean 'BImpl'
  2018-09-11 13:46:06  [ localhost-startStop-1:2562 ] - [ DEBUG ]  Autowiring by type from bean name 'AImpl' to bean named 'BImpl'
  2018-09-11 13:46:06  [ localhost-startStop-1:2563 ] - [ DEBUG ]  Finished creating instance of bean 'AImpl'
  2018-09-11 13:46:06  [ localhost-startStop-1:2563 ] - [ DEBUG ]  Returning cached instance of singleton bean 'BImpl'

行号:

1、告知所有的bean

2、开始创建AImpl对象

19、发现有属性(依赖)需要注入,即B对象。

20、缓存AImpl对象(以应对可能存在的循环依赖问题)

21、准备注入B依赖

30、开始创建BImpl(B的实现)

34、缓存BImpl对象(以应对可能存在的循环依赖问题)

35、发现有属性(依赖)需要注入,即A对象。(A对象未完全实例化,但对象存在,并进行了缓存,存在指向的地址。)

41、将AImpl(A的实现)注入BImpl对象。(虽然AImpl未完全实例化,但已经可以使用,不理解可以了解下对象创建过程

导读:https://www.cnblogs.com/chenyangyao/p/5296807.html 

虽然觉得这个写的不是很完美,但图文并茂已经很不错了。

46、完成BImpl对象的创建。

47、将BImpl(B的实现)注入AImpl对象。

48、完成AImpl对象的创建。

---   中间夹杂了一些内容的创建为AOP动态代理相关的内容(因为我写的切面对所有的service进行了干预)

创建action

package xyz.jangle.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import xyz.jangle.model.User;
import xyz.jangle.service.A;
import xyz.jangle.service.UserService;

@Controller
@RequestMapping("/mvc")
public class Hello {
	// 养成一种习惯,将项目的控制层对象,对应的视图,视图所用的js文件,三个名称进行统一。用Ctrl+R查找时可以很方便地找到他们
	@Autowired
	private A a;
	
	@RequestMapping("/testAb")
	public ModelAndView testAB() {
		a.testA();
		return new ModelAndView("hello");
	}

}

测试http://127.0.0.1/mvc/testAb执行结果:

[DEBUG] 2018-09-11 18:02:15,296 method:org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:845)
DispatcherServlet with name 'springmvc' processing GET request for [/mvc/testAb]
  [DEBUG] 2018-09-11 18:02:15,301 method:org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:297)
Looking up handler method for path /mvc/testAb
  [DEBUG] 2018-09-11 18:02:15,305 method:org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:302)
Returning handler method [public org.springframework.web.servlet.ModelAndView xyz.jangle.action.Hello.testAB()]
  [DEBUG] 2018-09-11 18:02:15,305 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:247)
Returning cached instance of singleton bean 'hello'
  [DEBUG] 2018-09-11 18:02:15,306 method:org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:931)
Last-Modified value for [/mvc/testAb] is: -1
  [DEBUG] 2018-09-11 18:02:15,326 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:247)
Returning cached instance of singleton bean 'executeIntercept'
  around befroe
before execute to do something
aaa
around befroe
before execute to do something
bbb
around after
after execute to do something
afterReturning execute to do something
around after
after execute to do something
afterReturning execute to do something
[DEBUG] 2018-09-11 18:02:15,331 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1610)
Invoking afterPropertiesSet() on bean with name 'hello'

猜你喜欢

转载自blog.csdn.net/Bof_jangle/article/details/82629698