Spring in Java Class Library

Generate Chinese Knowledge Article Title: Java Class Library Using the Spring Framework Spring is a popular Java development framework that provides many powerful class libraries to simplify and accelerate the development process. In this article, we will discuss some common Java class libraries and their sample code used in the Spring framework. 1. Spring Core: The Spring Core is the core part of the Spring framework, which provides dependency injection and control inversion functionality. The following is an example of using Spring Core: import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MainApp { public static void main(String[] args) { //Create an application context based on annotation configuration ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); //Get Bean from Context HelloWorld helloWorld = context.getBean(HelloWorld.class); //Calling methods for beans helloWorld.printMessage(); } } 2. Spring MVC (Spring Model View Controller): Spring MVC is a web application development framework based on the Model View Controller (MVC) pattern. It provides a set of class libraries for processing HTTP requests and responses. The following is an example of using Spring MVC: import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloWorldController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello(Model model) { //Add attributes to the model model.addAttribute("message", "Hello, World!"); //Returns the name of the view return "hello-world"; } } 3. Spring Data (Spring Data Access): Spring Data is a class library used to simplify data access, which supports various data storage technologies such as relational databases, NoSQL databases, and more. The following is an example of using Spring Data JPA: import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { User findByUsername(String username); } @Service public class UserService { @Autowired private UserRepository userRepository; public User getUserByUsername(String username) { return userRepository.findByUsername(username); } } The above are some common Java class libraries and their code examples used in the Spring framework. Spring's class libraries are very rich and can adapt to different development needs. Whether it's dependency injection, web application development, or data access, Spring provides powerful functionality and easy-to-use class libraries to accelerate the development process.