在Bean中获取Spring容器中的各种组件

Spring提供了许多***Aware接口来获取容器中的各种组件

***Aware 都定义了一个set***(XXX xx)方法需要子类bean去实现 。

在Spring初始化bean时 ,会对该bean的类型进行检查,判断是否实现了**Aware接口,如果实现了,就调用它对应的**set方法,并将对应的对象设置进去 。



以ApplicationContextAware为例,它用于获取ApplicationContext对象, 它的定义如下:

public interface ApplicationContextAware {
	
	/** 
	 * Set the ApplicationContext that this object runs in.
	 * Normally this call will be used to initialize the object.
	 * <p>Invoked after population of normal bean properties but before an init callback such
	 * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
	 * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
	 * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
	 * {@link MessageSourceAware}, if applicable.
	 * @param applicationContext the ApplicationContext object to be used by this object
	 * @throws ApplicationContextException in case of context initialization errors
	 * @throws BeansException if thrown by application context methods
	 * @see org.springframework.beans.factory.BeanInitializationException
	 */
	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}



实例:在我创建的Service中获取容器中的ResourceLoader、ApplicationEventPublisher、beanName、BeanFactory、MessageSource、ApplicationContext

package com.spring.service.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;

import com.spring.service.PersonService;

public class PersonServiceImpl implements PersonService, ApplicationContextAware, MessageSourceAware, BeanFactoryAware, BeanNameAware, ApplicationEventPublisherAware, ResourceLoaderAware {

    private String                    name;

    private ResourceLoader            resourceLoader;

    private ApplicationEventPublisher applicationEventPublisher;

    private String                    beanName;

    private BeanFactory               beanFactory;

    private MessageSource             messageSource;

    private ApplicationContext        applicationContext;

    public void sayHello() {
        System.out.println("Hello World!");
    }

    public String getName() {
        return this.name;
    }

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

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

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

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}


那么在该Service中就可以使用获取的那些对象了, spring在创建该bean的时候就会自动调用这些set方法,将对应的对象注入到我们的bean中。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架Bean是指由Spring容器管理的对象。这些对象通常是应用程序的核心组件,如服务、数据访问对象、控制器等。Spring容器会负责创建、配置、初始化和管理这些Bean,以确保应用程序的正常运行。 Spring容器Bean可以分为以下几种类型: 1. Singleton Bean:单例Bean,即在整个应用程序只存在一个实例的Bean,由Spring容器负责创建和管理。 2. Prototype Bean:原型Bean,即每次请求都会创建一个新的实例的Bean,由Spring容器负责创建,但需要手动管理Bean的生命周期。 3. Request Bean:请求Bean,即每个HTTP请求都会创建一个新的实例的Bean,由Spring MVC框架负责创建和管理。 4. Session Bean:会话Bean,即每个HTTP会话都会创建一个新的实例的Bean,由Spring Session框架负责创建和管理。 5. Global Session Bean:全局会话Bean,即在基于Portlet的Web应用程序,每个用户会话都会创建一个新的实例的Bean,由Spring Portlet MVC框架负责创建和管理。 Spring容器Bean可以通过XML配置、注解或Java配置来定义。通过XML配置定义Bean需要在XML文件显式地指定Bean的类名、属性和依赖项;通过注解定义Bean需要在Bean类上添加相应的注解,如@Component、@Service、@Controller等;通过Java配置定义Bean需要编写Java类来指定Bean的类名、属性和依赖项。无论是哪种方式,Spring容器都会在应用程序启动时自动扫描并创建这些Bean。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值