1. 程式人生 > >spring常用註解配置

spring常用註解配置

1、直接上乾貨:

<context:component-scan base-package="com.changhf" use-default-filter="true">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service">
</context:component-scan>

在com.changhf包及其子包中,如果一個類的頭上帶有 @Component/@Repository/@Service/@Controller 中的一個,就會將這個物件宣告為bean加入容器中。spring會自動掃描com.changhf路徑下的註解。

use-default-filter預設為true,通過配置filter屬性會包含或去掉com.changhf路徑下的指定的註解,如果想只包含某個註解,將該屬性設為false。

常用註解

宣告在類上的註解,通常如果不指定value,預設的bean名字為這個類的類名首字母小寫。如@Controller("userAction")或@Controller(value="UserAction")

@RestController是一個組合註解,相當於@Controller和@ResponseBody,返回的是json或xml.

@Scope 預設是單例模式(singleton)

struts2 是要求每次次訪問都對應不同的Action, @Scope("prototype")可以保證當有請求的時候都建立一個新的Action物件。spring中的bean預設scope為singleton,即IOC容器中只會存在一個共享的bean例項;而單例設計模式表示一個ClassLoader中只有一個class存在.

@Service
CmsUserService.java在bean中的id是"cmsUserService",即類名且首字母小寫。如果不想用這種形式,可以通過@Service("userService"),bean的id修改為userService

@Repository 資料訪問層Bean

mybatis中dao只定義了介面,並沒有實現類,如果想把它聲明瞭為spring容器的bean,只有通過配置檔案了。這段配置會掃描com.cmcc.akso.dao下的所有介面,然後建立各自介面的動態代理類

<!-- mybatis mapper scan -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.cmcc.akso.dao" />
	<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
</bean>

@Autowired與@Resource

@Autowired自動裝配,其作用是為了消除Java程式碼裡的getter/setter與bean屬性中的property,簡化程式碼。 如果注入的bean找不到但又不想讓Spring容器丟擲異常,可以設定@Autowired的required屬性為false即可,@Autowired(required=false)

兩個註解的區別:

  • @Autowired預設byType進行bean匹配,@Resource預設byName方式進行bean匹配
  • 匹配指定的實現類

@Autowired註解,通過 @Qualifier來指定bean的名稱,二者結合使用處理同一介面有多個實現類的情況

@Autowired
@Qualifier("cmsUserService2")
private UserService cmsUserservice;

@Resource 預設通過byName方式去匹配bean,找不到再通過byType去匹配,如果指定了name屬性按照name指定的值去匹配

@Resource
UserService userService;
// @Resource(name = "adminService")
// UserService userService;
  • @Autowired是Spring的註解,@Resource是J2EE的註解,Spring屬於第三方的,J2EE是Java自己的東西,建議使用@Resource註解,以減少程式碼和Spring之間的耦合。

@PostConstruct 和 @PreDestroy 方法 實現bean的初始化和銷燬時進行的操作,詳細參考bean的初始化與銷燬

@Lazy(true) 表示延遲初始化

@Async非同步方法呼叫

@Value("${memercached.servers}")這種形式不需要指定具體載入物件,這時候需要一個關鍵的物件來完成

<!-- 定義受環境影響易變的變數 -->
<bean
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
	<property name="ignoreResourceNotFound" value="false" />
	<property name="locations">
		<list>
			<value>classpath*:/config.properties</value> 
		</list>
	</property>
</bean>

config.properties文件中新增

memercached.servers=${env.memercached.servers}

env.memercached.servers配置在pom.xml檔案裡

自己在寫專案中,發現有時@Value無法取到config.properties檔案裡的值,經排查,以上bean元件需要在spring-mvc.xml和spring.xml各配置一份

bean的裝配

@Component泛指元件,當元件不好歸類的時候,我們可以使用這個註解進行標註。不建議使用。

@Primary:裝配時如果出現多個型別相同的Bean,被註解為@Primary的Bean將作為首選者,否則將丟擲異常,結合@Component使用,配置多資料來源時需要為其中一個配置該註解

相關推薦

spring常用註解配置

1、直接上乾貨: <context:component-scan base-package="com.changhf" use-default-filter="true"> <context:include-filter type="annotation

Spring常用註解匯總

無法 pool 也會 ext XML eth ref 可選 xsd 使用註解之前要開啟自動掃描功能 其中base-package為需要掃描的包(含子包)。 1 <context:component-scan base-pack

spring常用註解(轉載http://elf8848.iteye.com/blog/442806)

odin Coding spa -c tle 類型 epo sta frame 1 引入context命名空間(在Spring的配置文件中),配置文件如下: Xml代碼 xmlns:context="http://www.springframework.org/

Spring常用註解用法總結

ams 依賴 對象 strong 設置 osi puts 渲染 多個 轉自http://www.cnblogs.com/leskang/p/5445698.html 1、@Controller 在SpringMVC 中,控制器Controller 負責處理由Dispatch

spring學習筆記四:spring常用註解總結

bean logs single 配置文件 屬性註入 ring 如果 let ons 使用spring的註解,需要在配置文件中配置組件掃描器,用於在指定的包中掃描註解 <context:component-scan base-package="xxx.xxx.xxx

spring常用註解

http ase cto man proxy tar details aspect 綁定 原文鏈接:http://blog.csdn.net/sudiluo_java/article/details/51858978 Spring 註解學習 聲明Bean的註解:

Spring使用註解配置依賴註入

Java開發 Spring框架 依賴註入 註解 大部分情況下,使用Spring配置依賴註入時,都是使用註解來進行配置,因為註解比xml要方便和簡單。不過類似於數據源對象這種配置信息容易變更的對象除外,這種對象使用xml文件來進行配置會更適合,方便於在外部進行修改,而不需要打開代碼來進行修改。

Spring常用註解總結

doc exc ise bject factor 功能 一個 ets -s 傳統的Spring做法是使用.xml文件來對bean進行註入或者是配置aop、事物,這麽做有兩個缺點:1、如果所有的內容都配置在.xml文件中,那麽.xml文件將會十分龐大;如果按需求分開.xml文

spring常用註解筆記

場景 HERE 需要 數據庫表 過多 文件中 取數 微服務 int spring常用註解解釋: 1. Mybatis的映射文件xxxMapper.xml中resultMap標簽的作用 resultMap標簽是為了映射select查詢出來結果的集合,其主要 作

12 種 Spring 常用註解

1.宣告bean的註解 @Component 元件,沒有明確的角色 @Service 在業務邏輯層使用(service層) @Repository 在資料訪問層使用(dao層) @Controller 在展現層使用,控制器的宣告(C) 2.注入bean的註解 @Autow

Java 必須掌握的 12 種 Spring 常用註解

1.宣告bean的註解 @Component 元件,沒有明確的角色 @Service 在業務邏輯層使用(service層) @Repository 在資料訪問層使用(dao層) @Controller 在展現層使用,控制器的宣告(C) 2.注入bean的註解

Spring常用註解介紹

Spring的一個核心功能是IOC,就是將Bean初始化載入到容器中,Bean是如何載入到容器的,可以使用Spring註解方式或者Spring XML配置方式。 Spring註解方式減少了配置檔案內容,更加便於管理,並且使用註解可以大大提高了開發效率! 下面按照分類講解Spring中常用

Spring AOP註解配置demo

https://blog.csdn.net/yhl_jxy/article/details/78815636#commentBox   一 AOP的概念    AOP(Aspect Oriented Programming),即為面向切面程式設計。在軟體開發中,散佈於應用中多

spring常用註解作用小結

1、@controller 控制器(注入服務) 2、@service 服務(注入dao) 3、@repository dao(實現dao訪問) 4、@component (把普通pojo例項化到spring容器中,相當於配置檔案中的) @Component,@Service,@Control

Java 必須掌握的 12 種 Spring 常用註解

1.宣告bean的註解 // 元件,沒有明確的角色 @Component // 在展現層使用,控制器的宣告(controller 層) @Controller // 在業務邏輯層使用(service層) @Service // 在資料訪問層使用(dao 層) @Reposit

spring boot去除掃描自動注入依賴方法——Spring常用註解使用方法

問題: 最近做專案的時候,需要引入其他的jar。然後還需要掃描這些jar裡的某些bean。於 是使用註解:@ComponentScan 這個註解直接指定包名就可以,它會去掃描這個包下所有的class,然後判斷是否解析: 原始碼: public @interface SpringBoo

Spring常用註解小結

版權宣告:本文為章魚哥原創文章,若要轉載,請註明出處 https://blog.csdn.net/qq_40388552/article/details/84777234  以往我們使用Spring框架進行開發,使用xml檔案來對bean進行注入或者是配置aop、事物,慢慢我們發現是在是太麻

最全Spring常用註解詳解

我們在開發的時候,會看到各種的註解,如果不細細研究下,傻傻的分不清楚,更談不上怎麼合理的利用了,接下來我們一起看下。 研究順序,從最常用的來,follow me [email protected]註解 用於標註控制層元件(如struts中的action)。如果@Controller不指

spring--常用註解

@Controller @Service @Repository @Component 都是用來把一個類宣告為spring中的bean bean的名稱預設是 類名稱的首字母小寫 如果要自定義bean的名稱,則可以給註解加上value屬性:@Controller(value='xxxx') @Contr

Spring學習總結(27)——Spring常用註解再總結

1、宣告Bean的註解 @Component  元件,沒有明確的角色。 @Service  在業務邏輯層使用(service層)。 @Repository  在資料訪問層使用(dao層)。 @Controller  在展現層使用,控制器的宣告(Controller層)