1. 程式人生 > >spring容器通過動態代理獲取bean

spring容器通過動態代理獲取bean

通過IOC容器獲取bean的例項:

專案解構圖如下:


package com.company;

/**
 * Created by Dqd on 2017/4/15.
 */
public interface API {
    String t(int a);
}


package com.company;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dqd on 2017/4/15.
 */
public class Client {
    public static void main(String[] args){
        /*在讀取applicationContext.xml檔案的時候一般是不用
        BeanFactory的,因為ApplicationContext的功能比BeanFactory多*/

        //1.讀取配置檔案來建立Bean工廠
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                new String[]{"applicationContext.xml"}
        );

        //2.從工廠中得到我們所需要的bean
        API api = (API) applicationContext.getBean("test");
        //如果我們使用的是下面的這種方式去建立,都可以得到類,只不過通過不同的代理方式
        //Implone implone = (Implone) applicationContext.getBean("test");
        String tmp = api.t(33);
        System.out.println(tmp+"**");
    }
}


package com.company;

/**
 * Created by Dqd on 2017/4/15.
 */
public class Implone implements API {

    @Override
    public String t(int a) {
        System.out.println("第一個實現類"+a);
        return "Hello11=="+a;
    }
}



package com.company;

/**
 * Created by Dqd on 2017/4/15.
 */
public class Impltwo implements API {
    @Override
    public String t(int a) {
        System.out.println("第二個實現類"+a);
        return "Hello22=="+a;
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="test" class="com.company.Implone"></bean>
</beans>


Spring容器例項化的三種方式,其中最為常用的第一種ApplicationContext

 /*第一種進行例項bean工廠的方式*/
        /* ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                new String[]{"applicationContext.xml"}
        );*/


        /*第二種*/
/*
        Resource resource = new FileSystemResource("applicationContext.xml");
        //但是注意這種獲取配置檔案的時候應該放在專案的根目錄下而不是src目錄下
        BeanFactory applicationContext;
        applicationContext = new XmlBeanFactory(resource);*/

        /*第三種方式*/
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        BeanFactory applicationContext;
        applicationContext = new XmlBeanFactory(classPathResource);

        //2.從工廠中得到我們所需要的bean
        API api = (API) applicationContext.getBean("test");

然而Bean的例項化也有三種方式:

1.無參的構造方法

2.靜態工廠方法

3.沒有靜態方法的工廠