天天看點

Spring中經常使用的幾個注解

@Primary:在相同的bean中,優先選擇用@Primary注解的bean(該注解寫在各個bean上)

@Qualifier:在衆多相同的bean中,@Qualifier指定需要注入的bean(該注解跟随在@Autowired後)

@Configuration标注在類上,相當于把該類作為spring的xml配置檔案中的《beans》,作用為:配置spring容器(應用上下文)

以下兩種方式等價

方式一:

ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);

方式二:

ApplicationContext context = new

ClassPathXmlApplicationContext(“spring-context.xml”);

@ApiOperation注解不是Spring的,它是swagger裡的,

@ApiOperation是用來建立Api說明文檔的

使用方式:

@ApiOperation(value = “接口說明”, httpMethod = “接口請求方式”, response =

“接口傳回參數類型”, notes = “接口釋出說明”;其他參數可參考源碼;

@Component 通用元件

@Service 業務層協定

@Respository 持久層元件

@Controller 控制層元件

@Repository 為持久層元件注解。

@Autowired、@Inject 為指定Bean之間依賴關系的注解。

@Value 為注入Spring表達式值的注解。

@Scope(“prototype”) Bean的應用域注解

##延遲加載

@Lazy(true)

##生命周期

@PostConstruct 初始化

@PreDestroy 銷毀

注意:這兩個注解來自于javaee,需要導入annotation.jar檔案

@Value("#(spring表達式)")

繼續閱讀