spring ioc container
只是轉(zhuǎn)載bean的容器乡革,
IoC , DI,
DI的方式
- through constructor arguments 構(gòu)造函數(shù)
- arguments to a factory method, 工廠方式
- properties that are set, 屬性注入
IoC 容器
- BeanFactory
- ApplicationContext
- WebApplicationCotext
- DefaultListableBeanFactory
- ClassPathXmlApplicationContext
- FileSystemXmlApplicationContext
ApplicationContext與BeanFactory的區(qū)別
- Easier integration with Spring’s AOP features尼斧,aop特性
- Message resource handling (for use in internationalization), 消息處理益楼,主要是國際化
- Event publication , 事件發(fā)布, ApplicationEvent
- Application-layer specific contexts such as the WebApplicationContext for use in web applications. 擴展web的容器
spring ioc container
構(gòu)建應(yīng)用上下文的方式
- xml
- java
- java config
xml如何構(gòu)建上下文
- 編寫xml文件---services.xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
- 使用ClassPathXmlApplicationContext
// 支持多個xml資源, 拼接上去就可以了
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// 構(gòu)建2: classpath, 這個文件在resource文件夾下
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:services.xml");
另一種寫法
GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
context.refresh();
java config構(gòu)建
ApplicationContext context = new AnnotationConfigApplicationContext();
后臺會將該類所在的路徑作為基礎(chǔ)掃描路徑粘咖,當(dāng)然包括本類會被掃描進章办。
還會掃描對應(yīng)的注解@Configuration, @Bean, @Component, @Import, @DependsOn
另一種寫法
public class JavaConfigTest {
public static void main(String[] arg) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class); // 將本類注入到IoC中
ctx.refresh(); // 刷新上下文带膜,掃描并讀取對應(yīng)的class到beanDefinition中批狐,完成上下文構(gòu)建的工作
}
}
配置元數(shù)據(jù)
- Java-based configuration:
@Configuration, @Bean, @Import, and @DependsOn annotations. - Annotation-based configuration:
@Autowired,@Resource, @Inject - xml properties
<beans>, <bean>,
<property name="accountDao" ref="accountDao"/>
<import resource="resources/messageSource.xml"/>
例子
<beans>
// 注入其他xml文件
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
<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>
</beans>
獲取Bean
簡答方式就是通過getBean的方式
例子
T getBean(String name, Class<T> requiredType)
--------------------------------------------------------
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
寫于 2020-08-12 23:42:11
PS: 若你覺得可以扇售、還行前塔、過得去、甚至不太差的話承冰,可以“關(guān)注”一下华弓,就此謝過!