spring配置方式

2021-07-11 06:29:14 字數 1163 閱讀 4110

spring中配置方式可以完全使用註解也可以完全使用註解實現,但是大部分都是用xml加註解,這樣不僅兼顧了效率,也提高了程式的可讀性:

但是需要注意的是:

純註解實現時:需配置掃瞄:

純xml開發就不用講解了:

xml加註解時需要注意的是:

所有的bean物件都要在xml中配置,需要注入時,我們使用註解:

@autowired 和 

@resource 

進行使用

@service

@controller

@repository()不再建議使用了

這時需要配置:

掃瞄的效率比純註解高

spring 整合junit:

package com.leige.an;

import org.junit.test;

import org.junit.runner.runwith;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.test.context.contextconfiguration;

import org.springframework.test.context.junit4.springjunit4classrunner;

/** * @author 使用spring整合junit開發時,我們不需要再寫從xml獲取上下文,得到bean之類的**,

* 我們只需要,配置@runwith(springjunit4classrunner.class),表示整合springjunit

* @contextconfiguration(locations="classpath:com/leige/an/beans.xml")表示從當前類下查詢

* 否則會在src下查詢

* 使用@autowired

*按照型別注入

使用註解方式配置Spring

在配置檔案中新增如下 spring會自動掃瞄改包下的所有註解 component user 當分不清時用它 controller user web層 service user service層 repository dao層 不寫名稱預設是類名小寫 scope scopename prototype...

spring 註解方式配置Bean

spring能夠從classpath下自動掃瞄,偵測和例項化具有特定註解的元件特定元件包括 component 基本註解,標示了乙個受spring管理的bean元件 respository 標識資料訪問層 dao層 元件 service 標識服務層 業務層 元件 controller 標識控制層 表...

spring 配置事務 配置檔案方式

xmlns xsi xmlns aop xmlns tx xmlns jdbc xmlns p xsi schemalocation spring beans 3.0.xsd spring jdbc 3.0.xsd spring aop 3.0.xsd spring tx 3.0.xsd destr...