spring学习49-延迟加载

简介: spring学习49-延迟加载
pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
         <modelVersion>4.0.0</modelVersion>
         <groupId>com.geyao</groupId>
         <artifactId>spring01geyao</artifactId>
         <version>1.0-SNAPSHOT</version>
         <dependencies>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-context</artifactId>
                 <version>4.3.13.RELEASE</version>
             </dependency>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-test</artifactId>
                 <version>3.2.0.RELEASE</version>
             </dependency>
             <dependency>
                 <groupId>log4j</groupId>
                 <artifactId>log4j</artifactId>
                 <version>1.2.17</version>
             </dependency>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>
                 <version>4.12</version>
             </dependency>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-test</artifactId>
                 <version>4.3.4.RELEASE</version>
                 <scope>test</scope>
             </dependency>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-test</artifactId>
                 <version>4.3.4.RELEASE</version>
                 <scope>test</scope>
             </dependency>
         </dependencies>
     </project>log4j.properties
    ### 设置###
     log4j.rootLogger = ERROR,stdout
     ### 输出信息到控制抬 ###
     log4j.appender.stdout = org.apache.log4j.ConsoleAppender
     log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
     log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
     log4j.category.org.springframework.beans.factory=ERRORapplicationContext.xml
    <?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
         <!--
         bean元素:描述当前的对象需要由spring容器管理
         id属性:标识对象 未来在应用程序中可以根据id获取对象
         class对象:被管理的对象的全名
        -->
         <context:component-scan base-package="com.geyao.demo"></context:component-scan>
         <bean id="notepad" class="com.geyao.demo.NotePad" scope="singleton" lazy-init="true"/>
     </beans>notepad类
    package com.geyao.demo;
     public class NotePad {
         public NotePad() {
             super();
             System.out.println("NotePad的构造函数"+this.toString());
         }
     }notepad2类
    package com.geyao.demo;
     import org.springframework.beans.factory.config.ConfigurableBeanFactory;
     import org.springframework.context.annotation.Scope;
     import org.springframework.stereotype.Component;
     @Component
     //@Scope("prototype")
     //@Scope(scopeName = "prototype")
     @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
     public class Notepad2 {
         public Notepad2() {
             super();
             System.out.println("NotePad的构造函数"+this.toString());
         }
     }notepad3类
    package com.geyao.demo;
     import org.springframework.beans.factory.config.ConfigurableBeanFactory;
     import org.springframework.context.annotation.Scope;
     import org.springframework.stereotype.Component;
     public class Notepad3 {
         public Notepad3() {
             super();
             System.out.println("NotePad的构造函数"+this.toString());
         }
     }Appconfig类
    package com.geyao.demo;
     import org.springframework.context.annotation.Bean;
     import org.springframework.context.annotation.Configuration;
     import org.springframework.context.annotation.Scope;
     @Configuration
     public class Appconfig {
         @Bean
         @Scope
         public Notepad3 notepad3(){
             return new Notepad3();
         }
     }Notepadtest类
    package com.geyao.demo;
     import org.junit.Test;
     import org.springframework.context.support.ClassPathXmlApplicationContext;
     //无论我们是否去主动获取对象,spring上下文刚加载就会创建对象
     //无论获取多少次,都是统一对象
     //
     public class NotepadTest {
         @Test
         public void test01(){
             ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            NotePad notePad1= (NotePad)context.getBean("notepad");
             NotePad notePad2= (NotePad)context.getBean("notepad");
             System.out.println(notePad1=notePad2);
         }
     }notepadtestAuto类
    package com.geyao.demo;
     import org.junit.Test;
     import org.junit.runner.RunWith;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.test.context.ContextConfiguration;
     import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     @RunWith(SpringJUnit4ClassRunner.class)
     @ContextConfiguration("classpath:applicationContext.xml")
     public class NotePadTestAuto {
         @Autowired
         private NotePad notePad1;
         @Autowired
         private NotePad notePad2;
         @Test
         public void test01(){
             System.out.println(notePad1==notePad2);
         }
     }notepadtestt类
    package com.geyao.demo;
     import org.springframework.beans.factory.config.ConfigurableBeanFactory;
     import org.springframework.context.annotation.Scope;
     import org.springframework.stereotype.Component;
     @Component
     //@Scope("prototype")
     //@Scope(scopeName = "prototype")
     @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
     public class Notepad2 {
         public Notepad2() {
             super();
             System.out.println("NotePad的构造函数"+this.toString());
         }
     }notepad3test类
    package com.geyao.demo;
     import org.junit.Test;
     import org.junit.runner.RunWith;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.test.context.ContextConfiguration;
     import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     @RunWith(SpringJUnit4ClassRunner.class)
     @ContextConfiguration(classes=Appconfig.class)
     public class Notepad3Test {
         @Autowired
         private Notepad3 notePad1;
         @Autowired
         private Notepad3 notePad2;
         @Test
         public void test01(){
             System.out.println(notePad1==notePad2);
         }
     }运行结果
    NotePad的构造函数com.geyao.demo.Notepad3@3e2059ae
     NotePad的构造函数com.geyao.demo.Notepad2@4397ad89
     NotePad的构造函数com.geyao.demo.Notepad2@768fc0f2
     false
相关文章
|
1月前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
8月前
|
监控 Java 应用服务中间件
微服务——SpringBoot使用归纳——为什么学习Spring Boot
本文主要探讨为什么学习Spring Boot。从Spring官方定位来看,Spring Boot旨在快速启动和运行项目,简化配置与编码。其优点包括:1) 良好的基因,继承了Spring框架的优点;2) 简化编码,通过starter依赖减少手动配置;3) 简化配置,采用Java Config方式替代繁琐的XML配置;4) 简化部署,内嵌Tomcat支持一键式启动;5) 简化监控,提供运行期性能参数获取功能。此外,从未来发展趋势看,微服务架构逐渐成为主流,而Spring Boot作为官方推荐技术,与Spring Cloud配合使用,将成为未来发展的重要方向。
323 0
微服务——SpringBoot使用归纳——为什么学习Spring Boot
|
5月前
|
安全 Java 数据库
Spring Boot 框架深入学习示例教程详解
本教程深入讲解Spring Boot框架,先介绍其基础概念与优势,如自动配置、独立运行等。通过搭建项目、配置数据库等步骤展示技术方案,并结合RESTful API开发实例帮助学习。内容涵盖环境搭建、核心组件应用(Spring MVC、Spring Data JPA、Spring Security)及示例项目——在线书店系统,助你掌握Spring Boot开发全流程。代码资源可从[链接](https://pan.quark.cn/s/14fcf913bae6)获取。
835 2
|
7月前
|
Java Spring
Spring框架的学习与应用
总的来说,Spring框架是Java开发中的一把强大的工具。通过理解其核心概念,通过实践来学习和掌握,你可以充分利用Spring框架的强大功能,提高你的开发效率和代码质量。
192 20
|
Java Maven Spring
springboot学习一:idea社区版本创建springboot项目的三种方式(第三种为主)
这篇文章介绍了在IntelliJ IDEA社区版中创建Spring Boot项目的三种方法,特别强调了第三种方法的详细步骤。
10962 0
springboot学习一:idea社区版本创建springboot项目的三种方式(第三种为主)
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
286 9
|
数据采集 监控 Java
SpringBoot日志全方位超详细手把手教程,零基础可学习 日志如何配置及SLF4J的使用......
本文是关于SpringBoot日志的详细教程,涵盖日志的定义、用途、SLF4J框架的使用、日志级别、持久化、文件分割及格式配置等内容。
853 2
SpringBoot日志全方位超详细手把手教程,零基础可学习 日志如何配置及SLF4J的使用......
|
存储 开发框架 Java
什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
文章详细介绍了Spring、IOC、DI的概念和关系,解释了控制反转(IOC)和依赖注入(DI)的原理,并提供了IOC的代码示例,阐述了Spring框架作为IOC容器的应用。
862 1
什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
|
前端开发 Java 数据库
SpringBoot学习
【10月更文挑战第7天】Spring学习
183 9
|
XML Java 数据格式
Spring学习
【10月更文挑战第6天】Spring学习
119 1

热门文章

最新文章