spring5上下文对象创建方式

spring5上下文对象创建方式

前置说明

测试环境使用的是 springboot 项目 , 没有 web 依赖

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

项目示例

image-20220919214349880

ClassPathApplicationContext

package com.example.xmlcontext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringXmlContextTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        BBean bBean1 = ac.getBean(BBean.class);
        System.out.println(bBean1);

        BBean bBean2 = (BBean) ac.getBean("bBean");
        System.out.println(bBean2);

        System.out.println("获取的两个对象是否相等  :  "+ (bBean1==bBean2));
    }
}

GenericXmlApplicationContext

package com.example.gemerxml;

import org.springframework.context.support.GenericXmlApplicationContext;

public class SpringGenericXmlTest {
    public static void main(String[] args) {
        GenericXmlApplicationContext ac = new GenericXmlApplicationContext("classpath:applicationContextGen.xml");
        DBean bean = ac.getBean(DBean.class);
        System.out.println(bean);
    }
}

AnnotationConfigApplicationContext(1)

基于 this 进行扫描

package com.example.annotationcontext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example.annotationcontext")
public class SpringAnnotationcontextTest1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringAnnotationcontextTest1.class);
        A bean = ac.getBean(A.class);
        System.out.println(bean);
    }
}

AnnotationConfigApplicationContext(2)

基于扫描包进行扫描

package com.example.annotationscanner;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringAnnotationScanner {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext("com.example.annotationscanner");
        CBean bean = ac.getBean(CBean.class);
        System.out.println(bean);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值