springBoot常用注解

@autowired 将一个类注入到此类中
@Resource 与@Autowird相似

@Component 一般组件 model util pojo 等加上 作用在类上

@Configuration: 配置类 相当于一个类工具类 里面可以注入bean 相当于之前spring的配置文件注入bean 作用在类上面
@Configuration //相当于当初的spring 配置文件 xml 零xml配置文件

@Value("${spring.redis.host}") //spring注解 从配置文件中获取值yml
private String host;

@Bean 在@Configuration类里面注入bean后面就可以直接@Autowired 注入
@Bean //bean的实例 需要名称相对应 如果不对应需要加@qualifer进行修改

    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
        return client;
    }
    @Autowired
    @Qualifier("restHighLevelClient")
    RestHighLevelClient client;

    @Autowired
    htmlParseUtil htmlParse;

@RestController @Controller层加@ResponBody返回值 作用在类上面

@ResponseBody 看名字应该能猜出来返回值,用于对前端传来的操作进行回复反应,将controller的方法(实际调用的service层的方法)返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据,
注意:在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
是作用在方法上的,表示该方法的返回结果直接写入 HTTP response body 中,一般在异步获取数据时使用【也就是AJAX】。
注意:在使用 @RequestMapping后,返回值通常解析为跳转路径,但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据。@RequestBody 将 HTTP 请求正文插入方法中,使用适合的 HttpMessageConverter 将请求体写入某个对象
后台 Controller类中对应的方法:

@RequestMapping("/login.do") //前端访问此路径那么则会返回json的user对象
@ResponseBody
public Object login(String name, String password, HttpSession session) {
	user = userService.checkLogin(name, password);
	session.setAttribute("user", user);
	return new JsonResult(user);
}
上面可以改写为 即可以将@ResponseBody去掉
@RestCOntroller@RequestMapping("/login.do") //前端访问此路径那么则会返回json的user对象
public Object login(String name, String password, HttpSession session) {
	user = userService.checkLogin(name, password);
	session.setAttribute("user", user);
	return new JsonResult(user);
}

@Controller spring基本构建层 controller层 默认对应的index.html
@GetMapping({"/", “/index”}) //绑定多个跳转页面
public String getIndex() {
return “index”; //对应index.html 所以返回值 对应 相应的web 页面
}
@RestController直接返回return的数据,不跳转到指定页面

@Service("") spring基本构建层 service层 @Service(“contentService”) 双引号中的要与@autowired中的一致 @Service要写到 Impl实现类上面
@Autowired
private ContentService contentService;

@GetMapping({"/","parse"})
@GetMapping("/parse/{keyword}")
@RequestMapping(value = "/parse/{keyword}",method = RequestMethod.GET)
@PostMapping()
@RequestMapping(method = RequestMethod.POST)
   public Boolean parse(@PathVariable String keyword) throws IOException {
        return contentService.parseContent(keyword);
    }

@GetMapping @PostMapping 分别对应 @RequestMapping的 get post方法
@PathVariable(“keyword”) String keyword 对应 @GetMapping("/parse/{keyword}") 中的参数用{} 扩起来

    @GetMapping("/search/{keyword}/{pageNo}/{pageNum}")
    public List<Map<String, Object>> search(
            @PathVariable("keyword") String keyword,
            @PathVariable("pageNo") int pageNo,
            @PathVariable("pageNum") int pageNum) throws IOException {

        return contentService.searchPage(keyword, pageNo, pageNum);
    }

lombok插件自带的注解
@Data //getter setter
@AllArgsConstructor //有参构造器
@NoArgsConstructor //无参构造器
@Component //springBoot 组件构造器
public class User {
private String name;
private int age;
}

除@Component注释外,还定义了几个拥有特殊语义的注释,他们分别是@Reposity,@Service,@Controller

在目前的Spring版本中,这三个注释和@Component是等效的,但是从注释类的命名上,很容易看出这三个注释分别和持久层、业务层、控制层(web层)相对应,虽然目前三个注释和@Component相比没有什么新意,但Spring将在以后的版本中为它们添加特殊的功能,所以,如果web应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用上述注解对分层中的类进行注释。

@Service用于标注业务层组件

@Controller用于标注控制层组件(如structs中的action)

@Repository用于标注数据访问组件,即DAO组件

@Component泛指组件,当组件不好归类时,我们使用这个注释进行标注

@SpringBootTest 需要和启动类在相同的路径下
@Test
@SpringBootApplication

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值