spring容器通过动态代理获取bean

来源:互联网 发布:西西软件 下载 编辑:程序博客网 时间:2024/06/06 07:24

通过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.没有静态方法的工厂

0 0