国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
spring2中基于注釋的配置方式淺析
注:annotation-based configuration是一把雙刃劍,與xml-based configuration相比,有利有弊,其中利弊不在本文討論的范圍內(nèi),不再多說,僅提醒各位根據(jù)自身的需要謹(jǐn)慎選擇。
使用Annotaion似乎成了時(shí)尚和趨勢,spring2.5提供了一些Annotation,使開發(fā)者可以簡化我們的配置文件。本文簡單闡述一下spring2.5中的Annotation是如何使用的,不做深層次的研究。
一、配置文件可以簡化到什么程度?
使用annotation以前,我們要在xml配置文件中配置每一個(gè)我們用spring管理的bean,有十個(gè)要寫十個(gè),有一百個(gè)要寫一百個(gè)......
使用annotation之后,及至情況下,我們可以一個(gè)都bean都不寫,那么我們的配置文件就要寫成下面的樣子:
<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  [url]http://www.springframework.org/schema/beans/spring-beans-2.5.xsd[/url]
  [url]http://www.springframework.org/schema/context[/url]
  [url]http://www.springframework.org/schema/context/spring-context-2.5.xsd[/url]" default-autowire="byName" default-lazy-init="true">

  <context:annotation-config />
  <context:component-scan base-package="org.example">
  <context:include-filter type="regex" expression=".*Stub.*Repository"/>
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
  </context:component-scan>

</beans>
看看,上面沒有一個(gè)<bean>標(biāo)簽吧!下面簡單解釋一下下面的配置:
1) <beans>標(biāo)簽里面,以前我們用DTD,現(xiàn)在我們用的是XMLSchema-style,具體含義本文不講了(本文是“淺析”教程嘛)
2) <context:annotation-config />表示,我們要使用Annotation-based的bean管理方式。
3) <context:component-scan base-package="org.example">表示org.example包下的一些類使用Annotation-based的bean管理方式,換句話說就是在這個(gè)包下的Annotation才會(huì)起作用,其他包下的沒用。
4) <context:include-filter和<context:exclude-filter就是用正則表達(dá)式來include和exclude指定包(就是org.example)想的一下bean。
二、怎么什么聲明一個(gè)spring管理的bean?怎么注入bean?
聲明:在bean上加注釋@Component,例:
package org.example;
@Component("sampleBean")
public class SampleBean{
//......
}
注入:在field,seter等上加注釋@Autowired,例:
package org.example;
public class SampleBeanInjection{
private SampleBean sampleBean;
@Autowired
public setSampleBean(SampleBean sampleBean){
//......
}
//......
}
如上的例子不是很恰當(dāng),明眼人見諒。
三、有哪些Annotation可用?
在org.springframework.stereotype包下,有用于聲明bean的Annotation:
Component
Controller
Repository
Service
在org.springframework.beans.factory.annotation包下,有用于注入bean的Annotation:
Autowired
Qualifier
Required
在JSR-250的規(guī)范中(即j2ee的javax.annotation包下),有@Resource用來注入bean,其功能與@Autowired相似。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Spring依賴注入:注解注入總結(jié)
使用 Spring 2.5 注釋驅(qū)動(dòng)的 IoC 功能
Quartz與Spring的整合
主題:Spring注解入門
Spring 2.5 注解 知識點(diǎn) - jackpk的專欄 - CSDN博客
Spring Annotation 詳解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服