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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 樊凯

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