The relationship between bean and spring container
Bean configuration information defines the implementation and dependencies of beans. Spring container establishes bean definition registry within the container based on various forms of bean configuration information, then loads and instantiates beans according to the registry, and establishes dependencies between beans and beans. Finally, these ready beans are placed in the bean cache pool for external applications to call.
This article will introduce you in detail about the use of encoding methods to dynamically configure beans in Spring. We will share them for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.
1 DefaultListableBeanFactory
DefaultListableBeanFactory implements the ConfigurableListableBeanFactory interface, which can dynamically inject beans through this class. In order to ensure that the injected bean can also be enhanced by AOP, we need to implement the bean's factory postprocessor interface BeanFactoryPostProcessor.
Beans that require dynamic injection:
public class BookService { BookDao bookDao; public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } public BookDao getBookDao() { return bookDao; }}Implementing the factory postprocessor interface of the Bean:
@Componentpublic class BookServiceFactoryBean implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory; //Bean Definition BeanDefinitionBuilder builder=BeanDefinitionBuilder.genericBeanDefinition (BookService.class); //Set the property builder.addPropertyReference("bookDao","bookDao"); //Register Bean Definition Factory.registerBeanDefinition("bookService1",builder.getRawBeanDefinition()); //Register Bean instance factory.registerSingleton("bookService2",new net.deniro.spring4.dynamic.BookService()); }}Here it is assumed that bookDao has been injected into the container (XML or annotation method).
Here, we can register the definition of a bean or directly register the instance of a bean.
Configuration:
<context:component-scan base-package="net.deniro.spring4.dynamic" />
Unit Tests:
BookService bookService1 = (BookService) context.getBean("bookService1");assertNotNull(bookService1);assertNotNull(bookService1.getBookDao());bookService bookService2 = (BookService) context.getBean("bookService2");assertNotNull(bookService2);2 Custom tags
To better encapsulate components and enhance their ease of use, we define components as labels.
The steps to customize the tag are:
Create bookservice.xsd under the schema folder in resources
<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns="http://www.deniro.net/schema/service" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.deniro.net/schema/service" elementFormDefault="qualified" attributeFormDefault="unqualified" > <!-- Import beans Namespace --> <xsd:import namespace="http://www.springframework.org/schema/beans"/> <!-- Define book-service tag--> <xsd:element name="book-service"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <!-- Define dao attribute--> <xsd:attribute name="dao" type="xsd:string" use="required"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element></xsd:schema>
Then define the service tag parser:
public class BookServiceDefinitionParser implements BeanDefinitionParser { public BeanDefinition parse(Element element, ParserContext parserContext) { //Create Bean DefinitionBeanDefinitionBuilder builder=BeanDefinitionBuilder.genericBeanDefinition (BookService.class); //Inject custom tag attribute String dao=element.getAttribute("dao"); builder.addPropertyReference("bookDao",dao); //Register Bean Define parserContext.registerBeanComponent(new BeanComponentDefinition(builder .getRawBeanDefinition(),"bookService")); return null; }}Then register the parser you just defined in the namespace:
public class BookServiceNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("book-service",new BookServiceDefinitionParser()); }}Then create the META-INF folder in resources and create spring.schemas and spring.handlers, which are used to configure the document structure file path of the custom tag and the parser that parses the custom namespace.
File path
spring.handlers:
http/://www.deniro.net/schema/service=net.deniro.spring4.dynamic.BookServiceNamespaceHandler
spring.schemas:
http/://www.deniro.net/schema/service.xsd=schema/bookservice.xsd
Note: The xsd file must be placed in the descendants directory of resources.
Quoting custom tags:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:me="http://www.deniro.net/schema/service" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.deniro.net/schema/service http://www.deniro.net/schema/service.xsd "> <bean id="bookDao"/> <me:book-service dao="bookDao"/></beans>
Here, we referenced the custom tag at the head and named it "me", and then we can use it O(∩_∩)O~
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.