spring学习48-自动装配中定义的bean的作用域

简介: spring学习48-自动装配中定义的bean的作用域
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"/>
     </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());
         }
     }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());
         }
     }运行结果
    NotePad的构造函数com.geyao.demo.NotePad@117159c0
     NotePad的构造函数com.geyao.demo.Notepad2@3cce5371
     NotePad的构造函数com.geyao.demo.Notepad2@480d3575
     false
相关文章
|
9月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
644 26
|
8月前
|
JSON Java 数据格式
微服务——SpringBoot使用归纳——Spring Boot中的全局异常处理——定义返回的统一 json 结构
本课主要讲解Spring Boot中的全局异常处理方法。在项目开发中,各层操作难免会遇到各种异常,若逐一处理将导致代码耦合度高、维护困难。因此,需将异常处理从业务逻辑中分离,实现统一管理与友好反馈。本文通过定义一个简化的JsonResult类(含状态码code和消息msg),结合全局异常拦截器,展示如何封装并返回标准化的JSON响应,从而提升代码质量和用户体验。
226 0
|
11月前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
296 12
|
11月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
400 12
|
11月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
357 6
|
4月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
882 0
|
5月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
620 0
|
1月前
|
JavaScript Java Maven
【SpringBoot(二)】带你认识Yaml配置文件类型、SpringMVC的资源访问路径 和 静态资源配置的原理!
SpringBoot专栏第二章,从本章开始正式进入SpringBoot的WEB阶段开发,本章先带你认识yaml配置文件和资源的路径配置原理,以方便在后面的文章中打下基础
273 3
|
1月前
|
Java 测试技术 数据库连接
【SpringBoot(四)】还不懂文件上传?JUnit使用?本文带你了解SpringBoot的文件上传、异常处理、组件注入等知识!并且带你领悟JUnit单元测试的使用!
Spring专栏第四章,本文带你上手 SpringBoot 的文件上传、异常处理、组件注入等功能 并且为你演示Junit5的基础上手体验
829 3
|
8月前
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
392 0

热门文章

最新文章