8.1.2: Container Processor

Source: Internet
Author: User
Tags chop

In addition to the bean post-processor discussed in the previous article, spring also provides a container post-processor.Bean post-ProcessorProcesses All bean instances in the container, whileContainer ProcessorIt is responsible for processing the container itself.

The post-container processor must implementBeanfactorypostprocessor
Interface, which has a method:

Void postprocessbeanfactory (configurablelistablebeanfactory beanfactory) throws beansexception

The method body that implements this method is the processing of the spring container, which can be customized and extended to the spring container. Of course, the spring container can not be processed.

Axe. Java:

public interface Axe {public String chop();}

Steelaxe. Java:

Public class steelaxe implements axe {@ overridepublic string chop () {return "cutting firewood really fast";} public steelaxe () {system. out. println ("Spring instantiation dependent Bean: steelaxe instance... ");}}

Person. Java:

public interface Person {public void useAxe();}

Chinese. Java:

Public class Chinese implements person {private axe; Public void setaxe (axe) {system. out. println ("Spring executes dependency injection, setaxe... "); this. AXE = axe;} Public Chinese () {system. out. println ("Spring instantiation of the main Bean: Chinese instance... ") ;}@ overridepublic void useaxe () {system. out. println (axe. chop ());}}

Firstbeanfactorypostprocessor. Java:

Public class firstbeanfactorypostprocessor implements beanfactorypostprocessor {@ overridepublic void postprocessbeanfactory (configurablelistablebeanfactory beanfactory) throws beansexception {system. out. println ("the program initialization of beanfactory made by spring has not changed... "); system. out. println ("Spring container is:" + beanfactory );}}

Bean. XML Core Configuration:

 <bean id="chinese" class="com.bean.Chinese">    <property name="axe" ref="steelAxe"/> </bean>   <bean id="steelAxe" class="com.bean.SteelAxe"/> <bean id="beanFactoryPostProcessor" class="com.bean.FirstBeanFactoryPostProcessor"/>

Test. Java:

public class Test {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");Person p=(Person) ctx.getBean("chinese");p.useAxe();}}

Run test. Java and the console outputs:

The program will automatically search for classes that implement the beanfactorypostprocessor interface in the container and register it as a post-container processor.

Spring provides the following commonly used container backprocessors:

① Propertyplaceholderconfigurer: The property placeholder configurator.

② Propertyoverrideconfigurer: rewrite the placeholder configurator.

Customautowirecycler: customizes the automatically assembled configurator.

Customscopeconfigurer: A configurator with a custom scope.

From the above introduction, we can see that the post-container processor is usually used to process the spring container and always reads the metadata of the configuration file before the container instantiates any other bean, the metadata may be modified.

If necessary, the program can configure multiple container post-processors, and the order attribute can be set for multiple container post-processors to control the execution order of the container post-processor.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.