SpringBoot获取Bean

一种最简单的方法是实现ApplicationContextAware类来获取容器中的bean:

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

@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    //获取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);
    }

}

我们可以通过bean的名称、bean的类型或者bean的名称+类型来获取容器中的bean。

默认情况下,Spring容器中的bean是单例的,为此我做了一个测试,我创建了两个bean,一个是默认的,一个是我指定多例的:

import org.springframework.stereotype.Service;

/**
 * 这是一个单例的bean
 */
@Service
public class BeanSingletonService {
}
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/**
 * 这是一个多例的bean
 */
@Service
@Scope("prototype")
public class BeanPrototypeService {
}

验证下我的想法:

import com.xqnode.learning.common.config.SpringContextUtil;
import com.xqnode.learning.service.BeanPrototypeService;
import com.xqnode.learning.service.BeanSingletonService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LearningApplicationTests {
    
    @Test
    public void contextLoads() {
        BeanSingletonService s1 = SpringContextUtil.getBean(BeanSingletonService.class);
        BeanSingletonService s2 = SpringContextUtil.getBean(BeanSingletonService.class);
        BeanPrototypeService p1 = SpringContextUtil.getBean(BeanPrototypeService.class);
        BeanPrototypeService p2 = SpringContextUtil.getBean(BeanPrototypeService.class);
        System.out.println("s1==s2: " + s1.equals(s2));
        System.out.println("p1==p2: " + p1.equals(p2));
    }


}

bean
从结果中可以看到默认的BeanSingletonService 这个bean是单例的,两个对象完全相等,而我指定的BeanPrototypeService 这个bean则是多例的,两个bean不相同。

  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 在Spring Boot中,可以通过以下两种方法获取Bean对象: 1. 使用Autowired注解:在需要使用Bean对象的类上使用Autowired注解,并声明一个对应类型的属性,Spring Boot会自动将对应的Bean注入。 2. 使用ApplicationContext获取:在类中通过ApplicationContext获取Spring上下文,然后调用getBean方法获取对应的Bean对象。 ### 回答2: Spring Boot是Spring框架的一种扩展,大大简化了Spring应用程序的开发过程。在Spring Boot中,Bean是一个重要概念,它代表了应用中的对象或者组件。Spring Boot中获取Bean对象的方法多种多样,下面将详细介绍。 1. 自动注入 在Spring Boot中,通过注解自动注入可以轻松获取Bean对象。Spring Boot自动配置了Bean的管理,我们可以使用注解自动实例化Bean对象。 使用@Autowired、@Resource等注解时,Spring Boot会自动扫描当前包及其子包下的所有类,扫描到该注解时,就会自动实例化Bean对象。 例如: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; // ... } ``` 这段代码中使用了@Autowired注解进行依赖注入,Spring Boot会自动实例化UserDao对象并赋值给userDao变量。 2. 注解 除了自动注入,也可以使用注解来获取Bean对象。Spring Boot提供了多种Bean相关注解。 - @Component:用于标注一个普通的Bean,可以放在类、方法、属性上。 - @Repository:用于标注DAO对象。 - @Service:用于标注Service对象。 - @Controller:用于标注Controller对象。 例如: ``` @Service public class UserServiceImpl implements UserService { @Resource private UserDao userDao; // ... } ``` 这段代码使用了@Resource注解进行依赖注入,其实也可以使用@Autowired注解进行依赖注入。 3. ApplicationContext Spring Boot还提供了ApplicationContext类来获取Bean对象。ApplicationContext是Spring框架的核心接口,用于管理Bean对象。 例如: ``` @Configuration public class AppConfig { @Bean public UserDao userDao() { return new UserDaoImpl(); } } @Service public class UserServiceImpl implements UserService { private UserDao userDao; public UserServiceImpl() { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); this.userDao = context.getBean(UserDao.class); } // ... } ``` 这段代码中使用了ApplicationContext类来获取Bean对象,首先定义了一个AppConfig类,创建了一个UserDao类型的Bean,在UserService中通过ApplicationContext获取UserDao对象实例。 总之,Spring Boot提供了多种方法来获取Bean对象,开发者可以按照自己的需要选择适合自己的方式。对于初学者来说,推荐使用自动注入的方式获取Bean对象,这种方式简单易用,能够自动管理Bean。对于一些特殊的情况可以使用ApplicationContext来获取Bean对象。 ### 回答3: Spring Boot是Spring框架的一种简化版本,它可以帮助程序员快速构建独立的、生产级别的Spring应用程序。在使用Spring Boot时,我们可以使用自动配置来简化应用程序的配置过程,它会自动为我们创建Bean并将它们注册到Spring容器中,程序员可以方便地获取这些Bean对象。 Spring Boot获取Bean对象的方式主要有以下几种: 1. 使用自动注入:在类中使用@Autowired或@Inject等注解,在Spring容器中查找指定类型的Bean对象,并将其注入到当前类中。 2. 使用@Bean注解:在类中使用@Bean注解,Spring Boot会自动将标注了@Bean的方法返回的对象注入到Spring容器中。 3. 使用@Import注解:在类中使用@Import注解,可以将其他的Java配置类加载到Spring容器中。 4. 使用@Profile注解:在类或者方法上添加@Profile注解,可以根据不同的环境加载不同的Bean对象。 5. 使用@Qualifier注解:在@Autowired注解中使用@Qualifier注解,可以指定要注入的Bean对象的名称。 6. 使用ApplicationContext:通过Spring的ApplicationContext接口获取Bean对象,可以手动查询和获取Bean对象。 以上就是Spring Boot获取Bean对象的主要方式,程序员可以根据自己的需要和实际情况选择适合自己的方式获取Bean对象。同时,在Spring Boot中还有其他的一些方式获取Bean对象,开发人员可以根据自己的需求和情况进行选择和使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员青戈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值