Spring获取bean的种方法

第一种:在初始化时保存ApplicationContext对象

ApplicationContext ac = newFileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");

说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring。


第二种:通过Spring提供的工具类获取ApplicationContext对象

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac2.getBean("beanId");

说明:
1. 这两种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例;
2. 第一种方式在获取失败时抛出异常,第二种方式返回null.


第三种:继承自抽象类ApplicationObjectSupport

说明:通过抽象类ApplicationObjectSupport提供的getApplicationContext()方法可以方便的获取到ApplicationContext实例,进而获取Spring容器中的bean。Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。


第四种:继承自抽象类WebApplicationObjectSupport

说明:和上面方法类似,通过调用getWebApplicationContext()获取WebApplicationContext实例;


第五种:实现接口ApplicationContextAware

说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext对象。Spring初始化时,会通过该方法将ApplicationContext对象注入。

虽然Spring提供了后三种方法可以实现在普通的类中继承或实现相应的类或接口来获取Spring的ApplicationContext对象,但是在使用时一定要注意继承或实现这些抽象类或接口的普通java类一定要在Spring的配置文件(即application-context.xml文件)中进行配置,否则获取的ApplicationContext对象将为null

示例

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextHolder implements ApplicationContextAware {
    private static final Logger log = LoggerFactory.getLogger(ApplicationContextHolder.class);

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext context) throws BeansException {
        if (applicationContext != null) {
            throw new IllegalStateException("ApplicationContextHolder already holded 'applicationContext'.");
        }
        applicationContext = context;
        log.info("holded applicationContext,displayName:" + applicationContext.getDisplayName());
    }

    public static ApplicationContext getApplicationContext() {
        if (applicationContext == null)
            throw new IllegalStateException("'applicationContext' property is null,ApplicationContextHolder not yet init.");
        return applicationContext;
    }

    public static <T> T getBean(String beanName) {
        return (T)getApplicationContext().getBean(beanName);
    }
}

applicationContext.xml配置文件需添加如下配置:

<bean id="ApplicationContextHolder" class="com.dragonsoft.is2.sys.utils.ApplicationContextHolder"></bean>

这里还有一种方法:

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
EhCacheCacheManager cacheManager = (EhCacheCacheManager)wac.getBean(“cacheManager”);

### 回答1: 1. 通过getBean()方法获取bean 2. 通过@Autowired和@Qualifier注解获取bean 3. 通过@Resource注解获取bean 4. 通过@Inject注解获取bean 5. 通过Java Config类中的@Bean注解获取bean 6. 通过Java Config类中的@Configuration注解获取bean 7. 通过Java Config类中的@Import注解获取bean 8. 通过Java Config类中的@ImportResource注解获取bean 9. 通过XML配置文件中的<bean>标签获取bean ### 回答2: Spring获取Bean的9方式: 1. 通过ApplicationContext获取:可以直接通过ApplicationContext类的getBean()方法获取Bean实例。例如:ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); MyBean bean = (MyBean) context.getBean("myBean"); 2. 通过BeanFactory获取BeanFactory是Spring的最基本的工厂接口,可以通过BeanFactory来获取Bean实例。例如:BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); MyBean bean = (MyBean) factory.getBean("myBean"); 3. 通过@Autowired注解获取:使用@Autowired注解可以自动装配Bean实例。例如:@Autowired MyBean bean; 4. 通过@Resource注解获取:使用@Resource注解可以指定要装配的Bean实例。例如:@Resource(name = "myBean") MyBean bean; 5. 通过Java配置类获取:使用@Configuration注解创建一个Java配置类,然后在配置类中使用@Bean注解将Bean实例化并返回。例如:@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); MyBean bean = (MyBean) context.getBean("myBean"); 6. 通过@Qualifier注解获取:使用@Qualifier注解可以指定Bean的名称,用于解决多个同类型Bean的装配问题。例如:@Autowired @Qualifier("myBean") MyBean bean; 7. 通过构造函数获取:可以在类的构造函数中注入依赖的Bean实例。例如:public class MyClass { private MyBean myBean; public MyClass(MyBean myBean) { this.myBean = myBean; } } 8. 通过静态工厂方法获取:可以使用静态工厂方法来创建Bean实例。例如:public class MyFactory { public static MyBean createBean() { return new MyBean(); } } MyBean bean = MyFactory.createBean(); 9. 通过实例工厂方法获取:可以在实例工厂类中创建Bean实例。例如:public class MyFactory { public MyBean createBean() { return new MyBean(); } } MyFactory factory = new MyFactory(); MyBean bean = factory.createBean(); ### 回答3: Spring获取Bean的方式有以下9: 1. 通过注解:使用@Autowired或者@Inject注解,在类的属性或者构造方法上标注,Spring会自动将对应的Bean注入进来。 2. 通过配置文件:在Spring的配置文件中,使用<bean>标签定义Bean对象,并指定其对应的类名。 3. 通过ApplicationContext:通过ApplicationContext接口的getBean方法,传入Bean的名称获取Bean对象。 4. 通过BeanFactory:通过BeanFactory接口的getBean方法,传入Bean的名称获取Bean对象。BeanFactory是ApplicationContext的父接口。 5. 通过实现FactoryBean接口:自定义一个FactoryBean实现类,在getObject方法中返回具体的Bean对象。 6. 通过Bean的别名:在Spring的配置文件中,通过<alias>标签为Bean设置一个别名,然后通过别名获取Bean对象。 7. 通过泛型类:在初始化Bean的时候,使用泛型指定Bean的类型,然后调用getBean方法获取Bean对象。 8. 通过Bean的作用域:在Spring的配置文件中,可以为Bean设置不同的作用域,如单例(Singleton)、原型(Prototype)等,在需要使用Bean的地方获取相应的作用域的Bean对象。 9. 通过名称和类型:可以根据Bean的名称和类型,通过getBean方法获取Bean对象。 以上是Spring获取Bean的9常见方式,可以根据具体的场景选择合适的方式来获取Bean对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值