UserService.java
package com.itranswarp.learnjava.service; import java.time.ZoneId; import org.springframework.stereotype.Component; @Component public class UserService { public ZoneId zoneId = ZoneId.systemDefault(); public UserService() { System.out.println("UserService(): init..."); System.out.println("UserService(): zoneId = " + this.zoneId); } public ZoneId getZoneId() { return zoneId; } }
MailService.java
public String sendMail() { // null: ZoneId zoneId = userService.getZoneId(); String dt = ZonedDateTime.now(zoneId).toString(); return "Hello, it is " + dt; }
运行结果:
13:43:49.819 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@10a035a0 13:43:49.831 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' 13:43:49.869 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\projects\study\java_pro\spring-aop-field\target\classes\com\itranswarp\learnjava\LoggingAspect.class] 13:43:49.869 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\projects\study\java_pro\spring-aop-field\target\classes\com\itranswarp\learnjava\service\MailService.class] 13:43:49.870 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\projects\study\java_pro\spring-aop-field\target\classes\com\itranswarp\learnjava\service\UserService.class] 13:43:49.930 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor' 13:43:49.931 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' 13:43:49.932 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' 13:43:49.933 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator' 13:43:50.007 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'appConfig' 13:43:50.023 [main] DEBUG org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory - Found AspectJ method: public void com.itranswarp.learnjava.LoggingAspect.doAccessCheck() 13:43:50.087 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'loggingAspect' 13:43:50.088 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'mailService' 13:43:50.094 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService' UserService(): init... UserService(): zoneId = Asia/Shanghai [Before] do access check... Hello, it is 2022-08-23T13:43:50.134103900+08:00[Asia/Shanghai]
Sign in to make a reply
jasmine
UserService.java
MailService.java
运行结果: