


1 ioc底层原理使用技术
(1)xml配置文件
(2)dom4j解决xml
(3)工厂设计模式
(4)反射
2 画图分析ioc实现原理


(1)解压资料 zip 文件
Jar 特点:都有三个 jar 包,分别是必备包,文档包,源代码包。一般我们只应该 copy 第一个

(2)做spring最基本功能之后,导入四个核心的jar包就可以了

(3)导入支持日志输出的 jar 包
(4)最终我们必须的大约 6 个 jar 包spring ioc原理 反射,如下


(5)把他们放在 web/WEB-INF/lib 目录下,并添加至环境变量中
创建 com.liuyanzhao.ioc 包,并新建 User.java 文件
packagecom.liuyanzhao.ioc;/***CreatedbyLiu_Yanzhaoon2017/8/2.*/publicclassUser{publicvoidadd(){System.out.println("add..........");}publicstaticvoidmain(String[]args){//原始做法,通过new来建立对象;而spring是借助配置文件来建立对象Useruser=newUser();user.add();}}
(1)spring核心配置文件名称跟位置不是固定的
- 建议放到src下面,官方建议applicationContext.xml
(2)引入schema约束
具体位置在 \spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html

下的最后一个文件xsd-configuration.html 中
我们是有浏览器打开,滚动条滑到最底端,复制如下内容

(3)配置对象创建
在 src 下创建 bean1.xml 文件spring ioc原理 反射,其实文件名和文件位置都不重要,
官方建议命名为 applicationContext.xml ,最后添加如下代码
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=""><!--ioc入门--><!--id可以随意,class为类名路径--><beanid="user"class="com.liuyanzhao.ioc.User"></bean></beans>
(4)写代码测试 对象的构建

在 com.liuyanzhao.ioc 下新建 TestIOC 文件
packagecom.liuyanzhao.ioc;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;/***CreatedbyLiu_Yanzhaoon2017/8/2.*/publicclassTestIOC{@Testpublicvoidtestuser(){//1、加载Spring配置文件,根据构建对象ApplicationContextcontext=newClassPathXmlApplicationContext("bean1.xml");//2、得到配置构建的对象Useruser=(User)context.getBean("user");System.out.println(user);user.add();}}
对啦,这里需要导入 JUnit 包,用于测试方式
(5) 运行 TestIOC 方法
输出内容如下:
com.liuyanzhao.ioc.User@5bcea91b
add..........
说明,IOC 成功创建对象
本文内容参考:传智播客视频
本文链接:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-138287-1.html
米国出了就没有放弃对中国在政治上
我觉得这样是很有道理的
太威武了
#杨洋2015金投赏##杨洋轻奢young#哈哈哈哈