首页 > 其他分享 >Spring 配置 Bean 实例化有哪些方式?

Spring 配置 Bean 实例化有哪些方式?

时间:2023-08-06 16:04:49浏览次数:29  
标签:name Spring age public Bean context 化有 import class

All you need is the plan, the road map, and the courage to press on to your destination. 你所需要的只是计划,路线图,以及朝着目标前进的勇气。

Spring 实例化 bean 的方式

  • 构造器的方式
  • 静态工厂方式
  • 实例化工厂方式

1、使用构造器实例化bean 也是最常用的

ps:注意:Hello的无参构造器必须存在

【application.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="test-demo" class="com.jarvan.spring.Demo"/>
</beans>

【Demo:】
public class Demo {
    public Demo() {
        System.out.println("demo init .....");
    }
}
【Test:】
package com.jarvan.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application.xml"})
public class DemoTest {

    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Demo demo = (Demo) context.getBean("test-demo");
        System.out.println(demo.getName());
    }
}

2、使用静态工厂方式实例化Bean

public class Demo1Factory {
    public static Demo1 getInstance() {
        return new Demo1();
    }
}

class Demo1 {
    public Demo1() {
        System.out.println("demo1 init .....");
    }
}

<bean id="test-demo1" class="com.jarvan.spring.Demo1Factory" factory-method="getInstance"/>

3、使用实例工厂实例化Bean

public class Demo2Factory {
    public Demo2 getDemo2() {
        return new Demo2();
    }
}

class Demo2{
    public Demo2() {
        System.out.println("Demo2 init ....");
    }
}

<bean id="test-demo2" factory-bean="demo2Factory" factory-method="getDemo2"/>

4、用setter 方式

<bean id="test-demo3" class="com.jarvan.spring.Demo3" >
        <property name="age" value="22"/>
        <property name="name" value="jarvan"/>
    </bean>
    
public class Demo3 {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Demo3{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

@Test
    public void test3() {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Demo3 demo = (Demo3) context.getBean("test-demo3");
        System.out.println(demo.toString());
    }

整体测试项目

upload successful

标签:name,Spring,age,public,Bean,context,化有,import,class
From: https://blog.51cto.com/u_11906056/6984830

相关文章

  • Cannot resolve org.springframework.boot:spring-boot-starter-parent:2.4.3
    环境IDEA:2020-1-3MAVEN:3.6.2Spring-boog:2.3.4问题描述在pom.xml中写入所需依赖后,通过MAVEN加载依赖无法完成,报错异常信息Cannotresolveorg.springframework.boot:spring-boot-starter-parent:2.4.3问题原因应该是本地仓库中有之前由于网速不好未下载完成的文件,M......
  • 记录一下 搭建springboot,springCloud,springCloudAlibaba,nacos
    1,首先创建一个空项目里面有两个服务一个提供者一个调用者 2,父工程的使用依赖 以及springBoot的父依赖//springboot父工程<parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId>......
  • 在 Spring Boot 应用程序中,可以通过命令行参数或环境变量来指定配置文件和日志文件的
    1、使用命令行参数java-jaryour-app.jar--spring.config.location=file:/path/to/application.properties--logging.file=/path/to/logs/your-app.log在上述命令中,--spring.config.location参数用于指定配置文件的位置,--logging.file参数用于指定日志文件的位置。2、使......
  • SpringBoot对接OpenAI
    SpringBoot对接OpenAI随着人工智能技术的飞速发展,越来越多的开发者希望将智能功能集成到自己的应用中,以提升用户体验和应用的功能。OpenAI作为一家领先的人工智能公司,提供了许多先进的自然语言处理和语言生成模型,其中包括深受欢迎的GPT-3.5模型。本文将介绍如何利用SpringBoot框......
  • SpringBoot - IOC&DI
    目录三层架构三层架构controller:控制层,接收前端请求,对请求进行处理,并响应数据service:业务逻辑层,处理具体的业务逻辑dao:数据访问层(DataAccessObject)(持久层),负责访问操作,包括数据得增删改查员工案例重构:controller:packagecom.chuangzhou.controller;importcom.chu......
  • 记录小知识 springboot,maven创建的多模块 子模块无法使用父类版本
    使用依赖时发现依赖有问题,回来检查发现没有加springboot父工程检查父模块是否加入父标签:只需要在父模块中添加一次就可以了<parent><groupId>org.springframework.boot</groupId><cartifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELE......
  • Spring Boot&Vue3前后端分离实战wiki知识库系统<十一>--文档管理功能开发三
    文档内容的显示:在上一次SpringBoot&Vue3前后端分离实战wiki知识库系统<十>--文档管理功能开发二文档管理模块还差文档的显示木有完成,所以接下来先将这块模块给收尾了。增加单独获取内容的接口:概述:在前端页面文档查询时,只查询了文档的基本信息,其中文档的富文本内容是木有带出来的:当......
  • Springboot中怎么选择性使用thymeleaf进行渲染?
    SpringBoot默认支持多种模板引擎,包括Thymeleaf。如果你想选择性地使用Thymeleaf进行渲染,这基本上取决于你的Controller的实现。以下是一个基本示例:首先,确保你的SpringBoot项目已经添加了Thymeleaf的依赖。在你的pom.xml文件中,你应该看到类似以下的内容<dependency>......
  • Spring Boot + Vue3前后端分离实战wiki知识库系统<十一>--文档管理功能开发三
    文档内容的显示:在上一次https://www.cnblogs.com/webor2006/p/17510360.html文档管理模块还差文档的显示木有完成,所以接下来先将这块模块给收尾了。增加单独获取内容的接口:概述:在前端页面文档查询时,只查询了文档的基本信息,其中文档的富文本内容是木有带出来的:当然也不可能......
  • Spring Boot问题总结
    访问无响应指定包@ComponentScan(basePackages="com.example")浏览器访问跨域问题将所有请求全部放行而且每个请求都要加@CrossOrigin(origins="*")get返回htmlhtml放后端,首先dependency要依赖thymeleaf<dependency><groupId>org.springframe......