Home  >  Article  >  Java  >  Several ways to obtain beans in spring

Several ways to obtain beans in spring

小老鼠
小老鼠Original
2023-12-29 16:11:071197browse

How to obtain beans: 1. Obtain through ApplicationContext; 2. Obtain through annotations; 3. Obtain through BeanFactory; 4. Obtain through Java configuration class, etc. Detailed introduction: Choose to use according to specific project requirements and configuration methods. Generally speaking, it is recommended to use ApplicationContext to obtain beans because it provides more functions and features.

Several ways to obtain beans in spring

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

In the Spring framework, there are many ways to obtain Bean (object instance). Common methods include:

1. Obtain through ApplicationContext:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
YourBeanType yourBean = context.getBean("beanName", YourBeanType.class);

Required here Replace "applicationContext.xml" with the name of your actual Spring configuration file, "beanName" with the name of the bean defined in the configuration file, and YourBeanType with the type of your bean.

2. Obtain through annotations:

When configuring Spring using annotations, you can use @Autowired annotations or @Resource annotations to automatically inject beans. For example:

@Autowired
private YourBeanType yourBean;

Here you need to replace YourBeanType with the type of your Bean.

3. Obtain through BeanFactory:

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
YourBeanType yourBean = factory.getBean("beanName", YourBeanType.class);

Here you also need to replace "applicationContext.xml" with the name of the actual Spring configuration file, and "beanName" with the name of the Bean defined in the configuration file. , YourBeanType is the type of your Bean.

4. Obtain through Java configuration class:

If you use Java configuration-based method, you can obtain the Bean through the configuration class annotated with @Configuration:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
YourBeanType yourBean = context.getBean(YourBeanType.class);

Here AppConfig .class is a Java configuration class that contains the @Bean annotation, and YourBeanType is the type of your Bean.

These methods can be selected according to specific project requirements and configuration methods. Generally speaking, it is recommended to use ApplicationContext to obtain beans because it provides more functions and features.

The above is the detailed content of Several ways to obtain beans in spring. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn