Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The Spring framework was created with the goal of simplifying the development of JAVA EE applications. It provides rich functionality for enterprise development, but the underlying functionality relies on its two core features: dependency injection (DI) and section-oriented programming (AOP).

This article focuses on how Spring initializes the Bean container in a project. Environment Overview:

  • JDK 11
  • Spring 5.3.10 – the SNAPSHOT

A simple case

Here is a simple use case for a Spring Bean container:

// Create a container
AnnotationConfigApplicationContext applicationContext =  new AnnotationConfigApplicationContext(AppConfig.class);
// Get the Bean object
UserService serviceService = applicationContext.getBean(UserService.class);
// Print the Bean object
System.out.println(serviceService);
Copy the code

In this example, we can see that Spring is very simple to use. The question is often asked: Why are we using Spring? What development capabilities does Spring offer us?

  1. Also, Spring itself is a Bean container that helps us manage objects. For example, singleton beans can be created by default, and prototype objects can be quickly created by tag, which solves a series of problems we have in object reference, or object recycling.
  2. Spring can also provide fast integration with common middleware such as MyBatis, MVC, RocketMQ, etc.

Let’s look at the basic uses of Spring in terms of Bean creation, Aop implementation, and Transaction implementation.

Bean creation

The definition of a Bean

Spring Beans are Java objects that Spring creates through the Spring container. They are initialized, assembled, and managed by the Spring IOC container. These beans are created from metadata configured in the container.

Beans defined in the Spring framework are singletons by default. This can be done with the @scope (” Singleton “) annotation, or @Scope(“prototype”) if you need to declare it as a prototype Bean.

How to create a Bean

We can add @Bean annotations to methods to create beans, or add @Component � to classes as follows:

/ / bean annotation
@Bean
private UserService user(a) {
    return new UserService();
}

@Component
public class UserService {}Copy the code

Aop implementations

That section

@Aspect
public class UserAspect {
	
	// Matches all methods of all classes under all ServiceImpl packages
	@Pointcut("execution(* com.ctc.ServiceImpl.*.*(..) )"
	public void addLog(a){}}Copy the code

Define Advice

There are five types of Advice notes: 1.@Before pre-notification Pre-notification is executed before the pointcut runs and does not affect the logic of the pointcut 2.@After post-notification Post-notification is executed after the pointcut runs properly. If the pointcut throws an exception, 3.@AfterThrowing exception notification is executed before the exception is thrown by the pointcut. If the pointcut is healthy (and does not throw an exception), 4.@AfterReturning return notification is executed after the pointcut is healthy. If the pointcut throws an exception, 5.@Around wrap advice is the most powerful form of advice, and you can customize some actions before and after a pointcut. The surround notification is responsible for deciding whether to continue processing the Join point(which calls the ProceedingJoinPoint method proceed) or to interrupt execution. For example:

// Use the addLog pointcut for enhancement
@Before("addLog()")
public void before(a) {
    System.out.println("Before pre-notification......");
}
Copy the code

Using proxy objects

Statements UserService

@Component
public class UserService {

	public void test(a) {
		System.out.println("orderService:"+ orderService); }}Copy the code

Calling the test method

public class AopTest {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext applicationContext =
				newAnnotationConfigApplicationContext(AppConfig.class); UserService serviceService = applicationContext.getBean(UserService.class); serviceService.test(); }}// AppConfig.java
@Import(UserAspect.class)
@Configurable
public class AppConfig {

	@Bean
	private UserService userService(a) {
		return newUserService(); }}Copy the code

Reference documentation

  • www.javacodegeeks.com/spring-inte…
  • Ifeve.com/spring-inte…