Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to register Bean with container in Spring

2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to register Bean to the container in Spring. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

First of all, we need to know what Ioc is.

Ioc is one of the core technologies of Spring, whose full name is Inversion of Control (inversion of Control). The most original way to create an object is through new (manually coding implementation):

New Hollow ()

Spring provides Ioc containers for object creation, configuration, management, destruction, and so on.

Control: the container controls and manages the component code

Reverse: control of component code is transferred from external code to internal container (programmer to container)

When IoC is applied, other objects that an object depends on are passed in passively, rather than the object itself creating or finding dependent objects, which reduces the degree of coupling between classes. Instead of an object looking for a dependency from the container, the container actively passes the dependency to the object without waiting for the object request to initialize it.

So what exactly is a container?

From a program point of view, a container is actually an instance of an implementation class for a particular interface.

Spring provides two different types of containers:

Spring BeanFactory container: the simplest container that provides basic support for dependency injection

ApplicationContext container: the ApplicationContext container, which inherits from BeanFactory, includes all the functions of the BeanFactory container and extends its functionality.

Spring BeanFactory container

Org.springframework.beans.factory.BeanFactor interface

BeanFacotry is the relatively primitive Factory in spring. For example, XMLBeanFactory is a typical BeanFactory. The original BeanFactory can not support many plug-ins of spring, such as AOP function, Web application and so on.

The ApplicationContext interface, which is derived from the BeanFactory interface, ApplicationContext contains all the functions of BeanFactory and is generally recommended over BeanFactory.

Code:

Public interface BeanFactory {String FACTORY_BEAN_PREFIX = "&"; Object getBean (String var1) throws BeansException; T getBean (String var1, Class var2) throws BeansException; Object getBean (String var1, Object...) Var2) throws BeansException; T getBean (Class var1) throws BeansException; T getBean (Class var1, Object...) Var2) throws BeansException; ObjectProvider getBeanProvider (Class var1); ObjectProvider getBeanProvider (ResolvableType var1); boolean containsBean (String var1); boolean isSingleton (String var1) throws NoSuchBeanDefinitionException; boolean isPrototype (String var1) throws NoSuchBeanDefinitionException; boolean isTypeMatch (String var1, ResolvableType var2) throws NoSuchBeanDefinitionException; boolean isTypeMatch (String var1, Class var2) throws NoSuchBeanDefinitionException; @ Nullable Class getType (String var1) throws NoSuchBeanDefinitionException; String [] getAliases (String var1);}

The getBean method: gets the Bean object from the container

Is method: judgment of singleton, prototype and bean type

Get method: gets the Bean type and alias

The main implementation class of ApplicationContext

ClassPathXmlApplicationContext: loading configuration files from the classpath

FileSystemXmlApplicationContext: loading configuration files from the file system

ConfigurableApplicationContext: extended to ApplicationContext, adding two new main methods: refresh () and close (), so that ApplicationContext has the ability to start, refresh and close the context

ApplicationContext: all singletons' Bean are instantiated when the context is initialized.

WebApplicationContext: specifically for WEB applications, it allows initialization from the path relative to the WEB root directory

Bean Registration to Ioc Container process

1. Bean configuration information (xml configuration files, configuration classes, comments) defines the implementation and dependencies of Bean, that is, Bean configuration information.

2. The Spring container establishes the Bean definition registry inside the container according to various forms of Bean configuration information.

3. The container loads the Bean according to the registry, instantiates the Bean, establishes the dependency between Bean and Bean, and finally puts these ready Bean into the Bean cache pool for the outer application to call.

Bean registration method

1. XML configuration file (using the no-parameter constructor of the class)

Configuration file

The location and name of the configuration file are not fixed. It is recommended to name it applicationContext.xml.

Id: the name of the object created

Class: the full path of Bean that needs to be registered

Creating using a static factory and using an instance factory is similar to using a class's no-parameter constructor, and is no longer introduced

Testing method

Public void classBeanConfiguration () {ApplicationContext applicationContext=new ClassPathXmlApplicationContext ("config/applicationContext.xml"); System.out.println ("get Bean from container" + applicationContext.getBean ("hollow"));}

II. Java configuration class

Configuration class

@ Configurationpublic class SpringConfiguration {@ Bean public HollowController hollowController () {return new HollowController ();}}

Test class

@ Testpublic void xmlBeanConfiguration1 () {ApplicationContext applicationContext=new AnnotationConfigApplicationContext (SpringConfiguration.class); System.out.println ("get Bean from container:" + applicationContext.getBean ("hollowController"));}

III. Notes

Add comment

@ Controllerpublic class AnnotationController {public AnnotationController () {System.out.println ("annotationController created");}}

Turn on scanning

@ ComponentScan (value= "com.example.bean_configuration.configuration.controller") @ Configurationpublic class SpringConfiguration {}

Or

Testing method

@ Testpublic void xmlBeanConfiguration1 () {ApplicationContext applicationContext=new AnnotationConfigApplicationContext (SpringConfiguration.class); System.out.println ("get Bean from container:" + applicationContext.getBean ("annotationController"));}

Note: @ Controller annotation only serves as a marker. Bean will not be registered until the annotation scan @ ComponentScan is enabled.

After reading the above, do you have any further understanding of how to register Bean with the container in Spring? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report