使用註解代替xml

2021-09-19 19:02:38 字數 1005 閱讀 5307

(1)導包(4+2+spring-aop)

(2)為主配置檔案引入新的命名空間(約束)

在catalog中匯入約束

為主配置檔案匯入context約束

(3)書寫主配置檔案

(4)在類中使用註解

/*@service("user")

@controller("user")

@repository("user")*/

@component("user")

public class user

未破壞封裝性,但是不方便看

兩種方式:

(1)自動裝配

@autowired

public void setcar(car car)

當car有兩個以上的物件時,可以配合以下註解使用

@autowired

@qualifier("car2")

public void setcar(car car)

(2)指定物件

@resource(name="car2")

public void setcar(car car)

@postconstruct

public void init()

@predestroy

public void destroy()

當屬性設值為:@

scope

(scopename=

"prototype"

) 時,不會執行銷毀方法,因為設值該屬性後,spring建立該物件後將不會再管

Spring使用註解代替xml配置

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

Spring可以不用xml,完全用註解代替

spring可以不用xml,完全用註解代替,不用單獨寫xml,用乙個配置類裝載bean就行了 pojo package com.cbbpp.pojo import org.springframework.beans.factory.annotation.value import org.spring...

使用Join代替In

我們知道,在sql中使用in讓我們的where子句可以規定多個值。當需要從乙個集合中查詢包含某幾個值的記錄的時候,通常我們會選擇使用in來實現,其實,使用join也可以實現這樣的功能,而且效能要比in好。我會從以下幾個方面來進行總結。使用in和join的效能對比 ef中如何使用join來代替in 首...