第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
  • 控制反轉(zhuǎn)的解釋

    查看全部
    0 采集 收起 來源:IOC及Bean容器

    2020-09-25

  • AOP相關(guān)概念

    查看全部
  • aop實(shí)現(xiàn)方式

    預(yù)編譯AspectjJ

    SpringAOP

    JbossAOP


    查看全部
  • aop概念和主要功能

    查看全部
  • 框架和類庫

    http://img3.sycdn.imooc.com/5f6173290001a3bf11840522.jpg

    http://img1.sycdn.imooc.com//5f6173a70001b35911520526.jpg

    查看全部
    0 采集 收起 來源:Spring框架

    2020-09-16

  • 一、接口及面向接口編程

    接口:

    1、用于溝通的中介物的抽象化。(相當(dāng)于中間者,外部與接口溝通,但不與接口實(shí)現(xiàn)類進(jìn)行溝通)

    2、實(shí)體把自己提供給外界的一種抽象化說明,用以由內(nèi)部操作分離出外部溝通方法,使其能被修改內(nèi)部而不影響外界其他實(shí)體與其交互的方式(接口提供外部功能,至于內(nèi)部的具體實(shí)現(xiàn)不對外公開,比如:注冊,只需要提供手機(jī)號、密碼等信息就可以進(jìn)行注冊,具體怎么實(shí)現(xiàn)的,邏輯怎樣判斷的,數(shù)據(jù)存儲在哪里等具體實(shí)現(xiàn)邏輯不對外公開)

    3、對應(yīng)java接口即聲明,聲明了哪些方法是對外公開提供的,在Java8中,接口可以擁有方法體,(某種程度上和抽象類具有相似的功能)。

    面向接口編程:

    1、結(jié)構(gòu)設(shè)計中,分清層次及調(diào)用關(guān)系,每層只向外(上層)提供一組功能接口,各層間僅依賴接口而非實(shí)現(xiàn)類。(多態(tài)的體現(xiàn))

    2、優(yōu)點(diǎn):接口實(shí)現(xiàn)的變動不影響各層間的調(diào)用,這一點(diǎn)在向外提供公共服務(wù)或者在企業(yè)內(nèi)部提供公共服務(wù)中尤為重要,由于公共服務(wù),所以穩(wěn)定性一定很重要(對外提供接口保證了接口的穩(wěn)定性,對于接口的實(shí)現(xiàn)可以根據(jù)某一時期的具體情況或者某一時期具體邏輯的變化進(jìn)行調(diào)整)

    3、面向接口編程中的“接口”:用于隱藏具體實(shí)現(xiàn)和實(shí)現(xiàn)多態(tài)性的組件,面向接口編程在IOC中應(yīng)用非常廣泛(對外提供調(diào)用說明的,但不包含具體的實(shí)現(xiàn),具體的實(shí)現(xiàn)由接口實(shí)現(xiàn)類完成)

    二、什么是IOC

    IOC概念:(Inversion of Control)控制反轉(zhuǎn),控制權(quán)的轉(zhuǎn)移,應(yīng)用程序本身不負(fù)責(zé)依賴對象的創(chuàng)建和維護(hù),而是由外部容器負(fù)責(zé)創(chuàng)建(創(chuàng)建對象并且組裝對象之間的依賴關(guān)系)和維護(hù)(正常情況下使用對象則需要new對象,而使用外部容器之后,對象就被容器管理了,包括對象的創(chuàng)建、初始化、銷毀等,對象就可以直接從容器中直接拿過來用),IOC容器在初始化時,會創(chuàng)建一系列的對象,同時把對象之間的依賴關(guān)系通過注入的方式組織起來。(組裝:一個類A,和一個類B,類A把類B作為自己的屬性,當(dāng)IOC進(jìn)行實(shí)例化時,先實(shí)例化A,再實(shí)例化B,再把B注入到A中)

    比喻:我們手動去new一個對象,就像我們想要一所房子,我們自己手動的買材料去建造,而使用容器之后,就像找開發(fā)商,直接可以入住房子。

    DI概念:(Dependency Injection)依賴注入,控制反轉(zhuǎn)的一種實(shí)現(xiàn)方式,獲得依賴對象的過程由自身管理變?yōu)榱擞蒊OC容器主動注入,所謂依賴注入,就是由IOC容器在運(yùn)行期間,動態(tài)地將某種依賴關(guān)系注入到對象之中。

    IOC目的:創(chuàng)建對象并且組裝對象之間的關(guān)系(IOC容器在初始化時,會初始化一系列的對象,同時能把對象之間的依賴關(guān)系通過注入的方式組織起來)例如:一個類A里存在另一個類B的聲明,IOC會負(fù)責(zé)創(chuàng)建A和B的對象,并通過A的構(gòu)造方法注入B的實(shí)例。

    Spring官網(wǎng)給出IOC工作原理

    https://img3.sycdn.imooc.com/5ce9f4900001f86005910487.jpg

    描述IOC

    https://img2.sycdn.imooc.com/5ce9f6090001981108890469.jpg

    三、Spring的Bean配置

    1、在IOC容器中所有的對象都稱為Bean。

    2、Spring的Bean配置方式有兩種:基于XML配置、注解配置。

    3、XML中配置:<bean id="bean的唯一標(biāo)識",class="交給容器管理的類的路徑"></bean>

    四、Bean容器的初始化

    基礎(chǔ):兩個包:

    org.springframework.beans

    org.springframework.context

    beans提供BeanFactory及其子類,它提供配置結(jié)構(gòu)和基本功能,并加載初始化Bean

    context提供ApplicationContext及實(shí)現(xiàn)類保存了Bean對象并在Spring中被廣泛使用

    初始化ApplicationContext方式:

    1、加載本地文件(指定磁盤下某路徑下的xml文件)

    2、Classpath(相對路徑,相對項(xiàng)目的路徑)

    3、Web應(yīng)用中依賴servlet或Listener初始化Bean容器(兩種方式,任選其一)

    例子:

    https://img1.sycdn.imooc.com/5ce9fb7b000153dd11690635.jpg

    https://img4.sycdn.imooc.com/5ce9f7f700013bdc11130637.jpg


    查看全部
    1 采集 收起 來源:IOC及Bean容器

    2020-09-09

  • Resources:針對資源文件的統(tǒng)一接口,通過Spring加載一些資源文件的時候,可以通過它去控制。

    ——UrlResource:URL對應(yīng)的資源,根據(jù)一個URL地址即可構(gòu)建Resources。

    ——ClassPathResoure:獲取類路徑下的資源文件(相對路徑)。

    ——FileSystemResource:獲取文件系統(tǒng)里面的資源(絕對路徑)。

    ——ServletContextResource:ServletContext封裝的資源,用于訪問ServletContext環(huán)境下的資源。(和Web相關(guān)的資源文件的入口)

    ——InputStreamResource:針對于輸入流封裝的資源。(構(gòu)建它需要InputStream)

    ——ByteArrayResource:針對于字節(jié)數(shù)組封裝的資源。(構(gòu)建它需要ByteArray)

    ResourceLoader:對Resource加載的一個類,在SpringIOC容器中,所有的ApplicationContext都實(shí)現(xiàn)了ResourceLoader接口,所有的ApplicationContext都可以用來獲取Resource實(shí)例,所以可以通過getResource(String location)方法獲取資源Resource。

    ResourceLoader接口的聲明(有個方法,輸入為文件的位置,返回的是Resource的實(shí)例)

    https://img3.sycdn.imooc.com/5ceb51e40001570111520551.jpg

    ResourceLoader注入?yún)?shù)時前綴的幾種類型

    https://img4.sycdn.imooc.com/5ceb523d0001623e11430596.jpg


    ResourceLoader前綴:classpath:(相對路徑,加載文件)

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?file:(絕對路徑,加載文件)

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?url:??http(web路徑、加載文件)

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (none):直接輸入路徑,依賴ApplicationContext

    案例:(Bean通過實(shí)現(xiàn)ApplicationContext接口,間接的實(shí)現(xiàn)了ResourceLoader接口(加載Resource實(shí)例),就可以使用getResource()獲取Resource的實(shí)例,Resource擁有一系列的方法,比如獲取文件名稱(getFilename()和獲取文件長度contentLength())

    https://img1.sycdn.imooc.com/5ceb595a000150b106550460.jpg

    步驟1:


    public class ResourceDemo implements ApplicationContextAware {

    private ApplicationContext ac;

    @Override

    public void setApplicationContext(ApplicationContext ac) throws BeansException {

    this.ac=ac;

    }

    public void resource() throws IOException{

    Resource r=ac.getResource("classpath:resourceDemo.txt");(直接寫文件,而不寫全路徑,是因?yàn)镴ava build path 配置了source,所以這里是相對路徑)

    System.out.println(r.getFilename());

    System.out.println(r.contentLength());

    }

    }



    步驟2:

    ? ?<bean id="resourceDemo" class="ResourceDemo" ></bean>


    步驟3:

    @Test

    public void testBean() throws IOException{

    ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");

    ResourceDemo rd=(ResourceDemo)ac.getBean("resourceDemo");

    rd.resource();

    }

    測試:

    @Test

    public void testBean() throws IOException{

    ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");

    ResourceDemo rd=(ResourceDemo)ac.getBean("resourceDemo");

    rd.resource();

    }


    結(jié)果:(文件:resourceDemo.txt,在src——>resource文件夾下)

    https://img4.sycdn.imooc.com/5ceb57080001728c07670132.jpg

    resourceDemo.txt

    6

    案例:file方式

    https://img1.sycdn.imooc.com/5ceb5a6a000110ac13380462.jpg

    案例:url方式

    https://img3.sycdn.imooc.com/5ceb5b050001bbcb13550479.jpg


    查看全部
  • Bean管理的注解實(shí)現(xiàn)及例子

    1、Classpath掃描與組件管理

    定義:從Spring3.0開始,Spring JavaConfig項(xiàng)目提供很多特性,包括通過注解定義Bean,而不是使用XML。

    @Component:通用型注解,可用于任何Bean。

    @Repository:通常用于注解DAO類,即持久類。

    @Service:通常用于注解Service類,即服務(wù)層。

    @Controller:通常用于注解Controller,即控制層。

    2、類的自動檢測與注冊Bean

    定義:Spring可以檢測到類上和屬性上以及方法上的注解,可以通過注解進(jìn)行實(shí)例化或注入。

    3、<context:annotation-config/>

    定義:基于XML配置,如果使用該標(biāo)簽,則在描述中添加約束。

    https://img1.sycdn.imooc.com/5ce224ed0001984d10780418.jpghttps://img3.sycdn.imooc.com/5ce2258c0001969a10690590.jpg<context:component-scan>:一般處理類上的注解,它的功能包含<context:annotation-config>的功能,比較常用。

    <context:annotation-config>:一般處理實(shí)例化Bean的屬性和方法的注解。

    SpringXML配置過濾器作用:掃描特定包下的特定注解。

    https://img1.sycdn.imooc.com/5ce252ca0001574811540623.jpg

    4、@Component,@Repository,@Service,@Controller

    5、@Required

    6、@Autowired

    7、@Qualifier

    8、@Resource



    查看全部
  • 一、可以使用@Autowired注解注入Spring提供的解析依賴性接口(ClassPathXmlApplicationContext、AnnotationApplicationContext、BeanFactory、Envieonment、ResourceLoader、ApplicatonEventPublisher、MessageSource)

    https://img4.sycdn.imooc.com/5cdfaf6d000172fe09080337.jpg二、可以對數(shù)組(集合)類型的屬性進(jìn)行注入,注入的是ApplicationContext中所有符合該類型或者是該類型的子類的(List<String>,則會把所有String類型的Bean注入List集合中)。

    https://img1.sycdn.imooc.com/5cdfb06800011e1407300134.jpg


    https://img1.sycdn.imooc.com/5cdfb2330001ae4208300172.jpg

    Key:Bean的Id。

    Value:Bean的對象。

    如果希望數(shù)組有序

    1、Bean實(shí)現(xiàn)org.springframework.core.Ordered接口

    2、@Order注解(只針對List)

    InterfaceImplTwo添加@Order(value=1)

    InterfaceImplOne添加@Order(value=2),先取出Two的實(shí)例,再取出One的實(shí)例。

    @Autowired是由Spring BeanPostProcessor處理的,所以不能在配置類中使用它,也就是說要注入集合類型的屬性,這些屬性的值只能是通過xml或者在配置類中使用@Bean注解加載。

    注入List<BeanInterface>代碼:


    public interface InjectInterface {


    }

    @Component

    public class InterfaceImplOne implements InjectInterface {


    }

    @Component

    public class InterfaceImplTwo implements InjectInterface {


    }

    @Configuration

    @ComponentScan("injectList")

    public class InterfaceConfig {

    }

    @Component(value="invoker")

    public class InterfaceInvoker {

    private List<InjectInterface> list;

    @Autowired

    public void setList(List<InjectInterface> list) {

    this.list = list;

    }

    public void say(){

    if(null!=list){

    for (int i=0;i<list.size();i++) {

    System.out.println(list.get(i));

    }

    }else{

    System.out.println("list為null");

    }

    }

    }

    @Test

    public void test(){

    ApplicationContext ac=new AnnotationConfigApplicationContext(InterfaceConfig.class);

    InterfaceInvoker ii=ac.getBean("invoker",InterfaceInvoker.class);

    ii.say();

    }

    結(jié)果:


    injectList.InterfaceImplOne@39f46204

    injectList.InterfaceImplTwo@5b4f1255

    注入Map類型<String,InjectInterface>類型屬性(和List相似)

    代碼:


    @Component(value="invoker")

    public class InterfaceInvoker {

    private Map<String,InjectInterface> map;

    @Autowired

    public void setMap(Map<String, InjectInterface> map) {

    this.map = map;

    }

    public void say(){

    if(map!=null&&map.size()>0){

    for (Entry<String,InjectInterface> ii: map.entrySet()) {

    System.out.println(ii.getKey()+" ? ? ?"+ii.getValue());

    }

    }

    }

    }


    查看全部
  • @Qualifier:一般和@Autowired一起使用,按類型自動注入時可能會有多個Bean實(shí)例的情況,可以使用@Qualifier注解縮小范圍(或指定唯一),也可用于指定單獨(dú)的構(gòu)造器或方法參數(shù)。

    代碼:


    @Component(value="invoker")

    public class InterfaceInvoker {

    private List<InjectInterface> list;

    @Autowired()

    @Qualifier(value="one")

    public void setList(List<InjectInterface> list) {

    this.list = list;

    }

    public void say(){

    if(null!=list){

    for (int i=0;i<list.size();i++) {

    System.out.println(list.get(i));

    }

    }else{

    System.out.println("list為null");

    }

    }

    }

    @Component(value="one")

    public class InterfaceImplOne implements InjectInterface {


    }

    @Component

    public class InterfaceImplTwo implements InjectInterface {


    }

    結(jié)果:

    injectList.InterfaceImplOne@54266750

    https://img3.sycdn.imooc.com/5cdfc1dd000166e609290571.jpgxml文件中實(shí)現(xiàn)@Qualifier功能(不通過@Component)

    https://img1.sycdn.imooc.com/5cdfc3000001bb3809890545.jpg


    https://img2.sycdn.imooc.com/5ce21aaa0001915711040544.jpg

    https://img1.sycdn.imooc.com/5ce21aaa0001fd8411840624.jpg

    https://img3.sycdn.imooc.com/5ce21aaa0001aed411500618.jpg

    https://img2.sycdn.imooc.com/5ce21aab00016b5111410662.jpg


    查看全部
  • Java的容器注解說明——@Bean

    @Bean:由SpringIoc容器配置和初始化對象,類似于XML配置文件的<bean/>,。(一般@Bean 需要在配置類中使用,即類上需要加上@Configuration注解)

    @Bean的name屬性:可以通過name屬性指定Bean的Id,相當(dāng)于XML中<bean>的Id。

    @Component:如果一個類上標(biāo)識該注解,表示配置類可以通過@ComponentScan("路徑名")掃描到該類,并實(shí)例化這個類。

    舉例:

    https://img1.sycdn.imooc.com/5cde2b130001cc0411390232.jpg

    @Bean的initMethod屬性、destroyMethod屬性:通過@Bean實(shí)例化Bean時要執(zhí)行的初始化和銷毀邏輯。

    https://img4.sycdn.imooc.com/5cde2ca6000112a505420527.jpg


    查看全部
  • Java容器注解@ImportResource和@Value

    一、通過xml文件進(jìn)行配置。

    <context:property-placeholder ?location="資源文件的存放位置"/>:<beans>進(jìn)行配置,作用是加載資源文件(properties——Key-Value類型資源文件),然后就可以通過${Key},引用資源文件中的內(nèi)容(如數(shù)據(jù)庫的加載)。

    https://img1.sycdn.imooc.com/5cdf62ef0001c2b207160259.jpg

    https://img4.sycdn.imooc.com/5cdf62ef00011ca911070325.jpg

    代碼:

    public class Store {

    public Store(String username,String password,String url){

    System.out.println(username);

    System.out.println(password);

    System.out.println(url);

    }


    <context:property-placeholder location="classpath:/config.properties"/>

    <bean id="store" class="Bean.Store">

    <constructor-arg name="username" value="${jdbc.username}"></constructor-arg>

    <constructor-arg name="password" value="${jdbc.password}"></constructor-arg>

    <constructor-arg name="url" value="${jdbc.url}"></constructor-arg>

    </bean>

    測試:

    @Test

    public void test(){

    ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");

    Store s=ac.getBean("store",Store.class);

    System.out.println(s);

    }

    二、通過注解進(jìn)行配置前提xml已經(jīng)編寫(配置類中添加@ImportResource("classpath:config.xml") )。

    步驟1:配置類中添加@ImportResource,配置類中定好要用到的屬性,并在屬性上添加@Value("$(Key)")。

    代碼:

    @Configuration

    @ImportResource("classpath:/spring-ioc.xml")

    public class StoreConfig {

    @Value("${url}")

    private String url;

    @Value("${username}") ? ? ? ? ???//取到的是當(dāng)前操作系統(tǒng)的用戶名,所以不要使用username這個名。

    private String username;

    @Value("${password}")

    private String password;

    @Bean(name="store")

    public Store getStoreBean(){

    return new Store(username,password,url);

    }

    }


    步驟二:這種注入依賴于使用@Bean標(biāo)簽,并返回有參構(gòu)造函數(shù)對象,通過參數(shù)對Bean進(jìn)行注入。

    https://img3.sycdn.imooc.com/5cdf66c100017edf11550661.jpg

    代碼:xml代碼:

    ? ? ? ? <beans>標(biāo)簽里添加xmlns:context="

    ? ? ? ?xsi:schemaLocation="http://www.springframework.org/schema/beans

    ? ? ? ? http://www.springframework.org/schema/beans/spring-beans.xsd

    ? ? ? ? http://www.springframework.org/schema/context

    ? ? ? ? http://www.springframework.org/schema/context/spring-context.xsd" ,

    <beans>中添加

    <context:property-placeholder location="classpath:/config.properties"/>

    java代碼:

    public class Store {

    public Store(String username,String password,String url){

    System.out.println(username);

    System.out.println(password);

    System.out.println(url);

    }

    配置文件:

    username=\u5C0F\u660E

    password=123456

    url=localhost



    查看全部
  • @Scope和@Bean

    @Bean:默認(rèn)是單例模式。

    @Scope:value屬性指定Bean的作用域范圍,proxyMode屬性指定使用的代理方式(包括接口的代理和類的代理)。

    代理方式主要有兩種:針對接口的代理、針對類的代理,實(shí)現(xiàn)方式有所區(qū)別。前者是jdk動態(tài)代理,后者是cglib代理。

    proxyMode:容器啟動時bean還沒創(chuàng)建?通過cglib代理這個接口或者類注入到其它需要這個bean的bean中


    查看全部
  • 基于泛型的自動裝配(Spring4新增的內(nèi)容,和SPI使用方式相似,只不過Spring提供了更簡便的使用方式。API:應(yīng)用編程接口。SPI:針對服務(wù)框架開發(fā)或者一些工具的基礎(chǔ)使用的特殊類型接口。)

    基于依賴對象泛型的自動裝配案例

    步驟1:

    public class IntegerStore {

    }

    public class StringStore{

    @Autowired

    private IntegerStore s2;

    public void print(){

    System.out.println("s2: ?"+s2);

    }

    }

    步驟2:

    @Configuration

    public class StoreConfig {

    @Bean

    public StringStore stringStore(){

    return new StringStore();

    }

    @Bean

    public IntegerStore integerStore(){

    return new IntegerStore();

    }

    }

    步驟4:測試

    @Test

    public void testStore(){

    ApplicationContext ac=new AnnotationConfigApplicationContext(StoreConfig.class);

    StringStore ss=ac.getBean("stringStore",StringStore.class);

    ss.print();

    }

    結(jié)果:(如果不加@Autowired注解,s2:為空)

    s2: ?IntegerStore@3323f4c5

    基于集合泛型的自動裝配(注意:不能使用@Autowired注解,要使用@Resource注解,并且使用@Resource(name="名字1"),去匹配@Bean(name="名字1")。

    步驟1:


    public class IntegerStore {

    }

    public class StringStore {

    public StringStore() {

    System.out.println("無參構(gòu)造方法執(zhí)行了");

    }

    private List<IntegerStore> list;

    @Resource(name = "ll")

    public void setList(List<IntegerStore> list) {

    this.list = list;

    }

    public void print() {

    for (IntegerStore store : list) {

    System.out.println(store);

    }

    }

    }

    步驟2:

    @Configuration

    public class StoreConfig {

    @Bean(name = "ll")

    public List<IntegerStore> ll() {

    List<IntegerStore> lis = new ArrayList<IntegerStore>();

    lis.add(new IntegerStore());

    lis.add(new IntegerStore());

    lis.add(new IntegerStore());

    return lis;

    }


    @Bean(name = "stringStore")

    public StringStore stringStore() {

    return new StringStore();

    }

    }

    測試:


    @Test

    public void testStore() {

    ApplicationContext ac = new AnnotationConfigApplicationContext(StoreConfig.class);

    StringStore ss = ac.getBean("stringStore", StringStore.class);

    ss.print();

    }

    }

    結(jié)果:


    無參構(gòu)造方法執(zhí)行了

    IntegerStore@2b2c8b14

    IntegerStore@795ee430

    IntegerStore@44d74990

    Autowired擴(kuò)展——自定義qualifier注解(自定義自動化注入配置)

    https://img1.sycdn.imooc.com/5ce8c92a00015b4211260626.jpg


    查看全部
  • Spring Bean裝配之Spring對JSR支持的說明

    1:Spring支持使用JSR-250@Resource注解,通常用于成員變量和set方法上,這是一種在JavaEE5和6的通用模式(當(dāng)然7也是支持的),Spring對對象的管理也支持這種方式。

    2:@Resource有一個name屬性,并且Spring默認(rèn)把該name作為被注入bean的名稱(如果沒有顯示指定name屬性,默認(rèn)名稱是屬性名或者set方法名,默認(rèn)是根據(jù)注解的位置)。

    3:注解提供的名字被解析為一個Bean的名稱(@Resource的name屬性),這個過程是由ApplicationContext中的CommonAnnotationBeanPostProcessor發(fā)現(xiàn)并處理的。

    @PostConstruct和@PreDestroy

    1:CommonAnnotationBeanPostProcessor不僅能識別JSR-250中的生命周期注解@Resource,在Spring2.5中引入支持初始化回調(diào)和銷毀回調(diào),前提是CommonAnnotationBeanPostProcessor這個類已經(jīng)在Spring的ApplicationContext中注冊了。(只有CommonAnnotationBeanPostProcessor類在IOC容器中注冊了,才能處理@Resource,@PostConstruct,@PreDestroy)。

    注意:自動注入,用Map<String,Object>map盛放所有的容器中的bean,然后循環(huán)打印所有的bean所屬的類就能看到這個類了org.springframework.context.annotation.CommonAnnotationBeanPostProcessor,有這個類的bean。

    @PostConstruct和@PreDestroy:這兩個注解一般在的方法上,在初始化Bean和銷毀之前,都會去調(diào)用這兩個注解下的方法。

    案例:(@Resource案例)

    步驟1:


    @Component

    public class JsrDao {

    public void print(){

    System.out.println("JsrDao的方法執(zhí)行了");

    }

    }

    @Component

    public class JsrService {

    // @Resource

    private JsrDao jsrDao;


    public void print(){

    jsrDao.print();

    }

    @Resource

    public void setJsrDao(JsrDao jsrDao) {

    this.jsrDao = jsrDao;

    }

    }



    步驟2:


    @Configuration

    @ComponentScan

    public class JsrConfig {


    }



    測試:

    public class TestJsr {

    @Test

    public void testJsr(){

    ApplicationContext ac=new AnnotationConfigApplicationContext(JsrConfig.class);

    JsrService js=ac.getBean("jsrService",JsrService.class);

    js.print();

    }

    }

    結(jié)果:JsrDao的方法執(zhí)行了

    知識拓展:


    @Resource和@Autowired都是做bean的注入時使用,其實(shí)@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要導(dǎo)入,但是Spring支持該注解的注入。

    1、共同點(diǎn)

    兩者都可以寫在字段和setter方法上。兩者如果都寫在字段上,那么就不需要再寫setter方法。

    2、不同點(diǎn)

    (1)@Autowired

    @Autowired為Spring提供的注解,需要導(dǎo)入包org.springframework.beans.factory.annotation.Autowired;只按照byType注入。

    public?class?TestServiceImpl?{????//?下面兩種@Autowired只要使用一種即可????@Autowired????private?UserDao?userDao;?//?用于字段上?????????@Autowired????public?void?setUserDao(UserDao?userDao)?{?//?用于屬性的方法上?????????this.userDao?=?userDao;?????}?}

    @Autowired注解是按照類型(byType)裝配依賴對象,默認(rèn)情況下它要求依賴對象必須存在,如果允許null值,可以設(shè)置它的required屬性為false。如果我們想使用按照名稱(byName)來裝配,可以結(jié)合@Qualifier注解一起使用。如下:

    public?class?TestServiceImpl?{?????@Autowired?????@Qualifier("userDao")????private?UserDao?userDao;??}

    (2)@Resource

    @Resource默認(rèn)按照ByName自動注入,由J2EE提供,需要導(dǎo)入包javax.annotation.Resource。@Resource有兩個重要的屬性:name和type,而Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以,如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不制定name也不制定type屬性,這時將通過反射機(jī)制使用byName自動注入策略。

    public?class?TestServiceImpl?{????//?下面兩種@Resource只要使用一種即可?????@Resource(name="userDao")????private?UserDao?userDao;?//?用于字段上?????????@Resource(name="userDao")????public?void?setUserDao(UserDao?userDao)?{?//?用于屬性的setter方法上?????????this.userDao?=?userDao;?????}?}


    注:最好是將@Resource放在setter方法上,因?yàn)檫@樣更符合面向?qū)ο蟮乃枷耄ㄟ^set、get去操作屬性,而不是直接去操作屬性。

    @Resource裝配順序:

    ①如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進(jìn)行裝配,找不到則拋出異常。

    ②如果指定了name,則從上下文中查找名稱(id)匹配的bean進(jìn)行裝配,找不到則拋出異常。

    ③如果指定了type,則從上下文中找到類似匹配的唯一bean進(jìn)行裝配,找不到或是找到多個,都會拋出異常。

    ④如果既沒有指定name,又沒有指定type,則自動按照byName方式進(jìn)行裝配;如果沒有匹配,則回退為一個原始類型進(jìn)行匹配,如果匹配則自動裝配。

    @Resource的作用相當(dāng)于@Autowired,只不過@Autowired按照byType自動注入。

    @PostConstruct和@PreDestroy(和以上案例相似,主要增加兩個方法,并關(guān)閉Spring上下文)


    @Component

    public class JsrService {

    @Resource

    private JsrDao jsrDao;

    public void print(){

    jsrDao.print();

    }

    public void setJsrDao(JsrDao jsrDao) {

    this.jsrDao = jsrDao;

    }

    @PostConstruct

    public void jsrInit(){

    System.out.println("Bean初始化方法執(zhí)行了");

    }

    @PreDestroy

    public void jsrDestroy(){

    System.out.println("Bean銷毀方法執(zhí)行了");

    }

    }


    測試:


    @Test

    public void testJsr(){

    AbstractApplicationContext ac=new AnnotationConfigApplicationContext(JsrConfig.class);

    JsrService js=ac.getBean("jsrService",JsrService.class);

    js.print();

    ac.close();

    }

    結(jié)果:(和XML文件配置的init-method和destroy-method一樣的功能)

    Bean初始化方法執(zhí)行了

    JsrDao的方法執(zhí)行了

    Bean銷毀方法執(zhí)行了

    使用JSR330標(biāo)準(zhǔn)注解

    1:從Spring3.0開始支持JSR330標(biāo)準(zhǔn)注解(依賴注入注解),其掃描方式與Spring注解一致。

    2:使用JSR330需要依賴javax.inject包。

    3:使用Maven引入方式如下。

    https://img4.sycdn.imooc.com/5ce93eff0001ee7b09480306.jpg

    @Inject注解:等效于@Autowired,可以使用于類、屬性、方法、構(gòu)造器上。

    https://img1.sycdn.imooc.com/5ce93fcc0001b45509060381.jpg

    @Named注解:使用特定名稱進(jìn)行依賴注入,與@Qualifier是等效的,還可以注解在類上,相當(dāng)于@Component。(同一種類型的Bean在IOC容器中有多個時候,可以使用@Named指定特定的Bean)

    在方法上:

    https://img4.sycdn.imooc.com/5ce941460001b99109360389.jpg在類上:

    https://img1.sycdn.imooc.com/5ce94155000182bd09660383.jpg

    案例:@Inject和@Named

    把@Componet換成@Named,@Resource換成Inject,執(zhí)行結(jié)果相同。


    查看全部

舉報

0/150
提交
取消
課程須知
Java的高級課程,適合對Java基礎(chǔ)知識應(yīng)用自如,并熟悉MVC架構(gòu)的小伙伴們。如果想成為一名Java工程師,這門課程是一定要學(xué)噠。
老師告訴你能學(xué)到什么?
掌握依賴注入、IOC和AOP的概念,并能進(jìn)行簡單應(yīng)用。

微信掃碼,參與3人拼團(tuán)

微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復(fù)購買,感謝您對慕課網(wǎng)的支持!