Spring controls the order in which implementation classes that implement the same interface are called in the list

Source: Internet
Author: User

since the Redis cache is updated, each service implements the update function in the basic Rediscacheupdate interface, and in the scheduled task, it uses the List<rediscacheupdate The > method calls update in bulk, but there are cache dependencies between the service, such as service B relying on service A's cache, so that if B is updated then it will not fetch the cached data and every time it is taken in the database, it is a waste of time, so you need to control a, B In the list is injected in the order, then the question comes, how to control their call order?
Spring has long provided us with a solution that can proactively change the injection order of each implementation class by annotating the order, and the spring version here is 4.0.8-release.
test It, sure enough, in the given order to inject, the very aspect.


Interface base{
void print ();
}

@Component
@Order (3)
class A implements base{

void print () {
syso ("A");
     }
}
@Component
@Order (2)
class B implements base{

void print () {
syso ("B");
     }
}
@Component
@Order (1)
class C implements base{

void print () {
syso ("C");
     }
}
@Component
class invokerclass{
@Autowired
list<base> List;

void print () {
For (Base base:list) {
base.print ();
          }
     }
}

Public class main{

Public static void Main (string[] args) {
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath*:spring.xml");
Invokerclass in = Context.getbean (Invokerclass.class);
in.print ();
     }

Printed results: CBA This is mainly the use of @order annotations, the annotation assignment value according to the size of the value of the priority of the call, the greater the lower the priority. This completely alters the order in which the beans are called. can control their sequencing.

Spring controls the order in which implementation classes that implement the same interface are called in the list

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.