Spring IOC学習ノート(1)spring容器について
6332 ワード
Springのiocは制御反転であり、依存注入とも呼ばれるSpringのコアコンテンツはBeanFactoryでbeanのインスタンスを取得することができますが、そのsuperset(スーパーファーザー)Applectory Conteextを使用することを推奨します. 以上がスプリングの中のIOC容器の対象です.私達が作成したbeanは容器の対象に保存されています.
1.初期化ioc容器
2.XML配置bean
springのプロファイルは、importキーを介してルートプロファイルに導入することができます.
3.ioc容器を使用する
getBean法でbeanオブジェクトを取得する
1.初期化ioc容器
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
2.XML配置bean
<?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.xsd">
<!-- services -->
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
springのプロファイルは、importキーを介してルートプロファイルに導入することができます.
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
3.ioc容器を使用する
getBean法でbeanオブジェクトを取得する
// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();