-
@SpringBootApplication
- 功能:这是Spring Boot应用的核心注解,它是一个组合注解,实际上相当于同时使用了
@Configuration
、@EnableAutoConfiguration
和@ComponentScan
。它标记在主应用类上,用于开启Spring Boot的自动配置功能,扫描组件并加载应用程序上下文。 - 示例:
import org.springframework.boot.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringBootApplication.run(MyApplication.class, args);
}
}- 在这个示例中,
MyApplication
是主应用类,@SpringBootApplication
注解告诉Spring Boot这是一个应用的入口点,并且要开启自动配置、扫描组件等操作。
- 功能:这是Spring Boot应用的核心注解,它是一个组合注解,实际上相当于同时使用了
-
@RestController
- 功能:是
@Controller
和@ResponseBody
的组合注解。@Controller
表示这个类是一个Spring MVC控制器,用于处理Web请求;@ResponseBody
表示方法的返回值直接作为HTTP响应体返回,而不是解析为视图名称,通常用于构建RESTful风格的Web服务。 - 示例:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {
@GetMapping(“/hello”)
public String sayHello() {
return “Hello, World!”;
}
}- 这里的
HelloController
类被@RestController
注解标记,sayHello
方法处理/hello
路径的GET
请求,并直接返回一个字符串作为响应内容。
- 功能:是
-
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping等
- 功能:这些注解是Spring MVC提供的用于简化HTTP请求方法映射的注解。
@GetMapping
用于处理GET
请求,@PostMapping
用于处理POST
请求,@PutMapping
用于处理PUT
请求,@DeleteMapping
用于处理DELETE
请求。它们都可以指定请求路径作为参数。 - 示例:
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {
@GetMapping(“/users”)
public String getUsers() {
// 返回用户列表的逻辑
return “List of users”;
}@PostMapping("/users") public String addUser() { // 添加用户的逻辑 return "User added"; } @PutMapping("/users/{id}") public String updateUser() { // 更新用户的逻辑 return "User updated"; } @DeleteMapping("/users/{id}") public String deleteUser() { // 删除用户的逻辑 return "User deleted"; }
}
- 这个
UserController
类展示了如何使用这些注解来处理不同类型的HTTP请求,用于对用户资源进行CRUD(创建、读取、更新、删除)操作。
- 功能:这些注解是Spring MVC提供的用于简化HTTP请求方法映射的注解。
-
@RequestMapping
扫描二维码关注公众号,回复: 17573000 查看本文章- 功能:这是一个比较通用的请求映射注解,可以用于处理多种HTTP请求方法。它可以指定请求路径、请求方法、请求头、请求参数等条件来匹配请求。
@GetMapping
等注解实际上是@RequestMapping
的简化形式。 - 示例:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping(“/books”)
public class BookController {
@RequestMapping(value = “/{id}”, method = RequestMethod.GET)
public String getBookById() {
// 根据ID获取图书的逻辑
return “Book details”;
}
}- 这里
BookController
类使用@RequestMapping
注解指定了基础路径为/books
,getBookById
方法使用@RequestMapping
再次指定了请求路径为/{id}
并且请求方法为GET
,用于获取指定ID的图书信息。
- 功能:这是一个比较通用的请求映射注解,可以用于处理多种HTTP请求方法。它可以指定请求路径、请求方法、请求头、请求参数等条件来匹配请求。
-
@Autowired
- 功能:用于自动装配Spring容器中的Bean。当Spring容器中有一个类型匹配的Bean时,它会自动将这个Bean注入到被
@Autowired
注解标记的字段、方法参数或构造函数参数中。 - 示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserService {
private final UserRepository userRepository;@Autowired public UserService(UserRepository userRepository) { this.userRepository = userRepository; } // 其他业务逻辑
}
- 在这个
UserService
类中,通过构造函数注入了UserRepository
。@Autowired
注解告诉Spring容器自动找到UserRepository
类型的Bean并注入到构造函数中。
- 功能:用于自动装配Spring容器中的Bean。当Spring容器中有一个类型匹配的Bean时,它会自动将这个Bean注入到被
-
@Service、@Component、@Repository、@Controller(前面已提及部分功能)
- 功能:这些注解都是用于标记Spring中的组件,使得Spring容器能够扫描并管理这些组件。
-
@Service
:用于标记业务逻辑层的组件,表明这个类是一个服务类,主要用于处理业务逻辑。
-
@Component
:是一个通用的组件注解,用于标记任何Spring管理的组件。如果一个类不符合@Service
、@Repository
或@Controller
的语义,但又需要被Spring容器管理,就可以使用@Component
。
-
@Repository
:用于标记数据访问层(如数据库访问)的组件,它还具有一些额外的功能,比如在数据访问出现异常时会自动转换为Spring的DataAccessException
体系的异常。
- 示例:
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
// 业务逻辑实现
}import org.springframework.stereotype.Component;
@Component
public class MyComponent {
// 组件功能实现
}import org.springframework.stereotype.Repository;
@Repository
public class UserRepositoryImpl implements UserRepository {
// 数据访问逻辑实现
} -
@Configuration
- 功能:用于标记一个类作为配置类,在这个类中可以定义Bean、导入其他配置类、设置属性源等。它相当于一个XML配置文件的Java版,用于配置Spring容器中的Bean和其他相关设置。
- 示例:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}- 在这个
AppConfig
配置类中,@Bean
注解用于定义一个Bean,方法名(myBean
)默认作为Bean的名称,方法的返回值就是Bean的实例。
-
@Value
- 功能:用于将外部配置文件(如
application.properties
或application.yml
)中的属性值注入到Spring管理的组件中。可以用于注入简单类型的值,如字符串、数字等。 - 示例:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class MyComponent {
@Value(“${my.property}”)
private String myProperty;
// 其他逻辑
}- 假设在
application.properties
文件中有my.property=Hello
,那么myProperty
字段就会被注入值"Hello"
。
- 功能:用于将外部配置文件(如