查看“Spring:IOC”的源代码
←
Spring:IOC
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
[[category:SpringFramework]] == 关于IOC == IOC:Inversion of Control '''控制反转''',指的是'''对象的创建权反转(交给)给Spring''',作用是实现了程序的解耦合。 [[File:IOC 的底层实现原理.jpg|1000px]] 下载: * 官网:“http://spring.io/” * 下载地址:“http://repo.springsource.org/libs-release-local/org/springframework/spring” Spring 目录结构: * docs:API 和开发规范 * libs:jar 包和源码 * schema:约束 相关jar: :[[File:IOC相关jar包.jpg|400px]] : 即“core container”的四个部分; :[[File:SpringFramework Runtime.jpg|600px]] == 使用示例 == # 创建web 项目 # 引入Spring 的开发包 # 相关配置文件: ## log4j.properties:日志配置 ## applicationContext.xml:Spring配置文件: ##* (配置说明,位于包的doc中:“spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html”) ##: <syntaxhighlight lang="xml"> <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"> </beans> </syntaxhighlight> # 编写相关的类: #: <syntaxhighlight lang="java"> public interface UserDao { public void sayHello(); } public class UserDaoImpl implements UserDao { @Override public void sayHello() { System.out.println("Hello Spring..."); } } </syntaxhighlight> # 完成配置: #: <syntaxhighlight lang="xml"> <!-- Spring 的入门案例 --> <bean id="userDao" class="cn.itcast.spring.demo1.UserDaoImpl"></bean> </syntaxhighlight> # 编写测试程序: #: <syntaxhighlight lang="java"> @Test // Spring 的方式: public void demo2(){ // 创建Spring 的工厂类: ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 通过工厂解析XML 获取Bean 的实例. UserDao userDao = (UserDao) applicationContext.getBean("userDao"); userDao.sayHello(); } </syntaxhighlight> == Spring 中的工厂 == * BeanFactory :是在getBean 的时候才会生成类的实例. * ApplicationContext :在加载applicationContext.xml 时候就会创建. === ApplicationContext === ApplicatioContext 接口有两个实现类: # “ClassPathXmlApplicationContext”:加载类路径下Spring 的配置文件; # “FileSystemXmlApplicationContext”:加载本地磁盘下Spring 的配置文件; [[File:ApplicatioContext 接口的两个实现类.jpg|500px]] === BeanFactory === [[File:Spring的工厂.jpg|700px]] == Spring 的相关配置 == * '''id''':唯一确定一个Bean(唯一性约束),必须以字母开始,可以使用字母、数字、连字符、下划线、句点、冒号,不能出现特殊字符; * '''name''':Bean的名称(没有唯一性约束),name可以使用特殊字符;如果<bean>没有id 的话, name 可以当做id 使用; * '''class''':类的全路径名称; ** 整合struts1 的时候:“<bean name=”/loginAction” >”; * '''scope''':Bean 的作用范围; ** “singleton”:(默认)单例; ** “prototype”:多例; ** “request”:WEB 项目中,Spring 创建一个Bean 的对象,将对象存入到request 域中; ** “session”:WEB 项目中,Spring 创建一个Bean 的对象,将对象存入到session 域中; ** “globalSession”:WEB 项目中,应用在Porlet 环境.如果没有Porlet 环境那么globalSession 相当于session; * '''Bean 的生命周期'''的配置:通过配置<bean>标签上的“init-method”作为Bean 的初始化的时候执行的方法,配置“destroy-method”作为Bean 的销毁的时候执行的方法。 ** 销毁方法想要执行,需要是单例创建的Bean;而且在工厂关闭的时候,Bean 才会被销毁. * 分配置文件,实现的两种方法: *# 加载多个配置文件: *#: <syntaxhighlight lang="java"> ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml"); </syntaxhighlight> *# 在主配置文件(“applicationContext.xml”)中包含其他配置文件,如: *#: <syntaxhighlight lang="properties"> <import resource="applicationContext2.xml"></import> </syntaxhighlight> ===(配置文件提示)=== # 复制约束(applicationContext.xml):“http://www.springframework.org/schema/beans/spring-beans.xsd” # 添加eclipse的“XML Catalog”: #: [[File:eclipse配置提示XML Catalog.jpg|400px]] #: [[File:eclipse配置提示XML Catalog添加.jpg|400px]] == Bean的管理:XML == === Bean 实例化的三种方式 === ---- # '''使用类的无参数构造创建'''(重点) #: <syntaxhighlight lang="properties"> <!-- 方式一:无参数的构造方法的实例化--> <bean id="bean1" class="cn.itcast.spring.demo3.Bean1"></bean> </syntaxhighlight> # 使用静态工厂创建: #: <syntaxhighlight lang="java"> // 提供一个工厂类 public class Bean2Factory { public static Bean2 getBean2(){ return new Bean2(); } } </syntaxhighlight> #: <syntaxhighlight lang="properties"> <!-- 方式二:静态工厂实例化Bean --> <bean id="bean2" class="cn.itcast.spring.demo3.Bean2Factory" factory-method="getBean2"/> </syntaxhighlight> # 使用实例工厂创建 #: <syntaxhighlight lang="java"> // 提供Bean3 的实例工厂: public class Bean3Factory { public Bean3 getBean3(){ return new Bean3(); } } </syntaxhighlight> #: <syntaxhighlight lang="properties"> <!-- 方式三:实例工厂实例化Bean --> <bean id="bean3Factory" class="cn.itcast.spring.demo3.Bean3Factory"></bean> <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean> </syntaxhighlight> === Bean的依赖注入(DI) === ---- DI(Dependency Injection):'''依赖注入''',使用IOC创建类的过程中,将类所依赖的属性设置进去。 ==== Bean 的属性注入 ==== # 构造方法的方式注入属性: #: <syntaxhighlight lang="properties" highlight=""> <!-- 第一种:构造方法的方式--> <bean id="car" class="cn.itcast.spring.demo4.Car"> <constructor-arg name="name" value="保时捷"/> <constructor-arg name="price" value="1000000"/> </bean> </syntaxhighlight> # '''set 方法的方式注入属性''': #: <syntaxhighlight lang="properties" highlight=""> <!-- 第二种:set 方法的方式--> <bean id="car2" class="cn.itcast.spring.demo4.Car2"> <property name="name" value="奇瑞QQ"/> <property name="price" value="40000"/> </bean> </syntaxhighlight> # 对象类型的注入: #: <syntaxhighlight lang="properties" highlight="5"> <!-- 注入对象类型的属性--> <bean id="person" class="cn.itcast.spring.demo4.Person"> <property name="name" value="会希"/> <!-- ref 属性:引用另一个bean 的id 或name --> <property name="car2" ref="car2"/> </bean> </syntaxhighlight> # 名称空间p 的属性注入;(Spring2.x 以上) #: <syntaxhighlight lang="properties" highlight="3,14,15"> <!-- 第一步:引入p 名称空间 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- 第二步:使用p 名称空间. * 普通属性: p:属性名称=”” * 对象类型属性: p:属性名称-ref=”” --> <!-- p 名称空间的属性注入的方式--> <bean id="car2" class="cn.itcast.spring.demo4.Car2" p:name=" 宝马7" p:price="1200000"/> <bean id="person" class="cn.itcast.spring.demo4.Person" p:name=" 思聪" p:car2-ref="car2"/> ... </syntaxhighlight> # '''SpEL''' 方式的属性注入:(Spring3.x 以上) #* SpEL:Spring Expression Language,语法;“'''#{ SpEL }'''”; #: <syntaxhighlight lang="properties" highlight=""> <!-- SpEL 的注入的方式--> <bean id="car2" class="cn.itcast.spring.demo4.Car2"> <property name="name" value="#{'奔驰'}"/> <property name="price" value="#{800000}"/> </bean> <bean id="person" class="cn.itcast.spring.demo4.Person"> <property name="name" value="#{'冠希'}"/> <property name="car2" value="#{car2}"/> </bean> </syntaxhighlight> #: <syntaxhighlight lang="properties" highlight=""> <!-- SpEL 引用另一个类的属性--> <bean id="carInfo" class="cn.itcast.spring.demo4.CarInfo"></bean> <bean id="car2" class="cn.itcast.spring.demo4.Car2"> <!-- <property name="name" value="#{'奔驰'}"/> --> <property name="name" value="#{carInfo.carName}"/> <property name="price" value="#{carInfo.calculatePrice()}"/> </bean> </syntaxhighlight> # 注入复杂类型: #: <syntaxhighlight lang="properties"> <!-- Spring 的复杂类型的注入 --> <bean id="collectionBean" class="cn.itcast.spring.demo5.CollectionBean"> <!-- 数组类型的属性--> <property name="arrs"> <list> <value>会希</value> <value>冠希</value> <value>天一</value> </list> </property> <!-- 注入List 集合的数据--> <property name="list"> <list> <value>芙蓉</value> <value>如花</value> <value>凤姐</value> </list> </property> <!-- 注入Map 集合--> <property name="map"> <map> <entry key="aaa" value="111"/> <entry key="bbb" value="222"/> <entry key="ccc" value="333"/> </map> </property> <!-- Properties 的注入--> <property name="properties"> <props> <prop key="username">root</prop> <prop key="password">123</prop> </props> </property> </bean> </syntaxhighlight> == Bean的管理:注解 ==
返回至“
Spring:IOC
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
已展开
已折叠
查看
阅读
查看源代码
查看历史
更多
已展开
已折叠
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
笔记
服务器
数据库
后端
前端
工具
《To do list》
日常
阅读
电影
摄影
其他
Software
Windows
WIKIOE
所有分类
所有页面
侧边栏
站点日志
工具
链入页面
相关更改
特殊页面
页面信息