spring两种从ioc容器中获取bean实例的方法

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

spring两种从ioc容器中获取bean实例的方法.juint,@test

两种区别仅在junit测试
插播:junit和@test

@Test的使用 是该方法可以不用main方法调用就可以测试出运行结果,是一种测试方法
一般函数都需要有main方法调用才能执行,注意被测试的方法必须是public修饰的
JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。

步骤1:写一个java类
package person;

public class person01 {
	private String lastname;
	private int aage;
	private String email;
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public int getAage() {
		return aage;
	}
	public void setAage(int aage) {
		this.aage = aage;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "person01 [lastname=" + lastname + ", aage=" + aage + ", email=" + email + "]";
	}

}

在这里插入图片描述

步骤二:写springxml文件进行注册实例并赋值
<?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="per01" class="person.person01"> 
<property name="aage" value="18"></property></bean> -->
<!-- perxon02 -->
<bean id="per02" class="person.person01"> 
<property name="aage" value="18"></property>
<property name="email" value="17695455099@163.com"></property>
</bean>
<!-- csdn -->
</beans>
   

在这里插入图片描述

步骤三:写测试test使用juint4测试两种方法
package persontest;

import static org.junit.Assert.*;
import org.junit.Assert.*;

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

import person.person01;

public class testioc {

	private ApplicationContext ioc=new ClassPathXmlApplicationContext("hellospring01.xml");
	/* 通过类名获取bean实例*/ 
	@Test
	public void test02()
	{
		person01 bean =  ioc.getBean(person01.class);
		System.out.print(bean);
	}
	@Test
	/* 通过id获取bean实例*/
	public void test() {
		//fail("Not yet implemented");
		ApplicationContext ioc=new ClassPathXmlApplicationContext("hellospring01.xml");

		person01 bean = (person01) ioc.getBean("per02");
		System.out.print(bean);
	}

}

在这里插入图片描述
uiui干啥的忘了(好像没用)

关于SpringBoot获取IOC容器中注入的Bean(推荐) 本文通过实例代码给大家详解了springboot获取ioc容器中注入的bean问题,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧 立即下载

相关推荐

SpringIOC容器的简单实现、获取bean的三种方式

SpringIOC容器的简单实现、获取bean的三种方式

qq_42671904的博客 1692

Spring IoC获取对象的方式

3、context.getBean(String name, Class<T> requiredType):根据Bean名称和类型获取Bean。4、context.getBeansOfType(Class<T> type):根据类型获取容器中所有实现该类型的Bean。1、context.getBean(Class<T> requiredType):根据类型获取Bean。2、context.getBean(String name):根据名称获取Bean。【案例】IoC获取对象示例。

qq_54245389的博客 581

SSM整合之spring笔记(IOC 获取bean的三种方式)(P063—P069)

SSM整合之spring笔记(IOC 获取bean的三种方式)(P063—P069)

m0_59281987的博客 968

获取ioc容器bean的三种方式

根据bean的id获取 // 1.获取容器 ApplicationContext ioc= new ClassPathXmlApplicationContext("ioc.xml"); @Test public void testIoc1(){ // 2.获取bean Person person=(Person) ioc.ge...

stone的博客 2043

获取bean的三种方式和注意事项

获取bean的三种方式和注意事项

weixin_52385232的博客 2405

Spring学习及总结05】从IOC容器获取bean对象

文章目录1.根据bean的id从IOC容器获取bean对象2.根据bean的class类型从IOC容器获取bean对象3.总结 首先写一个实体类: @Data @AllArgsConstructor @NoArgsConstructor public class Person { private String lastName; private int age; pri...

你今天真好看呀 1872

获取Spring IOC容器bean对象

Spring常用的开发框架有SpringMVC和SpringBoot,很多时候后都是用@Component,@Service等注解类,或者在Spring配置文件中声明bean,使用的时候,在通过 @Resource或者 @Resource注解获取对象,但在某些情况下,无法使用自动注解获取对象,这个时候就需要从IOC容器获取bean对象。

@Xiao.Ran的博客 2612

Spring IOC容器Bean 管理 第1关:使用 Spring IOC 容器获取 Bean 信息

Spring IOC容器Bean 管理 第1关:使用 Spring IOC 容器获取 Bean 信息

于建章的博客 3134

SpringIoC(容器配置、Spring坐标导入、获取bean)

这篇博客主要学习了IoC思想怎么去实现。包括导入Spring 的坐标、写配置文件、获取 IoC 容器获取 bean 对象 等等......

岁岁岁平安的博客 1344

2_SpringIOC容器获取组件Bean

本文介绍了如何在Spring框架中创建IOC容器获取Bean组件。首先,定义了一个接口TestDemo及其实现类HappyComponent。接着,通过XML配置文件spring-03.xml将HappyComponent类配置为SpringBean。然后,通过ClassPathXmlApplicationContext类创建IOC容器,并加载配置文件。最后,演示了三种从IOC容器获取Bean的方式:通过Bean ID、Bean ID和类型、以及仅通过类型。文章还强调了根据类型获取Bean时,IOC

录大大i的博客 575

springboot返回json对象获取spring ioc容器里面所有的bean

springboot启动后,如何获取已经放入spring IOC容器的所有bean,不特指springboot,即便没有使用springboot,只使用spring的话,也会有这种功能性的要求的,其实要想获得spring IOC容器里所有的bean,首先要考虑怎么得到spring IOC容器applicationContext,然后才能从applicationContext中通过方法getBeanNamesForType(Class<?> type)获取所有的bean的名字,代码如下所示:

xiaozhuangyumaotao的博客 827

Spring IOC 容器源码分析 - 获取单例 bean

public Object getBean(String name) throws BeansException { // getBean 是一个空壳方法,所有的逻辑都封装在 doGetBean 方法中 return doGetBean(name, null, null, false); } protected <T> T doGetBean( fin...

381

Spring IoC容器Bean管理9:使用XML方式实现Spring IoC四:从IoC容器获取Bean;<bean>标签【id属性】和【name属性】的区别;

本篇博客主要内容: (1)在前三篇博客中,介绍三种创建对象的方式;本篇博客就介绍如何从IoC容器中提取对象; (2)然后,本篇博客也附带介绍了<bean>标签的id属性和name属性; 说明: (1)本篇博客沿用【Spring IoC容器Bean管理6:】和【Spring IoC容器Bean管理7:】和【Spring IoC容器Bean管理8:】中的代码; (2)本篇博客的内容,其实不多,用的过了熟练后,就会发现本篇博客的内容很少; 目录 一:从IoC容器获取Be..

小枯林的博客 522

使用SpringtUtil获取Spring IoC容器中的Bean

 功能:    方便获取Spring IoC容器中的Bean,调用方法:getBean(String name)     import org.springframework.context.ApplicationContext;    import org.springframework.context.support.ClassP

Jenny的人生旅途 1216

spring 获取ioc容器,从容器获取bean

/ 初始化 Spring 容器。// ... 获取和使用 Bean ...//指定bean 所在的包。

面朝大海,春暖花开 618

spring通用获取ioc容器中配置的bean的工具类

   一 、 在web应用中,我们常用ApplicationContext接口来获取ioc容器里面的资源,ApplicationContext是BeanFactory的子接口之一,并且对BeanFactory做了许多扩展,所以在绝大部分工作场景下,都会使用ApplicationContext作为ioc容器。       可以通过ApplicationContext的一个子类——ClassPath...

qq_31647257的博客 719

Spring】初始化Spring IoC容器(非Web应用),并获取Bean

参考文章 Introduction to the Spring IoC container and beans BeanFactory 和ApplicationContext(Bean工厂和应用上下文) Spring ApplicationContext - Resource leak: 'context' is never closed 版本说明 JDK 1.7 ...

dongjizheng9270的博客 126

spring1-test2-根据bean的类型从IOC容器获取bean实例

根据Person这个类型从容器获取到对象的值,如果该容器中有多个注册对象的话,那么就会出现错误。可以尝试使用第二种方法。 @Test public void test02(){ /** * 如果IOC容器中的这个类型的bean有多个,那么按照这个类型找会失败。 */ ApplicationContext ioc = new ClassPathXmlAppl...

Shen_R的博客 228

SpringIOC容器注册和获取Bean实例方法

Spring中核心的功能就是IoC和AOP,本文主要主要讲解IOC实例注册和使用的方法Spring IoC容器 Spring IoC容器可以容纳我们所开发的各种Bean,并且可以从中获取各种注册在Spring IoC容器里的BeanSpring IoC容器的设计主要是基于BeanFactory和ApplicationContext两个接口,其中ApplicationContext是BeanFactory的子接口之一。换句话说BeanFactory是Spring IoC容器所定义的最底层接口,而Ap

IT__learning的博客 1635
上一篇: jquery的基本概述
下一篇: springmvc入门
危笑qwq
博客等级 码龄7年 22粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值