1、使用注解需要导入的依赖

  • 1、1在application.xml文件中加入该约束

xmlns:context=http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd

 并且需要加入标签开启该注解

<context:annotation-config/>
或指定要扫描的包,包下的注解就会生效
<context:component-scan base-package="com.kuang.pojo"/>

 最终xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
</beans>
  • 1、2对应注解含义

@Autowired 自动装配,优先匹配类型,后名字
@Qualifier(value = "xxx")可指定自动装配的id
@Resource(name = "xxx") 自动装配,优先名字,后类型,也可指定名称
@Nullable 该注解后的参数可为空
@Component 组件,放在类上,说明该类被spring管理了,就是bean
    mvc三层架构:
        dao:@Repository
        service:@Service
        Controller:@Controller
功能一样,都是将该类注册到spring中,装配bean
该注解需配合<context:component-scan base-package="com.kuang.dao"/>扫包进行使用
任需ClassPathXmlApplicationContext,无法脱离配置文件
@Value("xxx")该注解用于给属性进行注入,也能够直接注入与set方法中
@Scope("xxx")该注解指定对应类的作用域,是单例模式或原型模式或其它
lombok包下的快速生成实体类对象的注解{
    @NoArgsConstructor快速创建无参构造
    @AllArgsConstructor 快速创建有参构造
    @Data 快速创建get,set
}

  spring4之后要使用注解必须导入aop包,若发现注解无效,可查看是否导入该包

使用java配置spring,完全舍弃spring的xml配置文件


@Configuration:将类指定为spring配置类
@Bean:指定该方法为xml中相当于<bean> 需返回一个实体类
@ComponentScan("xxxx"):使该配置类只扫描到指定的包下
@Import({xxx.class}):合并多个配置类

SpingMVC注解开发

@RequestMapping("/xxx"):该注解可映射一个访问路径,在单个方法上时直接访问 http://localhost:8080/xxx
						在类上时访问需加上类的访问路径 http://localhost:8080/类上的映射名/xxx
在返回单纯的数据时,它可以进行乱码解析
    @RequestMapping(value = "/sda",produces = "application/json;charset=utf-8")

RestFul风格

@PathVariable
加在参数前,可定义为路径变量

未使用前

package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestfulTest {
    @RequestMapping("restful")
    public String restful(int a, int b, Model model){
        int c = a+b;
        model.addAttribute("msg",c);
        return "hello";
    }
}

Spring注解开发

使用后

package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestfulTest {
    @RequestMapping("/restful/{a}/{b}")
    public String restful(@PathVariable int a, @PathVariable int b, Model model){
        int c = a+b;
        model.addAttribute("msg",c);
        return "hello";
    }
}

Spring注解开发

restful是一种风格,并非规范或标准

restful指定访问方式

@RequestMapping(value

value可换成path,禁止使用name,会出问题

package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class RestfulTest {
    @RequestMapping(value = "/restful/{a}/{b}",method = RequestMethod.GET)
    public String restful(@PathVariable int a, @PathVariable int b, Model model){
        int c = a+b;
        model.addAttribute("msg",c);
        return "hello";
    }
}

通过在注解中选择method可以指定通过什么方式来进行访问该路径才能得到对应的方法。

通过另外的注解也能实现对应的效果

@RequestMapping(name = "/restful/{a}/{b}",method = RequestMethod.GET)
//get方法可以用
@GetMapping("xxx")
//相同的,也有DeleteMapping等对应的注解可以实现method = RequestMethod.xxx

使用GetMapping注解接收前端参数,可直接从参数中获取,也可使用注解指定参数名

@GetMapping("/t1")
public ModelAndView he(@RequestParam("hs")String hs,User user){
    System.out.println(user);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("msg",user+"\n"+hs);
    modelAndView.setViewName("hello");
    return modelAndView;
}

@RequestParam("xxx") 指定该参数接收时的参数名必须为xxx

@Param("xxx")也可给指定参数一个别名

向前端返回数据,绕过视图解析器

在方法上写上@ResponseBody添加该注解,则绕过视图解析器,仅返回数据,不跳转视图

在类上添加@RestController注解,该类下的所有方法都只会返回数据,不跳转视图

Qualifier

@Qualifier

当bean中存在多个BookService类型对象时,搭配@Qualifier(“实现类名称”)表明注入的是哪一个具体实现类的bean(与 @Autowired配合使用加以区分)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。