spring context

It is necessary to understand the context of Spring, because that is where everything in Spring starts.

Understand the spring context

Org. Springframework. Context. Spring ApplicationContext interface said the IoC container, is responsible for the instantiation, configure, and assemble the bean. The container reads configuration metadata to get instructions about which objects to instantiate, configure, and assemble. Configuration metadata is represented in XML, Java annotations, or Java code. It allows you to express the objects that make up your application and the rich interdependencies between those objects.

Spring provides several implementations of the ApplicationContext interface. In a separate application, usually create ClassPathXmlApplicationContext or FileSystemXmlApplicationContext instance.

In most application scenarios, explicit user code is not required to instantiate a Spring IoC container instance. For example, in a Web application scenario, a simple 8-line Web descriptor XML in the application’s web.xml file is sufficient.

Configuration Metadata is injected into the Spring Container

As shown in the figure above, the Spring IoC container uses a form of configuration metadata. This configuration metadata represents how, as an application developer, you tell the Spring container to instantiate, configure, and assemble objects in your application. It is usually declared in one of two ways.

  • XML configuration file.
  • Java classes annotated with @Configuration.

How to inject instance

A few common comments for injecting instances:

@Bean

Annotation On a method, mark the return value of the method as an object managed by the Spring container.

@Configuration

A Java class annotated by @Configuration indicates that the class is used for Bean resource definitions.

@Configuration
public class AppConfig {
    @Bean
    public MyService myService(a) {
        return newMyServiceImpl(); }}Copy the code

Use Java-based configuration to create a Spring Container

public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
    MyService myService = ctx.getBean(MyService.class);
    myService.doStuff();
}
Copy the code

AnnotationConfigApplicationContext is not limited to use @ the annotation class instantiation of the Configuration, it can use any @ Component or JSR – 330 annotations classes, such as:

public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class,Dependency2.class);
    MyService myService = ctx.getBean(MyService.class);
    myService.doStuff();
}
Copy the code

@ComponentScan

Allows Spring to scan @Component annotated classes in the specified directory

@Configuration
@ComponentScan(basePackages = "com.acme")public class AppConfig {... }Copy the code

Inject your business objects into Spring Container

Java classes @Controller,@Service, and @Repository are annotated by the @Component annotation and will be treated by Spring as @Component annotations

Using the spring bean

Assemble the bean in the container with @autoWired for Method,field

Reference documentation

Docs. Spring. IO/spring – fram…