SpringBoot 如何从容器中获取对象
作者:飘零未归人 发布时间:2023-12-06 08:20:40
如何从容器中获取对象
有时候在项目中,我们会自己创建一些类,类中需要使用到容器中的一些类。方法是新建类并实现ApplicationContextAware 接口,在类中建立静态对象 ApplicationContext 对象,这个对象就如同xml配置中的 applicationContext.xml,容器中类都可以获取到。
例如@Service、 @Component、@Repository、@Controller 、@Bean 标注的类都能获取到。
/**
* 功能描述:Spring Bean 管理类
*
*/
@Component
public class SpringContextUtils implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
try{
return getApplicationContext().getBean(clazz);
}catch (Exception e){
return null;
}
}
/**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
SpringBoot中的容器
容器功能
1、组件添加
(1)主要注解
@Configuration
告诉SpringBoot这是一个配置类 == 配置文件
注意:spring5.2以后@Configuration多了一个属性proxyBeanMethods,默认为true
@Configuration(proxyBeanMethods = true)
proxyBeanMethods
:代理bean的方法Full(proxyBeanMethods = true)
、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】 外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象Lite(proxyBeanMethods = false)
【每个@Bean方法被调用多少次返回的组件都是新创建的】组件依赖必须使用Full模式默认。其他默认是否Lite模式
● Full模式与Lite模式
○ 最佳实战
■ 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
■ 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
@Bean
给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
配置类里面使用@Bean标注在方法上给容器注册组件,默认是单实例的
配置类本身也是组件
(2) 基本使用
bean包:
Pet类:
/**
* 宠物
*/
public class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Pet(String name) {
this.name = name;
}
public Pet() {
}
@Override
public String toString() {
return "Pet{" +
"name='" + name + '\'' +
'}';
}
}
User类:
/*
用户
*/
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
config包:
MyConfig类
@Configuration(proxyBeanMethods = false)//告诉Spring这是一个配置类
public class MyConfig {
@Bean//给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01(){
return new User("zhangsan",18);
}
@Bean("tom")
public Pet tomcatPet(){
return new Pet("tomcat");
}
}
controller包:
MainApplication类
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com")
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
//3、从容器中获取组件
MyConfig bean = run.getBean(MyConfig.class);
System.out.println(bean);
//如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
//保持组件单实例
User user = bean.user01();
User user1 = bean.user01();
System.out.println("组件为:"+(user == user1));
}
}
结果
(3)补充 @Import
给容器导入一个组件
必须写在容器中的组件上
* @Import({User.class, DBHelper.class})
* 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
*
*
*
*/
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
}
@Configuration测试代码如下
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com")
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
//3、从容器中获取组件
MyConfig bean = run.getBean(MyConfig.class);
System.out.println(bean);
//如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
//保持组件单实例
User user = bean.user01();
User user1 = bean.user01();
System.out.println("组件为:"+(user == user1));
//5、获取组件
String[] beanNamesForType = run.getBeanNamesForType(User.class);
System.out.println("======");
for (String s : beanNamesForType) {
System.out.println(s);
}
DBHelper bean1 = run.getBean(DBHelper.class);
System.out.println(bean1);
}
}
@Conditional
条件装配:满足Conditional指定的条件,则进行组件注入
ConditionalOnBean
:当容器中存在指定的bean组件时才干某些事情ConditionalOnMissingBean
:当容器中不存在指定的bean组件时才干某些事情ConditionalOnClass
:当容器中有某个类时才干某些事情ConditionalOnResource
:当项目的类路径存在某个资源时,才干什么事
=====================测试条件装配==========================
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
//@ConditionalOnBean(name = "tom")
@ConditionalOnMissingBean(name = "tom")
public class MyConfig {
@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(tomcatPet());
return zhangsan;
}
@Bean("tom22")
public Pet tomcatPet(){
return new Pet("tomcat");
}
}
测试:
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
boolean tom = run.containsBean("tom");
System.out.println("容器中Tom组件:"+tom);
boolean user01 = run.containsBean("user01");
System.out.println("容器中user01组件:"+user01);
boolean tom22 = run.containsBean("tom22");
System.out.println("容器中tom22组件:"+tom22);
}
2、原生配置文件引入(xml文件引入)
@ImportResource
导入资源
@ImportResource("classpath:beans.xml")//导入spring的配置文件
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false)//告诉Spring这是一个配置类
@ConditionalOnMissingBean(name = "tom")
public class MyConfig {
======================beans.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 https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="haha" class="com.atguigu.boot.bean.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
<bean id="hehe" class="com.atguigu.boot.bean.Pet">
<property name="name" value="tomcat"></property>
</bean>
</beans>
测试
======================测试=================
boolean haha = run.containsBean("haha");
boolean hehe = run.containsBean("hehe");
System.out.println("haha:"+haha);//true
System.out.println("hehe:"+hehe);//true
3、配置绑定
如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用;
(1) @Component + @ConfigurationProperties
properties文件
/**
* 只有在容器中的组件,才会拥有SpringBoot提供的强大功能
*/
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
private String brand;
private Integer price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
(2) @EnableConfigurationProperties + @ConfigurationProperties
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
@ConditionalOnMissingBean(name = "tom")
@ImportResource("classpath:beans.xml")
//@EnableConfigurationProperties(Car.class)
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
public class MyConfig {
来源:https://blog.csdn.net/qq_34484062/article/details/101282262


猜你喜欢
- 一、概述现实生活中,我们常会看到这样的一种集合:IP地址与主机名,身份证号与个人,系统用户名与系统用户对象等,这种一一对应的关系,就叫做映射
- 在 C# 语言中 StreamReader 类用于从流中读取字符串。它继承自 TextReader 类。StreamReader 类的构造方
- SpringMvc代码jar包commons-fileuploadcommons-iospring-mvc.xml配置<bean id
- 本文介绍了Android串口通信封装之OkUSB的示例代码,分享给大家。具体如下:Github传送门:OkUSBOkUSB一个简洁的Andr
- 环境介绍 IDEA我用的是2020.2Gradle 安装参考 Gradle安装配置我这安装的是6.6.1C:\Users\herion>
- 本文实例讲述了Java获得当前时间前指定几个小时具体时间的方法。分享给大家供大家参考,具体如下:package getBeforeHourD
- 一、概述平衡二叉树具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。这个方案很好的
- using Microsoft.Win32 ; 1.读取指定名称的注册表的值 &nbs
- 1.非静态成员变量当成员变量为非静态成员变量且对当前类进行实例化时,将会产生死循环例子:public class ConstructorCl
- filter类不能注入@Autowired变量问题描述项目中的登录是用了shiro以及filter * 。输入正确的账号密码之后却不能正常登
- 本文实例讲述了Java使用备忘录模式实现过关类游戏功能。分享给大家供大家参考,具体如下:一.模式定义备忘录模式,在不破坏封闭的前提下,捕获一
- Lambda,希腊字母λ,在C#编程语言中,被引入为Lambda表达式,表示为匿名函数(匿名方法)。编程时离不开函数,
- /* * 使用 C# 动态编译代码和执行 * 作者: yaob */ static void Main(string[] args) { /
- 一、首先编写一个工具类Hello:public class Hello { public static void say(Str
- Android镂空字体的实现效果图,感兴趣的朋友可以参考实现代码。效果图:记录一下...自定义TextViewpublic class Ho
- Java源码系列三-工具类Arrays今天分享java的源码的第三弹,Arrays这个工具类的源码。因为近期在复习数据结构,了解到Array
- 1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁。也就是如果有Synchr
- 在android中做图像镜像有很多方法,今天算是学习了! 两种方法如下: //方法一 Matrix matrix = new Matrix(
- 【1】首先我们定义一段假数据,这里以一个string为例字static void Main(string[] args){string da
- jpa EntityManager复杂查询概念EntityManager:EntityManager是JPA中用于增删改查的接口,它的作用相