From 3794c0e8726daec943ef95f758677cf6a76619a3 Mon Sep 17 00:00:00 2001 From: xuchengsheng Date: Tue, 10 Oct 2023 22:57:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Espring-annotation-lazy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-annotation/pom.xml | 1 + .../spring-annotation-lazy/pom.xml | 14 +++++++++++ .../java/com/xcs/spring/LazyApplication.java | 25 +++++++++++++++++++ .../main/java/com/xcs/spring/bean/MyBean.java | 12 +++++++++ .../xcs/spring/config/MyConfiguration.java | 23 +++++++++++++++++ .../com/xcs/spring/service/MyService.java | 17 +++++++++++++ 6 files changed, 92 insertions(+) create mode 100644 spring-annotation/spring-annotation-lazy/pom.xml create mode 100644 spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/LazyApplication.java create mode 100644 spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/bean/MyBean.java create mode 100644 spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/config/MyConfiguration.java create mode 100644 spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/service/MyService.java diff --git a/spring-annotation/pom.xml b/spring-annotation/pom.xml index 9679c3c..dc2f627 100644 --- a/spring-annotation/pom.xml +++ b/spring-annotation/pom.xml @@ -19,6 +19,7 @@ spring-annotation-propertySource spring-annotation-componentScan spring-annotation-dependsOn + spring-annotation-lazy \ No newline at end of file diff --git a/spring-annotation/spring-annotation-lazy/pom.xml b/spring-annotation/spring-annotation-lazy/pom.xml new file mode 100644 index 0000000..8eae53f --- /dev/null +++ b/spring-annotation/spring-annotation-lazy/pom.xml @@ -0,0 +1,14 @@ + + + + spring-annotation + com.xcs.spring + 0.0.1-SNAPSHOT + + + 4.0.0 + spring-annotation-lazy + + \ No newline at end of file diff --git a/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/LazyApplication.java b/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/LazyApplication.java new file mode 100644 index 0000000..64964c2 --- /dev/null +++ b/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/LazyApplication.java @@ -0,0 +1,25 @@ +package com.xcs.spring; + +import com.xcs.spring.config.MyConfiguration; +import com.xcs.spring.service.MyService; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * @author xcs + * @date 2023年08月07日 16时21分 + **/ +public class LazyApplication { + + public static void main(String[] args) { + System.out.println("启动 Spring ApplicationContext..."); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); + System.out.println("完成启动 Spring ApplicationContext..."); + + System.out.println("准备获取MyService..."); + MyService myService = context.getBean(MyService.class); + System.out.println("成功获取MyService...-->" + myService); + + System.out.println("调用show方法..."); + myService.show(); + } +} diff --git a/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/bean/MyBean.java b/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/bean/MyBean.java new file mode 100644 index 0000000..87fad41 --- /dev/null +++ b/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/bean/MyBean.java @@ -0,0 +1,12 @@ +package com.xcs.spring.bean; + +public class MyBean { + + public MyBean() { + System.out.println("MyBean 的构造函数被调用了!"); + } + + public void show() { + System.out.println("hello world"); + } +} diff --git a/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/config/MyConfiguration.java b/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/config/MyConfiguration.java new file mode 100644 index 0000000..093b9a1 --- /dev/null +++ b/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/config/MyConfiguration.java @@ -0,0 +1,23 @@ +package com.xcs.spring.config; + +import com.xcs.spring.bean.MyBean; +import com.xcs.spring.service.MyService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; + +@Configuration +public class MyConfiguration { + + @Bean + @Lazy + public MyBean myBean(){ + System.out.println("MyBean 初始化了!"); + return new MyBean(); + } + + @Bean + public MyService myService(){ + return new MyService(); + } +} diff --git a/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/service/MyService.java b/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/service/MyService.java new file mode 100644 index 0000000..4fe18e8 --- /dev/null +++ b/spring-annotation/spring-annotation-lazy/src/main/java/com/xcs/spring/service/MyService.java @@ -0,0 +1,17 @@ +package com.xcs.spring.service; + +import com.xcs.spring.bean.MyBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; + +public class MyService { + + @Autowired + @Lazy + private MyBean myBean; + + public void show() { + System.out.println("@Lazy MyBean = " + myBean.getClass()); + myBean.show(); + } +}