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
- spring容器通过动态代理获取bean
- 通过ApplicationContextAware获取spring容器中的bean
- 通过ApplicationContextAware获取spring容器中的bean
- Bean获取Spring容器
- Bean获取Spring容器
- 【Spring】Spring容器获取Bean
- Spring动态获取IoC容器中管理的Bean
- Spring容器动态注入bean
- 让bean获取spring容器
- 让Bean获取Spring容器
- 让Bean获取Spring容器
- ApplicationContextAware动态获取spring bean
- Spring动态管理IoC容器中的Bean
- 动态注入Bean到Spring容器
- 基于Spring容器Bean的动态注入
- java类获取spring容器的bean
- Java类获取spring 容器的bean
- 获取Spring 容器中bean的方法
- API 设计的最佳实践
- 接收到普通Exception,事务无法回滚解决方案
- 1043. Is It a Binary Search Tree (25)
- 自定义view(五):自定义view使文字居中对齐显示
- Docker镜像的备份和恢复
- spring容器通过动态代理获取bean
- 数据库
- docker学习笔记
- Python+Selenium中级篇之7-Python中字符串切割操作
- debug New table
- Java总结篇系列:Java多线程(一)
- 画图板
- activity跳转&传参
- 第七届 山东省ACM Execution of Paladin(水题)