多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > php教程 > Spring概念簡介、bean掃描與注冊實現(xiàn)方式

Spring概念簡介、bean掃描與注冊實現(xiàn)方式

來源:程序員人生   發(fā)布時間:2016-09-29 09:04:02 閱讀次數:3391次

1、概念部份:

1、spring概念:網上有很多

2、spring核心:IOC(DI)和AOP

3、IOC:控制反轉,控制權的轉移,利用程序本身不負責依賴對象的創(chuàng)建和保護,而是由外部容器負責創(chuàng)建和保護,只是負責使用

解釋1下就是:原來你在A類里面使用B類,需要new B(),現(xiàn)在不用new了,new對象的進程交給外部容器(Spring容器,它把所有的對象都稱作為Bean)實現(xiàn)控制權轉移,A類只是負責使用

4、DI:依賴注入,是IOC的1種實現(xiàn)方式,目的:創(chuàng)建對象并且組裝對象之間的關系

5、創(chuàng)建對象并且組裝對象之間的關系,這是兩個進程:
1)
、創(chuàng)建對象可以稱為bean的掃描、注冊,可通過xml配置和注解兩種方式實現(xiàn)
2)、組裝對象之間的依賴關系稱為注入,注入方式1般分為:setter注入和構造器注入,根據情勢不同又分為xml配置注入、xml配置自動裝配、注解自動裝配

6、AOP:面向切面編程,具體概念略,實現(xiàn)看后續(xù)整理

2、bean的掃描、注冊

1、xml配置(schema)方式,手動掃描

<?xml version="1.0" encoding="UTF⑻"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context⑶.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop⑶.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc⑶.0.xsd"> <!-- 定義注冊User的bean,唯1名稱為user --> <bean id="user" class="com.test.User"></bean> <!-- 定義注冊Dept的bean,唯1名稱為dept --> <bean name="dept" class="com.test.Dept"></bean> </beans>
2、注解方式,自動掃描

1)、現(xiàn)在spring的xml文件中開啟注解掃描和配置掃描的范圍:<context:component-scan base-package="">標簽

<context:component-scan base-package="com.test"> <!-- 只掃描com.test包及子包下的注解為Service的類,而過濾注解為Controller的類 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
延伸部份:在spring中,<context:annotation-config/>標簽作用也是開啟注解,它與<context:component-scan/>標簽的區(qū)分是甚么(還有1個’’)???

<context:annotation-config/> 標簽告知Spring到bean類中尋覓1些annotation定義的類, 比如@Autowired @PostConstruct @PreDestroy @Resource 等。
需要注意的是它并沒有激活@Transactional 和 @TransactionAttribute

<context:component-scan/>標簽告知Spring搜索指定包下面和1些需要被自動注入的bean,比如@Component @Repository @Service @Controller,而<context:component-scan>標簽功能包括<context:annotation-config>的功能。

<mvc:annotation-driven/>這個標簽的作用之1就是在springMVC中告知Spring去檢測RequestMapping。其他的作用以下:
- 激活@ExceptionHandler這個annotation
- 配置了這個標簽還可以將RequestMappingHandlerAdapter注冊到Spring中
- 是SpringMVC提供默許的類型轉化,由于我們沒有在<mvc:annotation-driven/> 的屬性中配置ConversionService。


1、xml配置注入,手動裝配,提供setter方法或constructor構造函數

<bean id="userDao" class="com.jsun.test.springDemo.ioc.User.UserDaoImpl"></bean> <bean id="userService" class="com.jsun.test.springDemo.ioc.User.UserServiceImpl"> <!-- 配置注入屬性 --> <property name="userDao" ref="userDao"></property> </bean>

//setter方式::聲明UserDao屬性,名字與xml中property的name名稱不具有關聯(lián)性,比如可以叫userDaoAlias private UserDao userDao; //setter方法,其中setXxxx的Xxxx要與xml文件中property標簽中name屬性值1致,首字母大寫,具有關聯(lián)性 public void setUserDao(UserDao userDao) { System.out.println("setUserDao注入"); this.userDao = userDao; }

<bean id="userDao" class="com.jsun.test.springDemo.ioc.User.UserDaoImpl"></bean> <bean id="userService" class="com.jsun.test.springDemo.ioc.User.UserServiceImpl"> <!-- 配置注入屬性 --> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>

//構造器方式::聲明UserDao屬性,屬性名與<constructor-arg>標簽中name屬性值無關 private UserDao userDao; //參數名與<constructor-arg>標簽中name屬性值具有關聯(lián)性 public UserServiceImpl(UserDao userDao){ System.out.println("構造器UserServiceImpl注入"); this.userDao = userDao; }


2、xml配置注入,自動裝配,提供setter方法或constructor構造函數

<bean id="userDao" class="com.jsun.test.springDemo.ioc.User.UserDaoImpl"></bean> <!-- 注冊bean并自動裝配所有屬性bean --> <bean id="userService" class="com.jsun.test.springDemo.ioc.User.UserServiceImpl" autowire="byName/byType/constructor"></bean>

1、byName:把Bean的屬性具有相同名字的的其他Bean自動裝配到Bean的對應屬性中,采取setter方式,set***()

2、byType:把與Bean的屬性具有相同類型的的其他Bean 自動裝配Bean的對應屬性當中,如果存在多個該類型bean,那末拋出異常,并指出不能使用byType進行自動裝配;如果沒有找到相匹配的bean,則甚么事都不產生,采取setter方式

3、constructor:把與Bean的構造器入參具有相同類型的其他Bean自動裝配到構造器的對應入參中,如果容器中沒有找到與構造器參數類型1致的bean,那末拋出異常,采取構造器方式

注意:在利用上下文(spring容器)中,如果有多個bean的類型與該bean的自動裝配屬性相匹配,那末就會出錯,由于byType方式只允許匹配1個類型相同的Bean。

4、補充:設置全局的默許自動裝配

<beans ... default-autowire="byName"> </beans>

</pre><p></p><h2 id="3注解自動裝配可以不用提供setter方法或constructor構造函數">3、注解自動裝配,可以不用提供setter方法或constructor構造函數</h2><p></p><p>使用@Autowired注解,可以用在屬性、setter方法、constructor構造方法上,實現(xiàn)自動裝配。</p><p>spring的xml配置文件:</p><p></p><pre name="code" class="html"><!-- 開啟注解配置 --> <context:annotation-config></context:annotation-config> <!-- 注冊bean --> <bean id="userDao" class="com.jsun.test.springDemo.ioc.User.UserDaoImpl"></bean> <!-- 注冊bean --> <bean id="userService" class="com.jsun.test.springDemo.ioc.User.UserServiceImpl"></bean>

1、注解在setter方法上:

2、constructor構造方法上:

3、聲明的屬性上:

4、補充:@Value注解基本類型數據或動態(tài)裝配數據

@Value("我就是注入strValue屬性的值") private String strValue;

使用表達式來動態(tài)的計算并裝配屬性的值,比如使用spel表達式從系統(tǒng)屬性中獲得1個值

@Value("#{systemProperties.myFavoriteSong}") private String strValue;

也能夠像使用EL表達式1樣,獲得spring上下文中加載的*.properties資源文件數據:

@Value("${jdbc.url}") private String url;

====================================================================================================

首先了解從spring2.5增加的新特性:

這些新特性包括:注解驅動的依賴性注入(annotation-driven dependency injection),使用注解而非XML元數據來自動偵測classpath上的Spring組件,注解對生命周期方法的支持,1個新的web控制器模型將要求映照到加注解的方法上,在測試框架中支持Junit4,Spring XML命名空間的新增內容,等等。

1.條件條件   引入context 的 Schema命名空間 在配置文件中添加context:annotation-config標簽

為了取得新的的特性  首先要引入新的context的Schema命名空間,該命名空間對注釋驅動、屬性文件引入、加載期織入等功能提供了便捷的配置。我們知道注釋本身是不會做任何事情的,它僅提供元數據信息。要使元數據信息真正起作用,必須讓負責處理這些元數據的處理器工作起來。

    <?xml version="1.0" encoding="UTF⑻"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context⑶.2.xsd
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans⑶.2.xsd
     http://www.springframework.org/schema/jdbc 
     http://www.springframework.org/schema/jdbc/spring-jdbc⑶.2.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx⑶.2.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop⑶.2.xsd">

    <!--

     這個配置隱式注冊了多個對注釋進行解析處理的處理器,代碼中就能夠直接使用@Autowired, @Required等annotaion了。 

  1. AutowiredAnnotationBeanPostProcessor 對應@Autowire,CommonAnnotationBeanPostProcessor 對應@Resource等,  
  2. PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor 對應@required

    -->

    <context:annotation-config />  

    </beans>

 

2.自動裝配屬性實例。

使用@Autowired或@Resource注解方式進行裝配,這兩個注解的區(qū)分是:

@Autowired 默許按類型裝配,@Resource默許按名稱裝配,當找不到與名稱匹配的bean才會按類型裝配。

強烈建議 放棄@Autowire 使用@Resource 緣由 spring支持標準

Spring支持JSR⑵50注解

 

即 Java EE5中引入了“Java平臺的公共注解(Common Annotations for the Java Platform)”,而且該公共注解從Java SE 61開始就被包括其中。

在2.5版本中,Spring框架的核心(core)現(xiàn)在支持以下JSR⑵50注解:

  • @Resource
  • @PostConstruct
  • @PreDestroy

結合Spring,這些注解在任何開發(fā)環(huán)境下都可使用——不管是不是有利用程序服務器——乃至是集成測試環(huán)境都可以。

 

使用方式:可以用來標注在字段或屬性的setter方法上.如果標注在字段上,則可以省略掉該屬性的getter 和setter方法。

同時

所要注入實例bean的名稱可以通過@Resource的name屬性指定,如果沒有指定name屬性,

1.當注解標注在字段上,即默許取字段的名稱作為bean名稱尋覓依賴對象

2.當注解標注在屬性的setter方法上,即默許取屬性名作為bean名稱尋覓依賴對象。

 

//用于字段上  

@Resource(name="personDao")  

 private PersonDaopersonDao;

 

//用于屬性的set方法上  

@Resource(name="personDao")  

public void setPersonDao(PersonDao personDao) { 

  this.personDao = personDao;  

 

注意:如果沒有指定name屬性,并且依照默許的名稱找不到依賴對象時, @Resource注解會回退到按類型裝配。但1旦指定了name屬性,就只能按名稱裝配了。

 

3.spring自動掃描機制

spring2.5為我們引入了組件自動掃描機制,它可以在classPath路徑底下尋覓標注了@Component、@Service、@Controller、@Repository注解的類,并把這些類納入進spring容器中管理。它的作用和在xml文件中使用bean節(jié)點配置組件是1樣的。

也就是要spring自動掃描機制只會查找指定類路徑下包括@Component、@Service、@Controller、@Repository這4種注解的類。

要使用自動掃描機制,我們需要打開以下配置信息:

1、引入context命名空間 需要在xml配置文件中配置以下信息: 同上先引入context 命名空間,同時

2、在配置文件中添加context:component-scan標簽

<context:component-scan base-package="*"/> 

其中base-package為需要掃描的包(含子包)。

注:

1、在使用組件掃描元素時,AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor會隱式地被包括進來。 也就是說,連個組件都會被自動檢測并織入 - 所有這1切都不需要在XML中提供任何bean配置元數據。也就是說如果使用了context:component-scan標簽,就能夠不需要再引入context:annotation-config標簽

 

<context:component-scan />還允許定義過濾器將基包下的某些類納入或排除。Spring支持以下4種類型的過濾方式:

過濾器類型 表達式范例 說明 
注解 org.example.SomeAnnotation 將所有使用SomeAnnotation注解的類過濾出來 
類名指定 org.example.SomeClass 過濾指定的類 
正則表達式 com\.kedacom\.spring\.annotation\.web\..* 通過正則表達式過濾1些類 
AspectJ表達式 org.example..*Service+ 通過AspectJ表達式過濾1些類 

正則表達式為例,我羅列1個利用實例:

Java代碼 
<context:component-scan base-package="com.casheen.spring.annotation"> 
    <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
</context:component-scan> 

        <context:component-scan base-package="com.casheen.spring.annotation">
                <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
        </context:component-scan>

值得注意的是<context:component-scan />配置項不但啟用了對類包進行掃描以實行注釋驅動Bean定義的功能,同時還啟用了注釋驅動自動注入的功能(即還隱式地在內部注冊了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此當使用<context:component-scan />后,就能夠將<context:annotation-config />移除。

2.為需要被掃描的類添加相應的注解,注解的類型有以下幾種:

@Service用于標注業(yè)務層組件、

@Controller用于標注控制層組件(如struts中的action)、

@Repository用于標注數據訪問組件,即DAO組件。

而@Component泛指組件,當組件不好歸類的時候,我們可使用這個注解進行標注。 這4種注解僅僅是角色不同,但實質都1樣。

細節(jié)問題總結:

1.當我們進行測試時,用ApplicationContext對象的getBean()方法尋覓組件。在之前的配置文件中我們會用<bean>標簽的id屬性去定義,在使用自動掃描注解后怎樣取得組建的id呢?

在這類情況下,Spring會將被標注注解的類名,然后再將該類名的第1個字母變成小寫,放到getBean()方法中。如:UserBizImpl類的組件Id就會是userBizImpl,獲得時為context.getBean("userBizImpl");

那末,我們在使用注解時可以自定義組件的Id嗎?

固然可以。我們需要在為相應的類添加注解時,在注解以后添加自定義的類名,例如:

@Service("userBiz")

public class UserBizImpl implements UserBiz {

……

}

當我們在獲得該組件時,為context.getBean("userBiz);

2.在配置文件中我們可以對組件(bean)的作用域范圍進行設置,它的默許值是單例模式,那末在添加注解的情況下,我們怎樣設置組件的作用域范圍呢?

我們可以直接在為類添加注解的同時,應用另外一個注解@Scope("prototype")來設置,以下

@Service("userBiz")@Scope("prototype")

public class UserBizImpl implements UserBiz {

……

}

3.在使用注解時,為組件設置初始化和燒毀方法:

在添加注解的相應的類中,如果想初始化或燒毀某個方法,我們可以直接在方法上添加注解,以下:

@PostConstruct

public void addItem() {

System.out.println("初始化方法");

}

@PreDestroy

public void testItem() {

System.out.println("釋放資源");

}

4.在使用Spring自動掃描組件后,怎樣進行依賴注入?

應用注解@Resource和@Autowired,并為依賴對象設置名稱,例如:

@Resource(name="userDao")

private UserDAO userDao = null;

首先它會根據名稱去找Spring自動掃描的并加入到Spring容器的組件(bean),如果有相同的名稱,則進行依賴注入,如果沒有相同的名稱。則會根據類型區(qū)尋覓組件。

 

理解以上的內容后,你就能夠很輕松的實現(xiàn)spirng零配置。

 

------------------------------------------------------------------------------------------------

項目后期開發(fā)工作 定義了大量的bean,現(xiàn)在需要為每一個數據庫操作添加 日志記錄,所以就定義了1個logBiz,

如果依照通常的做法,需要修改所有的配置文件 添加property屬性,現(xiàn)在使用自動注入機制。

在baseAction中添加通用日志方法,留出1個IogBiz接口,在繼承的子類action中,定義1個logBiz屬性 并用@Resouce 注解。便可。





生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲 欧美 日韩在线一区 | 欧美 日韩 亚洲 中文字幕 一区 | 视频网站高清免费 | 最近中文字幕免费完整 | 国产福利一区二区三区在线视频 | 最近中文字幕免费6 | 亚洲欧美日韩精品中文乱码 | 免费国产h视频在线观看 | 亚洲欧美偷拍另类 | 性欧美欧美另类巨大 | 成人亚洲在线观看 | 精品欧美成人高清在线观看2021 | 国产成人精品亚洲一区 | 国产免费久久精品久久久 | 精品国产看高清国产毛片 | 久久福利资源站免费观看i 久久高清一级毛片 | 最近最新的免费中文字幕 | 欧美jizz19性欧美 | 国产免费叼嘿网站免费 | 亚洲国产精品第一区二区 | 日韩中文字幕精品久久 | 久久亚洲人成网站 | freehdvideo性欧美 | 在线成人亚洲 | 欧美高清3dfreexxxx性 | 国产精品久久久久久免费 | 久久手机看片 | 一级毛片视频在线观看 | 老外一级毛片免费看 | 亚洲精品国产一区二区图片欧美 | 久久综合国产 | 西欧free性video巴西 | 日韩精品欧美激情国产一区 | 在线观看中文字幕码2022 | 亚洲资源站 | 91精品一区二区三区在线观看 | 性欧美18一19sex性高清播放 | 久久精品国产第一区二区 | h网站在线看 | 国产亚洲一区二区三区不卡 | 手机视频在线 |