Spring註解開發

1、使用註解需要導入的依賴

  • 1、1在application.xml文件中加入該約束

xmlns:context=//www.springframework.org/schema/context
        //www.springframework.org/schema/context
        //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="//www.springframework.org/schema/beans"
       xmlns:context="//www.springframework.org/schema/context"
       xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="//www.springframework.org/schema/beans
       //www.springframework.org/schema/beans/spring-beans.xsd
       //www.springframework.org/schema/context
       //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"):該註解可映射一個訪問路徑,在單個方法上時直接訪問 //localhost:8080/xxx
						在類上時訪問需加上類的訪問路徑 //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";
    }
}

nam

使用後

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";
    }
}

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配合使用加以區分)

Tags: