Loading

Spring IOC的快速入门案例

>下载Spring的开发包
https://repo.spring.io/libs-release-local/org/springframework/spring/
目录结构:
docs : API文档和开发规范
libs : 开发需要的jar包、源代码和文档
schema : 开发需要的xml文档的约束

>复制Spring开发jar包到工程
使用pom.xml引入Spring的4个核心包以及 logging及Log4j包


>理解IOC控制反转和DI依赖注入
细节1:junit测试工具包中包含一个@Test注解
>编写Spring核心配置文件
默认文件名: applicationContext.xml
该文件名是Spring管理Bean的容器的接口名
<beans>
<bean id="us" class="com.imooc.demo.UserServiceImpl"></bean>
</beans>

>在程序中读取Spring配置文件,通过Spring框架获得Bean,完成相应操作
需要做的事情:
1.通过引入的xml文件构造工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocation:"applicationContext.xml");
2、通过工厂获得类
UserService userService = (UserService)applicationContext.getBean(s:"userService");
userService.satHello();
posted @ 2020-05-13 15:31  Z_ONE  阅读(238)  评论(0)    收藏  举报