How to use autowire in the Java library for dependencies to inject

In the Java class library, using @Autowired annotation for dependency injection is a commonly used method to simplify code and increase flexibility.This article will introduce how to use @Autowired annotations in the Java library for dependent injection and provide some example code. Dependent injection is a design pattern that decoupled the dependent relationship between classes from hard -coded, and improves the maintenance and scalability of the code.Using @Autowired annotation can make the Spring container automatically find and inject the dependent dependencies. To use the @Autowired annotation in the Java library for dependencies, the following steps are required: 1. Configure the Spring container: Configure the Spring container in the project configuration file (such as ApplicationContext.xml) so that it can automatically scan and manage the instance in the class library. 2. Use @Autowired annotations to use @Autowired annotation injection dependencies: use the @Autowired annotation on the type of member variables, constructor or method parameters that need to inject dependencies.When the Spring container is initialized, it will automatically find and inject the corresponding dependencies through type matching. Below is a simple example code that demonstrates how to use the @Autowired annotation in the Java class library for dependent injection: public class UserService { @Autowired private UserRepository userRepository; // Use the injected dependency execution business logic public void createUser(String username, String password) { // Use UserRepository for user creation operation userRepository.create(username, password); } } public interface UserRepository { void create(String username, String password); } @Component public class UserRepositoryImpl implements UserRepository { public void create(String username, String password) { // Realize the logic of creating users System.out.println ("Create user:" + username); } } @SpringBootApplication public class Application { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(Application.class, args); // Get the UserService instance UserService userService = context.getBean(UserService.class); // Calling method, the implementation of userRePOSITORY will be automatically injected into the business logic userService.createUser("abc", "123"); } } In the above sample code, userService is called a service class, and UserRepository is called a data access class.The UserService class uses @AutowIred annotations to inject UserRepository instances and use it to perform the creation user operation in the business logic. In the Application class, we use the SpringApplication.run method to start the Spring application, obtain the UserService instance, and then call the CreateUser method. Through the above steps, we can use the @Autowired annotation in the Java library for dependent injection.Using dependency injection can improve the testability and scalability of the code, reduce the coupling of the code, and make the code clearer and easy to maintain.