一、Bean和BeanFactory
// 以指定路径下bean.xml配置文件为参数,创建文件输入流
InputStream is = new FileInputStream( " beans.xml " );
// 以指定的文件输入流is,创建Resource对象
InputStreamResource isr = new InputStreamResource(is);
// 以Resource对象作为参数,创建BeanFactory的实例
XmlBeanFactory factory = new XmlBeanBeanFactory(isr);
// 搜索CLASSPATH路径,以CLASSPATH路径下的beans.xml文件创建Resource对象
ClassPathResource res = new ClassPathResource( " beans.xml " );
// 以Resource对象为参数,创建BeanFactory实例
XmlBeanFactory factory = new XmlBeanFactory(res);
// 搜索CLASSPATH路径,以CLASSPATH路径下的applicationContext.xml
// serve.xml 文件创建 ApplicationContext
ClassPathXmlApplicationContext appContext =
new ClassPathXmlApplicationContext(
new String[] ... { "applicationContext.xml", "service.xml"} );
// 事实上,ApplicationContext是BeanFactory的子接口,支持强制类型转换
BeanFactory factory = (BeanFactory) appContext;
// 指定路径下的applicationContext.xml,service.xml文件创建ApplicationContext
FileSystemXmlApplicationContext appContext =
new FileSystemXmlApplicationContext(
new String[] ... { "applicationContext.xml", "service.xml"} );
// 事实上,ApplicationContext是BeanFactory的子接口,支持强制类型转换
BeanFactory factory = (BeanFactory) appContext;
* 附加:
< bean id = " p2 " class = " prolove.Person " singleton = " false " />
<? xml version = " 1.0 " encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN "
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
< bean id = " dataSource " class = " org.apache.commons.dbcp.BasicDataSource
destroy - method = " close "" >
< property name = " driverClassName " >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name = " url " >
< value > jdbc:mysql: // localhost:3306/j2ee</value>
</ property >
< property name = " username " >
< value > root </ value >
</ property >
< property name = " password " >
< value > root </ value >
</ property >
</ bean >
</ beans >
public class BeanTest
... {
public static void main(String[] args) ...<
// 以指定路径下bean.xml配置文件为参数,创建文件输入流
InputStream is = new FileInputStream( " beans.xml " );
// 以指定的文件输入流is,创建Resource对象
InputStreamResource isr = new InputStreamResource(is);
// 以Resource对象作为参数,创建BeanFactory的实例
XmlBeanFactory factory = new XmlBeanBeanFactory(isr);
// 搜索CLASSPATH路径,以CLASSPATH路径下的beans.xml文件创建Resource对象
ClassPathResource res = new ClassPathResource( " beans.xml " );
// 以Resource对象为参数,创建BeanFactory实例
XmlBeanFactory factory = new XmlBeanFactory(res);
// 搜索CLASSPATH路径,以CLASSPATH路径下的applicationContext.xml
// serve.xml 文件创建 ApplicationContext
ClassPathXmlApplicationContext appContext =
new ClassPathXmlApplicationContext(
new String[] ... { "applicationContext.xml", "service.xml"} );
// 事实上,ApplicationContext是BeanFactory的子接口,支持强制类型转换
BeanFactory factory = (BeanFactory) appContext;
// 指定路径下的applicationContext.xml,service.xml文件创建ApplicationContext
FileSystemXmlApplicationContext appContext =
new FileSystemXmlApplicationContext(
new String[] ... { "applicationContext.xml", "service.xml"} );
// 事实上,ApplicationContext是BeanFactory的子接口,支持强制类型转换
BeanFactory factory = (BeanFactory) appContext;
* 附加:
< bean id = " p2 " class = " prolove.Person " singleton = " false " />
<? xml version = " 1.0 " encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN "
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
< bean id = " dataSource " class = " org.apache.commons.dbcp.BasicDataSource
destroy - method = " close "" >
< property name = " driverClassName " >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name = " url " >
< value > jdbc:mysql: // localhost:3306/j2ee</value>
</ property >
< property name = " username " >
< value > root </ value >
</ property >
< property name = " password " >
< value > root </ value >
</ property >
</ bean >
</ beans >
public class BeanTest
... {
public static void main(String[] args) ...<