Research on bean initialization callback method in Spring container (i)

Source: Internet
Author: User


@Author Xiejun
@Since 2015/10/24

feeling a bit, once a lecturer said that the spring bean creation said three days and three nights can not finish, this kind of empty -talk ears
Cocoon, but still constantly be repeat, investigate its reason, O (︶^︶) o Alas indeed some research.

****
The m-word symbol is business stripping
Business Introduction:
system A needs to be obtained from another system B in bulk to the ticket, and then deposited into the database, when used, a system will be the number and binding information back to the B system.

in this business, the acquisition and deposit database can be made into a separate service that runs automatically in the background thread. Introduce the implementation:
at the container initialization level, create a listener service thread, inject the service, and start the thread.

The service involves database operations because it uses the MyBatis Mapper.xml file plus the way the interface is accessed, so there is no specific DAO implementation object that can be new.
In this case, the DAO interface needs to be injected into the service mentioned earlier, but not directly new.
****

Here's the good question:
The listener created in the Spring Web context container matches an interface entity that is injected MyBatis automatically generated. Then write a method in this service
perform operations on this automatically generated entity, Boom ~ ~nullpointerexception.

Debug
Database operation entity ==null.

It is certain that the properties of the bean managed by spring can be injected, and that the method execution should precede the attribute injection.

How to Solve:
The methods required by the service are placed before all Bean attribute injections are completed.

Spring container All beans are injected, there is a callback function. It is afterpropertiesset (), defined in spring's Initializingbean interface.
However, the class that contains the callback method implements the spring interface. It is then implemented in Afterpropertiesset (), which shows that this is done directly, without reflection.
However, the interface of spring is coupled in the service layer. This service module will not be used for non-sring framework applications, code reusability is poor.


answer:bingo!!
bean XML configuration can specify an argument-free initialization method that executes after all the attributes of the bean have been injected. This method is simply for my custom haha. The



test:

mybatis framework automatically generated database operation object injected successfully, DAO operation executed successfully.


Conclusion:
The properties and methods of the spring bean are specified in the order of the Tomcat Web context container after initialization is completed. Request a message
There is the wrong place, beg treatise!

Research on bean initialization callback method in Spring container (i)

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.