How to get the IOC applicationcontext in Job

How to get the IOC applicationcontext in Job

 https://segmentfault.com/q/1010000008002800  

 

SpringBoot get ApplicationContext in three ways
what ApplicationContext that?

Spring is simply in a container, the container can be used to obtain a variety of bean components, register to listen to events, load the resource files and other functions.

Application Context obtained in several ways

1 using direct injection Autowired
@Component
public class {Book1

@Autowired
private ApplicationContext applicationContext;

public void show (){
System.out.println(applicationContext.getClass());
}
}


2 spring4.3 use new features
using spring4.3 new features but there are some limitations, must meet the following two points:

1 constructor can only have one, if there is more, there must be a constructor with no arguments, this time, spring will call the no-argument constructor

2 constructor parameter must be present in the spring container
 
@Component
public class Book2 {

private ApplicationContext applicationContext;

public Book2(ApplicationContext applicationContext){
System.out.println(applicationContext.getClass());
this.applicationContext=applicationContext;
}

public void show (){
System.out.println(applicationContext.getClass());
}

}
 
Interface implemented. 3 ApplicationContextAware spring provided
spring bean after initialization is not determined ApplicationContextAware subclass, call setApplicationContext () method, it will be passed into the container ApplicationContext

@Component
public class Book3 implements ApplicationContextAware {

private ApplicationContext applicationContext;

public void show (){
System.out.println(applicationContext.getClass());
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

  
Get three results:

class org.springframework.context.annotation.AnnotationConfigApplicationContext
class org.springframework.context.annotation.AnnotationConfigApplicationContext
class org.springframework.context.annotation.AnnotationConfigApplicationContext
————————————————
 

Guess you like

Origin www.cnblogs.com/kelelipeng/p/11490766.html