spring 註解驅動 01

2021-10-04 17:39:45 字數 1635 閱讀 2498

註解有:@controller 標註是乙個控制層

@service :標註是乙個業務層

@repository:標註是乙個持久層

@configuration:表示是乙個配置類

@componentscan:表示掃瞄包與spring bean.xml檔案中的掃瞄包效果一致

下面看是示例:

一:這是乙個配置類

package com.aaa.config;

import com.aaa.pojo.person;

import org.springframework.context.annotation.*;

import org.springframework.stereotype.controller;

import org.springframework.stereotype.service;

@configuration //告訴spring這是乙個配置類

/*@componentscan指定掃瞄包

filter[

] includefilters 指定只掃瞄那些包

filter[

] excludefilters(

) 指定不掃瞄那些包

*//*

* excludefilters=)}

指定掃瞄的時候按照什麼規則排除那些元件

**includefilters =

)},usedefaultfilters =

false

指定只掃瞄那些包, @componentscan 預設是指定的包都生效

* */

/** @componentscans指定掃瞄策略 可以寫多個@componentscan

* 可以多寫幾個規則

* */

@componentscan(value =

"com.aaa"

)public class mainconfig

}

業務層:

package com.aaa.service;

import org.springframework.stereotype.service;

@service

public class bookservice

控制器層

package com.aaa.controller;

import org.springframework.stereotype.controller;

@controller

public class bookcontroller

package com.aaa.dao;

持久層import org.springframework.stereotype.repository;

@repository

public class bookdao

測試

//使用配置類進行除錯

@test

public void atest(

)}

總結:使用了這寫註解之後,就不需要寫spring的xml檔案了

方便了我們的開發,註解減少了**量。

Spring註解驅動開發 01

匯入依賴 spring context spring aop spring bean spring core commons logging spring expression 註解式開發 配置類 config 等同於配置檔案 configuration 告訴spring這是乙個配置類 bean 給...

Spring註解驅動開發 容器day01

1.回顧舊的方式 xml去開發 此處省略pom.xml檔案!person實體類 test類 package com.yikuan.test import org.junit.after import org.junit.before import org.junit.test import impo...

Spring 常用註解01

一 當配飾檔案為xml時,若要用註解,必須加以下 用於告知spring建立容器時要掃瞄的包。二 常用註解 分為以下類 用於建立物件的 相當於xml配置檔案中的標籤 component 作用 把當前類存入spring容器中 屬性 value用於指定bean的id。預設是首字母小寫的當前類名。當屬性只有...