Spring获取bean的一种方式

随便一百度,网上一大把,并且还不止一种。所以这里就只记录目前用的一种好了。

实现ApplicationContextAware接口 即可:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        if (SpringUtils.applicationContext == null) {
            SpringUtils.applicationContext = arg0;
        }
    }

    // 获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    // 通过name获取 Bean.
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    // 通过class获取Bean.
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    // 通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

使用时,直接SpringUtils.getBean()即可。

时间: 2024-07-28 16:34:20

Spring获取bean的一种方式的相关文章

spring 获取bean的几种方式

1.读取xml文件的方式,这种在初学入门的时候比较适用 . ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:tt.xml"); ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:tt.xml"); 2.继承spring的A

Spring在代码中获取bean的几种方式(转:http://www.dexcoder.com/selfly/article/326)

方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObjectSupport 方法四:继承自抽象类WebApplicationObjectSupport 方法五:实现接口ApplicationContextAware 方法六:通过Spring提供的ContextLoader 获取spring中bean的方式总结: 方法一:在初始化时保存Applicati

Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContext方式 我自己常用的方法: 读取一个文件1 //创建Spring容器 2 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); 3 //获取chinese 实例 4 Person p = ctx.g

Spring在代码中获取bean的几种方式

方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObjectSupport 方法四:继承自抽象类WebApplicationObjectSupport 方法五:实现接口ApplicationContextAware 方法六:通过Spring提供的ContextLoader 获取spring中bean的方式总结: 方法一:在初始化时保存Applicati

001-Spring在代码中获取bean的几种方式

一.概述 方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObjectSupport 方法四:继承自抽象类WebApplicationObjectSupport 方法五:实现接口ApplicationContextAware 方法六:通过Spring提供的ContextLoader 二.详细介绍 1.在初始化时保存ApplicationContext对

spring 装配bean的三种方式

这段时间在学习Spring,依赖注入DI和面向切面编程AOP是Spring框架最核心的部分.这次主要是总结依赖注入的bean的装配方式. 什么是依赖注入呢?也可以称为控制反转,简单的来说,一般完成稍微复杂的业务逻辑,可能需要多个类,会出现有些类要引用其他类的实例,也可以称为依赖其他类.传统的方法就是直接引用那个类对象作为自己的一个属性,但如果我们每次创建这个类的对象时,都会创建依赖的类的对象,还有如果那个类将来可能不用了,还需要到这个类去删除这个对象,那破坏了代码的复用性和导致高度耦合! 依赖注

Spring定义Bean的两种方式:和@Bean

前言:    Spring中最重要的概念IOC和AOP,实际围绕的就是Bean的生成与使用. 什么叫做Bean呢?我们可以理解成对象,每一个你想交给Spring去托管的对象都可以称之为Bean. 今天通过Spring官方文档来了解下,如何生成bean,如何使用呢? 1.通过XML的方式来生成一个bean    最简单也是最原始的一种方式,通过XML来定义一个bean,我们来看下其过程 1)创建entity,命名为Student @Data@AllArgsConstructor@NoArgsCon

Spring实例化bean的三种方式

在面向对象编程的过程中,要想调用某个类的成员方法,首先要实例化该类的成员变量. 在Spring 中,实例化Bean有三种方式: 1.构造器实例化:2.静态工厂方式实例化:3.实例化工厂方式实例化 构造器实例化:Spring容器通过Bean对应的类中默认的构造器函数实例化Bean. 1-1.创建一个实体类 Person1 package com.mengma.instance.constructor; public class Person1 { } 1-2.创建Spring配置文件,在 com.

Spring 实例化Bean的两种方式

使用Spring管理Bean也称依赖注入( Dependency Injection, DI ),通过这种方式将Bean的控制权交给Spring 在使用Spring实例化一个对象时,无论类是否有参数都会默认调用对象类的无参构造,对于有参数的情况,Spring有两种方式可以带参实例化 示例类 Shape public class Shape { private Integer width; private Integer height; public Shape() { System.out.pr