Spring註解之xml配置中的和

2021-07-26 09:06:31 字數 2870 閱讀 4702

首先從說起

他的作用是式地向

spring 

容器註冊

autowiredannotationbeanpostprocessor、

commonannotationbeanpostprocessor、

persistenceannotationbeanpostprocessor、

requiredannotationbeanpostprocessor

註冊這4

beanpostprocessor

的作用,就是為了你的系統能夠識別相應的註解。說白了,如果你想使用

@autowired

註解,那麼就必須事先在

spring

容器中宣告

<

bean

class

="org.springframework.beans.factory.annotation. autowiredannotationbeanpostprocessor "

/>

如果想使用

@ resource 

、@ postconstruct

、@ predestroy

等註解就必須宣告

commonannotationbeanpostprocessor

如果想使用

@persistencecontext

註解,就必須宣告

persistenceannotationbeanpostprocessor

的bean。

如果想使用

@required

的註解,就必須宣告

requiredannotationbeanpostprocessor

的bean

。同樣,傳統的宣告方式如下:

<

bean

class

="org.springframework.beans.factory.annotation.requiredannotationbeanpostprocessor"

/>

一般來說,這些註解我們還是比較常用,尤其是

antowired

的註解,在自動注入的時候更是經常使用,所以如果總是需要按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,於是

spring

給我們提供

的簡化配置方式,自動幫你完成宣告。

但是你可能也注意到了現在的註解很多,僅僅這幾個註解恐怕是不夠的,不怕一萬就怕萬一。。。。。。。。。。。。。。

比如這幾個就沒有:@component,@service,@repository,@controller

spring 2.5引入了更多典型化註解(stereotype annotations):

@component@service

@controller@component是所有受spring管理元件的通用形式;

@repository@service

@controller則是@component的細化,用來表示更具體的用例(例如,分別對應了持久化層、服務層和表現層)。也就是說,你能用@component來註解你的元件類,但如果用@repository@service@controller來註解它們,你的類也許能更好地被工具處理,或與切面進行關聯。例如,這些典型化註解可以成為理想的切入點目標。當然,在spring framework以後的版本中,

@repository@service

@controller也許還能攜帶更多語義。如此一來,如果你正在考慮服務層中是該用@component還是@service,那@service顯然是更好的選擇。同樣的,就像前面說的那樣,

@repository已經能在持久化層中進行異常轉換時被作為標記使用了。」

其他的先不說就單單說他的包過濾就感覺很好:

提供兩個子標籤:和各代表引入和排除的過濾。 如:

filter標籤在spring3有五個type,如下:

filter typeexamples expressiondescription

annotation

org.example.someannotation

符合someannoation的target class

assignable

org.example.someclass

指定class或inte***ce的全名

aspectj

org.example..*service+

aspectj語法

regex

org\.example\.default.*

regelar expression

custom

org.example.mytypefilter

spring3新增自訂type,實作org.springframework.core.type.typefilter

綜上所述:一般的話還是使用吧!因為他不僅包含了還包含了其他的註解,特別是現在springmvc這麼火的時候。。。

Spring使用註解代替xml配置

component user 相當於public class user service user 和 component效果一樣,用於區分不同層的註解 controller user repository user component user 相當於 scope prototype 預設值為sin...

spring註解機制和XML配置機制之間的比較

xml配置的優缺點 優點有 1.xml配置方式進一步降低了耦合,使得應用更加容易擴充套件,即使對配置檔案進一步修改也不需要工程進行修改和重新編譯。2.在處理大的業務量的時候,用xml配置應該更加好一些。因為xml更加清晰的表明了各個物件之間的關係,各個業務類之間的呼叫。同時spring的相關配置也能...

Spring中多個 xml配置

在spring中,如果spring中的的配置檔案比較多又需要在tomcat啟動的時候自動載入,那麼在web.xml中配置的時候配置一下啟動的順序。那麼有乙個很重要的問題,怎麼講那麼多的spring配置檔案配置到web.xml中呢?技巧如下 remoting org.springframework.w...