欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

零基礎(chǔ)入門學(xué)習(xí)——Spring Boot注解(一)

 更新時(shí)間:2017年05月06日 11:11:39   投稿:mrr  
這篇文章主要介紹了Spring Boot注解學(xué)習(xí)(一)要點(diǎn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧

聲明bean的注解:

@Component組件,沒有明確角色的bean
@Service,在業(yè)務(wù)邏輯層(service)中使用
@Repository,在數(shù)據(jù)訪問層(dao)中使用
@Controller,在展現(xiàn)層中使用
@Configuration聲明配置類

實(shí)體類無需添加注解,因?yàn)椴⒉恍枰白⑷搿睂?shí)體類

指定Bean的作用域的注解:

@Scope("prototype")

默認(rèn)值為singleton

可選值prototype、request、session、globalSession

聲明生成Bean的方法的注解:

@Bean 用在方法上,告訴Spring容器,你可以從下面這個(gè)方法中拿到一個(gè)Bean

使用AnnotationApplicationContext對(duì)象的getBean方法獲取Bean

注入Bean的注解:

@Autowired,自動(dòng)注入(默認(rèn)為byType型的注入),可以用在屬性或者方法上,可以通過設(shè)置required = "false"說明不要求一定要注入有多個(gè)同樣的接口的實(shí)現(xiàn)時(shí),通過@qualifier區(qū)分

當(dāng)注入的變量為L(zhǎng)ist后者M(jìn)ap時(shí),會(huì)把所有的接口實(shí)現(xiàn)都注入進(jìn)來,key為Bean的名字,value為實(shí)現(xiàn)類對(duì)象??梢酝ㄟ^在實(shí)現(xiàn)類上添加@order=1來指定加載順序,數(shù)越小越優(yōu)先加載

@Lazy啟動(dòng)延遲注入

配置類注解:

@Configuration聲明當(dāng)前類是一個(gè)配置類,相當(dāng)于Spring配置的一個(gè)xml文件
@ComponentScan,自動(dòng)掃描配置類所在包名下的所有bean
@EnableAutoConfiguration,啟動(dòng)自動(dòng)配置

在spring boot中這三個(gè)注解可以用一個(gè)@SpringBootApplication替代

@EnableTransactionManagement,開啟事務(wù)支持

事務(wù)管理:

@EnableTransactionManagement,加在配置類中,開啟事務(wù)支持
@Transactional,加在Service的方法上,標(biāo)注需要事務(wù)支持

AOP注解:

@AspectJ

任務(wù)調(diào)度:

@Scheduled用在需要定時(shí)執(zhí)行的方法上
@EnableScheduling用在需要使用的入口類上

Spring MVC集成:

首先需要對(duì)Application類進(jìn)行修改

@SpringBootApplication
@EnableTransactionManagement
//1、添加繼承SpringBootServletInitializer
public class Application extends SpringBootServletInitializer{
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
  @Override
  //2、重寫configure方法
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return super.configure(builder);
  }
}
  Spring MVC的注解:
@Controller,在展現(xiàn)層使用
@ResponseBody
@RestController

以上所述是小編給大家介紹的Spring Boot注解學(xué)習(xí)(一),希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論