惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Register - Security
The Register - Security
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
量子位
C
Check Point Blog
博客园 - 【当耐特】
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
博客园 - Franky
N
Netflix TechBlog - Medium
Webroot Blog
Webroot Blog
A
About on SuperTechFans
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
D
Docker
S
Security Affairs
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
V2EX - 技术
V2EX - 技术
Jina AI
Jina AI
Help Net Security
Help Net Security
L
LangChain Blog
P
Proofpoint News Feed
The Cloudflare Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
Schneier on Security
Schneier on Security
Recent Announcements
Recent Announcements
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
Microsoft Security Blog
Microsoft Security Blog
H
Heimdal Security Blog
P
Proofpoint News Feed
O
OpenAI News
H
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
爱范儿
爱范儿
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - 樊凯

Weblogic10.3客户端jar文件 PHP5操作MySQL数据库 - 樊凯 - 博客园 Struts2拦截器 使用AppServ快速建立php运行环境 Hibernate缓存 一个关于SpringSecurity很好的参考文档 Struts2.1.6 + Spring2.5+Hibernate3.2整合 JQuery和Struts实现Ajax文件上传 struts1.x+spring2.5+JPA(hibernate)整合 使用Apache的commons-codes加密 Spring备忘(涵盖Spring2.5) Spring备忘四(涵盖Spring2.5) Sping备忘三(涵盖Spring2.5) Spring备忘二(涵盖Spring2.5) - 樊凯 - 博客园 ubuntu遇到的错误 Ubuntu命令(更新中) 异常java.lang.UnsupportedClassVersionError: Bad version number in .class file 根据ResultSetMetaData对象动态创建pojo或其集合(JDBC) 天生我牛必有用
Spring备忘一(涵盖Spring2.5)
樊凯 · 2009-05-11 · via 博客园 - 樊凯

BeanFactory & ApplicationContext

在spring中容器分为两类:

n BeanFactory

n ApplicationContext

BeanFactory:

从名字上面可以看出,BeanFactory使用的是工厂设计模式。也就是说这个类可以分发所有的Bean。

BeanFactory接口有多种实现,常用的为XmlBeanFactory。

//用BeanFactory读取classpath中的spring配置文件

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring.xml"));

//用BeanFactory读取文件系统中的spring配置文件

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("C:/spring.xml"));

ApplicationContext:

对于简单的应用BeanFactory已经足够强大了,但是为了获得spring提供的更强大的功能,则要使用更高级的spring容器ApplicationContext。ApplicationContext和BeanFactory都是从配置文件中载入Bean的定义,进行装配,然后根据需要进行分发Bean。但是ApplicationContext提供了如下的更多功能:

n 提供了文本解析工具,包括I18N国际化;

n 提供了载入文件资源的通用方法;

n 可以向注册为监听器的Bean发送事件。

在ApplicationContext的实现中,三个实现经常用到:

n ClassPathXmlApplicationContext 从classpath(包含jar)中读取spring配置文件

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

n FileSystemXmlApplicationContext 从文件系统中读取spring配置文件

ApplicationContext context = new FileSystemXmlApplicationContext("C:/spring.xml");

n XmlWebApplicationContext 从web系统中读取spring配置文件

除了ApplicationContext提供的附加功能外,,ApplicationContext和BeanFactory的一个重要区别是关于单例Bean是如何被载入的。BeanFactory延迟加载所有的Bean,直到getBean()方法被调用的时候Bean才会被创建。ApplicationContext则会预载入单例Bean,当调用getBean()方法的时候,Bean已经被创建成功。

spring配置文件的schema

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

</beans>

在spring配置文件中配置一个bean

<bean id="userDao" class="com.kay.dao.impl.UserDAOImpl"></bean>

<bean name="userDao" class="com.kay.dao.impl.UserDAOImpl"></bean>

注意id和name属性的区别及联系:

id和name属性都是用来指定bean标识符。id具有唯一性,并且是xml中真正的id属性,受到xml解析器的验证功能,在使用中必须和java中命名变量一样去命名id的值,比如不能以数字开始等约束。但是name属性值则没有要求,如果愿意,可以赋给的值为“123”等,name属性还可以使用”,”号来进行分割多个为该bean指定的标识(别名),例如:

<bean name="userDao,dao,user" class="com.kay.dao.impl.UserDAOImpl"></bean>

name属性并不是必须的,当不写name属性时,spring容器会自动生成name属性值。

<bean class="com.kay.dao.impl.UserDAOImpl"></bean>

那么在获取该bean的时候,name属性和class属性值相同。

如果在spring配置文件中同时这样声明了三个相同的bean:

<bean class="com.kay.dao.impl.UserDAOImpl"></bean>

<bean class="com.kay.dao.impl.UserDAOImpl"></bean>

<bean class="com.kay.dao.impl.UserDAOImpl"></bean>

那么name的值分别为:

com.kay.dao.impl.UserDAOImpl

com.kay.dao.impl.UserDAOImpl#1

com.kay.dao.impl.UserDAOImpl#2