日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

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

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

1、概念部份:

1、spring概念:網(wǎng)上有很多

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

3、IOC:控制反轉(zhuǎn),控制權(quán)的轉(zhuǎn)移,利用程序本身不負(fù)責(zé)依賴對(duì)象的創(chuàng)建和保護(hù),而是由外部容器負(fù)責(zé)創(chuàng)建和保護(hù),只是負(fù)責(zé)使用

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

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

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

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

2、bean的掃描、注冊

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

<?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、注解方式,自動(dòng)掃描

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

<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/>標(biāo)簽作用也是開啟注解,它與<context:component-scan/>標(biāo)簽的區(qū)分是甚么(還有1個(gè)’’)???

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

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

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


1、xml配置注入,手動(dòng)裝配,提供setter方法或constructor構(gòu)造函數(shù)

<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名稱不具有關(guān)聯(lián)性,比如可以叫userDaoAlias private UserDao userDao; //setter方法,其中setXxxx的Xxxx要與xml文件中property標(biāo)簽中name屬性值1致,首字母大寫,具有關(guān)聯(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>

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


2、xml配置注入,自動(dòng)裝配,提供setter方法或constructor構(gòu)造函數(shù)

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

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

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

3、constructor:把與Bean的構(gòu)造器入?yún)⒕哂邢嗤愋偷钠渌鸅ean自動(dòng)裝配到構(gòu)造器的對(duì)應(yīng)入?yún)⒅?,如果容器中沒有找到與構(gòu)造器參數(shù)類型1致的bean,那末拋出異常,采取構(gòu)造器方式

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

4、補(bǔ)充:設(shè)置全局的默許自動(dòng)裝配

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

</pre><p></p><h2 id="3注解自動(dòng)裝配可以不用提供setter方法或constructor構(gòu)造函數(shù)">3、注解自動(dòng)裝配,可以不用提供setter方法或constructor構(gòu)造函數(shù)</h2><p></p><p>使用@Autowired注解,可以用在屬性、setter方法、constructor構(gòu)造方法上,實(shí)現(xiàn)自動(dòng)裝配。</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構(gòu)造方法上:

3、聲明的屬性上:

4、補(bǔ)充:@Value注解基本類型數(shù)據(jù)或動(dòng)態(tài)裝配數(shù)據(jù)

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

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

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

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

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

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

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

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

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

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

    <?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">

    <!--

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

  1. AutowiredAnnotationBeanPostProcessor 對(duì)應(yīng)@Autowire,CommonAnnotationBeanPostProcessor 對(duì)應(yīng)@Resource等,  
  2. PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor 對(duì)應(yīng)@required

    -->

    <context:annotation-config />  

    </beans>

 

2.自動(dòng)裝配屬性實(shí)例。

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

@Autowired 默許按類型裝配,@Resource默許按名稱裝配,當(dāng)找不到與名稱匹配的bean才會(huì)按類型裝配。

強(qiáng)烈建議 放棄@Autowire 使用@Resource 緣由 spring支持標(biāo)準(zhǔn)

Spring支持JSR⑵50注解

 

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

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

  • @Resource
  • @PostConstruct
  • @PreDestroy

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

 

使用方式:可以用來標(biāo)注在字段或?qū)傩缘膕etter方法上.如果標(biāo)注在字段上,則可以省略掉該屬性的getter 和setter方法。

同時(shí)

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

1.當(dāng)注解標(biāo)注在字段上,即默許取字段的名稱作為bean名稱尋覓依賴對(duì)象

2.當(dāng)注解標(biāo)注在屬性的setter方法上,即默許取屬性名作為bean名稱尋覓依賴對(duì)象。

 

//用于字段上  

@Resource(name="personDao")  

 private PersonDaopersonDao;

 

//用于屬性的set方法上  

@Resource(name="personDao")  

public void setPersonDao(PersonDao personDao) { 

  this.personDao = personDao;  

 

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

 

3.spring自動(dòng)掃描機(jī)制

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

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

要使用自動(dòng)掃描機(jī)制,我們需要打開以下配置信息:

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

2、在配置文件中添加context:component-scan標(biāo)簽

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

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

注:

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

 

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

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

正則表達(dá)式為例,我羅列1個(gè)利用實(shí)例:

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 />配置項(xiàng)不但啟用了對(duì)類包進(jìn)行掃描以實(shí)行注釋驅(qū)動(dòng)Bean定義的功能,同時(shí)還啟用了注釋驅(qū)動(dòng)自動(dòng)注入的功能(即還隱式地在內(nèi)部注冊了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此當(dāng)使用<context:component-scan />后,就能夠?qū)?lt;context:annotation-config />移除。

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

@Service用于標(biāo)注業(yè)務(wù)層組件、

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

@Repository用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件。

而@Component泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可使用這個(gè)注解進(jìn)行標(biāo)注。 這4種注解僅僅是角色不同,但實(shí)質(zhì)都1樣。

細(xì)節(jié)問題總結(jié):

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

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

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

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

@Service("userBiz")

public class UserBizImpl implements UserBiz {

……

}

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

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

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

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

public class UserBizImpl implements UserBiz {

……

}

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

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

@PostConstruct

public void addItem() {

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

}

@PreDestroy

public void testItem() {

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

}

4.在使用Spring自動(dòng)掃描組件后,怎樣進(jìn)行依賴注入?

應(yīng)用注解@Resource和@Autowired,并為依賴對(duì)象設(shè)置名稱,例如:

@Resource(name="userDao")

private UserDAO userDao = null;

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

 

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

 

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

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

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

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





生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 色片免费在线观看 | 国产精品99久久久久 | 久久久久久国产精品免费免费 | 欧美精品网站 | 国产精久| 欧美 日韩 中文字幕 | 国产欧美一区二区三区国产幕精品 | 久久精品视频网站 | 亚洲人久久 | 久久曰| av一区在线 | 在线观看国产 | 欧美国产一区二区 | 亚洲欧美日韩在线 | 亚洲一区二区三区四区五区中文 | 久久精彩视频 | 欧美在线播放一区 | 国产精品久久久久免费 | 91精品成人久久 | 日韩欧美一 | 成人免费乱码大片a毛片软件 | 久久av在线 | 日本大黄视频 | 在线观看国产麻豆 | 久精品视频 | 亚洲精品电影在线观看 | 日韩av在线高清 | 国产视频一区二区三区四区 | 久久国产一区 | 欧美综合在线视频 | 黄色免费网站在线观看 | 日本久久网 | 久在线观看 | 国内精品国产三级国产在线专 | 久久国产免费 | 久久国产精品一区 | 中文字幕一区二区三区在线视频 | 一级看片 | 欧美综合第一页 | 欧美区一 | 日本国产精品视频 |