3.Spring有哪些常用注解呢?
Spring常用注解
Web:
@Controller:組合注解(組合了@Component注解),應(yīng)用在MVC層(控制層)。
@RestController:該注解為一個組合注解,相當(dāng)于@Controller和@ResponseBody的組合,注解在類上,意味著,該Controller的所有方法都默認(rèn)加上了@ResponseBody。
@RequestMapping:用于映射Web請求,包括訪問路徑和參數(shù)。如果是Restful風(fēng)格接口,還可以根據(jù)請求類型使用不同的注解:
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@ResponseBody:支持將返回值放在response內(nèi),而不是一個頁面,通常用戶返回json數(shù)據(jù)。
@RequestBody:允許request的參數(shù)在request體中,而不是在直接連接在地址后面。
@PathVariable:用于接收路徑參數(shù),比如@RequestMapping(“/hello/{name}”)申明的路徑,將注解放在參數(shù)中前,即可獲取該值,通常作為Restful的接口實現(xiàn)方法。
@RestController:該注解為一個組合注解,相當(dāng)于@Controller和@ResponseBody的組合,注解在類上,意味著,該Controller的所有方法都默認(rèn)加上了@ResponseBody。
容器:
@Component:表示一個帶注釋的類是一個“組件”,成為Spring管理的Bean。當(dāng)使用基于注解的配置和類路徑掃描時,這些類被視為自動檢測的候選對象。同時@Component還是一個元注解。
@Service:組合注解(組合了@Component注解),應(yīng)用在service層(業(yè)務(wù)邏輯層)。
@Repository:組合注解(組合了@Component注解),應(yīng)用在dao層(數(shù)據(jù)訪問層)。
@Autowired:Spring提供的工具(由Spring的依賴注入工具(BeanPostProcessor、BeanFactoryPostProcessor)自動注入)。
@Qualifier:該注解通常跟 @Autowired 一起使用,當(dāng)想對注入的過程做更多的控制,@Qualifier 可幫助配置,比如兩個以上相同類型的 Bean 時 Spring 無法抉擇,用到此注解
@Configuration:聲明當(dāng)前類是一個配置類(相當(dāng)于一個Spring配置的xml文件)
@Value:可用在字段,構(gòu)造器參數(shù)跟方法參數(shù),指定一個默認(rèn)值,支持 #{} 跟 ${} 兩個方式。一般將 SpringbBoot 中的 application.properties 配置的屬性值賦值給變量。
@Bean:注解在方法上,聲明當(dāng)前方法的返回值為一個Bean。返回的Bean對應(yīng)的類中可以定義init()方法和destroy()方法,然后在@Bean(initMethod=”init”,destroyMethod=”destroy”)定義,在構(gòu)造之后執(zhí)行init,在銷毀之前執(zhí)行destroy。
@Scope:定義我們采用什么模式去創(chuàng)建Bean(方法上,得有@Bean) 其設(shè)置類型包括:Singleton 、Prototype、Request 、 Session、GlobalSession。
實體類和接口
@Component
@Aspect
public class Loger {
@Before("execution(* *..BookServiceimpl.*(..))")
public void check(){ System.out.println("前置通知/增強(qiáng):執(zhí)行系統(tǒng)的權(quán)限驗證"); }
@AfterReturning("execution(* *..BookServiceimpl.*(..))")
public void logPrint(){ System.out.println("后置通知/增強(qiáng):執(zhí)行日志的打印"); }
@AfterThrowing("execution(* *..BookServiceimpl.*(..))")
public void exeption(){ System.out.println("異常通知/增強(qiáng):做出異常的通知"); }
@After("execution(* *..BookServiceimpl.*(..))")
public void distory(){ System.out.println("最終通知/增強(qiáng):資源的釋放"); }
public interface BookService {
void add();
void del();
void update();
void find();
@Component
public class BookServiceimpl implements BookService {
@Override
public void add() {
System.out.println("添加");
}
@Override
public void del() {
System.out.println("刪除");
}
@Override
public void update() {
System.out.println("修改");
}
@Override
public void find() {
System.out.println("查詢");
}
}
spring。xml代碼文章來源:http://www.zghlxwxcb.cn/news/detail-410938.html
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.lei"/>
<aop:aspectj-autoproxy/>
</beans>
測試類文章來源地址http://www.zghlxwxcb.cn/news/detail-410938.html
@Test
public void test01(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
BookService bookService=context.getBean(BookService.class);
bookService.add();
bookService.del();
bookService.find();
bookService.update();
}
到了這里,關(guān)于spring框架注解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!