spring註解和元件掃瞄配置

2021-08-31 12:58:15 字數 616 閱讀 6013

spring的部分bean在xml中配置,但我覺得action,service,dao這些完全沒必要在xml中配置,一來如果ide支援不好容易配置錯誤,二來這些類多起來後簡直就沒辦法看。故採用註解配置這三層,基本配置配好後可以不動xml配置檔案了。

一、首先新增註解和元件掃瞄配置。在spring配置檔案中加入:

二、在action,service,dao這三層的實現類中分別對應註解controller,service,repository如果你不想邏輯上有所區分就直接用component。

**類似於:

@controller

public class myaction extends baseaction

@service

public class myservice extends baseservice

@repository

public class mydao extends basedao

@component

public class my*** extends ba***xx

spring 註解掃瞄bean配置

spring公共配置 spring 會自動掃瞄com.zghw下的spring註解 常用註解 定義bean的註解 controller controller bean的名稱 定義控制層bean,如action service service bean的名稱 定義業務層bean repository ...

spring 註解 配置元件

configuration bean scope lazy conditional import 載入指定包,預設載入 component controller service repository 限定載入範圍 componentscan value com.example.demo includ...

spring 元件掃瞄

有的時候我們的xml中有過多的配置,很繁瑣,為了簡化配置我們使用context中的元件掃瞄的方式,將一部分類直接交付給spring管理。xml中的配置如下。xmlns xmlns xsi xmlns context xsi schemalocation spring beans.xsd spring...