spring学习(49):javaconfig里面定义bean的作用域

本文探讨了在Spring框架的JavaConfig配置中如何定义Bean的作用域,包括其在不同场景下的应用和配置方式,并通过实例展示了Bean的生命周期管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录结构

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=ERROR

applicationContext.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());
    }

}

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.NotePad@117159c0
NotePad的构造函数com.geyao.demo.Notepad2@3cce5371
NotePad的构造函数com.geyao.demo.Notepad2@480d3575
false

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小歌谣

放弃很容易 但是坚持一定很酷

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值