Spring (3.2.3), spring3.2.3
BeanFactory & ApplicationContext
Org. springframework. beans. factory. BeanFactory is the most basic Spring container interface, which provides some basic functions for Bean management. The BeanFactory Interface contains the following basic methods:
Object getBean (String name) throws BeansException;
<T> T getBean (String name, Class <T> requiredType) throws BeansException;
Boolean containsBean (String name );
Class <?> GetType (String name) throws NoSuchBeanDefinitionException;
Org. springframework. context. applicationContext is org. springframework. beans. factory. beanFactory sub-interfaces not only provide features supported by BeanFactory, ApplicationContext also provides some additional features, such as international support, event mechanism, and resource access.
Basic Structure of Spring configuration file
<?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-3.0.xsd"> <bean id="..." class="..." > <property name="..." value="..." /> <property name="..." ref="..." /> </bean> </beans>
Instantiate a container
Load the configuration file by File System Path:
ApplicationContext appCtx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Load multiple configurations by class path:
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
Load multiple configuration files:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
Use Spring container
ExampleBean example = appCtx.getBean("example", ExampleBean.class);