装配注解
Spring 提供了以下多个注解,这些注解可以直接标注在 Java 类上,将它们定义成 Spring Bean
-
@Component
:
- 该注解用于描述 Spring 中的 Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,例如 Service 层、Dao 层等. 使用时只需将该注解标注在相应类上即可. 次注解分为以下三类:
-
@Repository
: 该注解用于将数据访问层(Dao 层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
-
@Service
: 该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
-
@Controller
: 该注解通常作用在控制层(如 Struts2 的 Action、SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同
可以通过以下注解描述依赖关系
- 可以应用到 Bean 的属性变量、setter 方法、非 setter 方法及构造函数等,默认按照 Bean 的类型进行装配。
- @Autowired 注解默认按照 Bean 的类型进行装配,默认情况下它要求依赖对象必须存在,如果允许 null 值,可以设置它的 required 属性为 false。如果我们想使用按照名称(byName)来装配,可以结合 @Qualifier 注解一起使用
- 作用与 Autowired 相同,区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 的名称进行装配。
- @Resource 中有两个重要属性:name 和 type。
Spring 将 name 属性解析为 Bean 的实例名称,type 属性解析为 Bean 的实例类型。
如果指定 name 属性,则按实例名称进行装配;
如果指定 type 属性,则按 Bean 类型进行装配;
如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常。
-
@Qualifier
与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。
@Service
public class CategoryService {
private static final Logger LOG = LoggerFactory.getLogger(CategoryService.class);
@Resource
private CategoryMapper categoryMapper;
@Resource
private SnowFlake snowFlake;
.....
.....
.....
基于Java配置类的装配
- 不使用xml文件配置Bean,而是单独写一个配置类来配置Bean
-
@Bean
注解扮演与 元素相同的角色。用到方法上,表示当前方法的返回值是一个bean
-
@Configuration
类允许通过简单地调用同一个类中的其他 @Bean 方法来定义 Bean 间依赖关系。相当于spring的配置文件XML
@Configuration
public class ServiceConfiguration {
@Bean
public InventoryService inventoryService() {
return new InventoryService();
}
@Bean
public ProductService productService() {
return new ProductService(inventoryService());
}
}
SpringMVC注解
-
@Controller
: 表明这是一个Controller
-
@ResponseBody
: 将返回对象写到response的body区,通常用来返回json或xml数据;如果不加responsebody,默认返回的是跳转路径
-
@RestController
: 包含@Controller
和@ResponseBody
两个注解
@RequestMapping("/login")
@ResponseBody
public User login(User user){
return user;
}
//User字段:userName pwd
//那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}'
@RestController
@RequestMapping("/doc")
public class DocController {
@Resource
private DocService docService;
@GetMapping("/all")
//将repuest封装成 DocReq类
public CommonResp all() {
CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
List<DocQueryResp> list = docService.all();
resp.setContent(list);
return resp;
}
....
....
-
@RequestMapping
: controller指定可以处理哪些 URL 请求, 具体分为以下几种, 对应RESTful
-
@PostMapping
: Post Request
-
@GetMapping
: Get Request
-
@DeleteMapping
: Delete Request
-
@PutMapping
: Put Request
-
@PathVariable:
可以使用@PathVariable将路径中的参数绑定到请求方法参数上
//根据id删除,需要传入id
@DeleteMapping("/delete/{id}")
public CommonResp delete(@PathVariable Long id) {
CommonResp resp = new CommonResp<>();
categoryService.delete(id);
return resp;
}
-
@RequestBody
: 此注解用在请求handler方法的参数上,用于将http请求的Body映射绑定到此参数上。HttpMessageConverter负责将对象转换为http请求. 适用于请求体格式为 application/json的请求,只能用于接收对象
@PostMapping("/save")
//将repuest封装成 CategoryReq类
//注意要加RequestBody才能以json形式接收参数
public CommonResp save(@Valid @RequestBody CategorySaveReq req) {
CommonResp resp = new CommonResp<>();
categoryService.save(req);
return resp;
}
-
@RequestPart
: 此注解用在请求handler方法的参数上,支持的请求方法是MultipartFile,@RequestPart适用于复杂的请求域(像JSON,XML),既可以接收对象又可以接收二进制数据流
-
@RequestParam
: 此注解用在请求handler方法的参数上,只能用来接收基本数据类型、String 或者MultipartFile类型
Spring配置注解
@ComponentScan, @SpringBootApplication, @MapperScan
@ComponentScan("com.daniel")
@SpringBootApplication
@MapperScan("com.daniel.wiki.mapper")
public class WikiApplication {
private static final Logger LOG = LoggerFactory.getLogger(WikiApplication.class);
public static void main(String[] args) {
SpringApplication app = new SpringApplication(WikiApplication.class);
Environment env = app.run(args).getEnvironment();
LOG.info("启动成功!!");
LOG.info("地址: \thttp://127.0.0.1:{}", env.getProperty("server.port"));
}
}