Spring - 开始 , IOC创建对象,别名 和 创建对象的方式
2015-12-25 10:17
691 查看
1.开始
(1)框架是什么 ?
使用别人搭好的舞台,你来做表演;
框架的特点 : 半成品,封装了特定的处理流程和控制逻辑,成熟的不断升级改进的软件;
框架与类库的区别 :框架一般是封装了逻辑,高内聚的,类库则是松散的工具组合;
(2)为什么使用框架 ?
软件系统日趋复杂;
重用度高,开发效率和质量提高;
软件设计人员要专注于对框架的了解;
(3)Spring 简介
官网 :spring.io
一个开源框架,为了解决应用开发复杂性而创建的;
是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架,
从大小与开销两方面而言Spring都是轻量级的;
通过控制反转(IOC)的技术达到松耦合的目的;
提供了面向切面编程的丰富的支持,允许通过分离应用的业务逻辑与系统服务进行内聚性的开发;
包含并管理应用对象的配置和生命周期,这个意义上市一种容器;
将简单的组件配置,组合成为复杂的应用,这个意义上市框架;
Spring是非常活跃的开源框架,它是一个基于Core来构架多层javaEE系统框架,它的主要目的是简化企业开发;
Spring以一种非侵入式的方式来管理你的代码,Spring提倡最少侵入,这样就意味着你可以适当的时候安装或卸载Spring;
(4)优点
简单,方便,快捷,面向接口编程(5)容器
提供了多种技术的支持 : JMS ,MQ ,UnitTest ..;AOP(事务管理,日志等);
提供了众多方便的应用辅助类 (JDBC Template等);
对主流应用框架(Hibernate)提供了良好的支持;
(6) 使用范围:
构建企业应用 : SpringMvc + Spring + Hibernate/Mybatis/ibatis
单独使用Bean容器,bean管理;
单独使用AOP进行切面处理;
其他的Spring功能,如对消息的支持等;
在其它互联网中的应用;
(7)面向接口编程
接口:用于沟通的中介物的抽象化,对应java接口及声明,声明了那些方法是对外公开提高的;(在java8中,接口可以拥有方法体)
面向接口编程:
->结构设计中,分清层次及调用关系,每层只向外层提高一组功能接口,各层间仅依赖接口而非实现类;
->接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要;
->面向接口编程中的接口是用于隐藏具体实现和实现多态性的组件;
2. IOC
控制反转 总结一句话 : 即 把对象的创建交给Spring容器来做;(1)创建对象步骤实现
1)新建helloSpring.javapublic class HelloSpring {
public void hello(){
System.out.println("HI Spring , CreateObj!");
}
}
2)新建ApplicationContext-createhello.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-2.5.xsd">
<bean id="hellospring" class="cn.labelnet.createhello.HelloSpring"></bean>
</beans>
3)新建ApplicationContext.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 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 导入资源 -->
<import resource="/cn/labelnet/createhello/ApplicationContext-createhello.xml"/>
</beans>
4)测试 : 新建测试类
启动Spring容器,从Spring容器中将对象取出来,调用方法
private final String BENA_CONTEXT_XML="ApplicationContext.xml";
@Test
public void testHelloWord(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(BENA_CONTEXT_XML);
HelloSpring hs = (HelloSpring) applicationContext.getBean("hellospring");
hs.hello();
}
(2)使用别名(Alias)创建对象
1)新建java类public class HelloSpring {
public void hello(){
System.out.println("HI Spring , Alias!");
}
}
2)新建配置文件配置bean和别名 alias
<?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-2.5.xsd">
<!-- id唯一标识,class类的路径 -->
<bean id="hellospring_alias" class="cn.labelnet.alias.HelloSpring"></bean>
<!-- name 为bean的id -->
<alias name="hellospring_alias" alias="hi_alias"/>
</beans>
3)测试
@Test
public void testHelloWordAlias(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(BENA_CONTEXT_XML);
HelloSpring hs = (HelloSpring) applicationContext.getBean("hi_alias");
hs.hello();
}
3.spring容器创建对象的方式
(1) 默认是调用默认的构造函数;
(2)利用静态工厂创建 factory-mehtod属性;
spring调用工厂方法产生对象,但是真正创建对象还是要自己写;说明:spring配置文件中,只要是一个bean就会为该bean创建对象;
(3)实例工厂方法,配置 factory-bean 属性;
(4)实例
1)新建helloword类<span style="font-family:Comic Sans MS;font-size:18px;">public class HelloSpring { public HelloSpring() { System.out.println("It's Default 构造函数!"); } public void hello(){ System.out.println("HI Spring , 默认构造函数方式!"); } }</span>
2)新建工厂类
public class HelloSpringFactory {
public static HelloSpring getInstance(){
return new HelloSpring();
}
public void hello(){
System.out.println("HI Spring , 默认构造函数方式!");
}
}
3)新建配置
<span style="font-family:Comic Sans MS;font-size:18px;"><?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-2.5.xsd"> <!-- id唯一标识,class类的路径 --> <bean id="hellospring_default_type" class="cn.labelnet.createtype.HelloSpring"></bean> <!-- factory-method : 添加工厂方法方法名 --> <bean id="hellospring_factory" class="cn.labelnet.createtype.HelloSpringFactory" factory-method="getInstance"></bean> </beans></span>
4)总配置
<?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-2.5.xsd">
<!-- 导入资源 -->
<import resource="/cn/labelnet/alias/ApplicationContext-Alias.xml"/>
<import resource="/cn/labelnet/createhello/ApplicationContext-createhello.xml"/>
<import resource="/cn/labelnet/createtype/ApplicationContext-createType.xml"/>
</beans>
5)测试
@Test
public void testHelloWordFactoryType(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(BENA_CONTEXT_XML);
HelloSpring hs = (HelloSpring) applicationContext.getBean("hellospring_factory");
hs.hello();
}
6)结果
4.总结
刚刚开始学习Spring知识,还未了解更多,与mybatis相比,在IOC配置文件上和Mapper配置有相似之处;Demo免积分下载 :http://download.csdn.net/detail/lablenet/9376525
相关文章推荐
- Python动态类型的学习---引用的理解
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 土人系列AS入门教程 -- 对象篇
- C#托管堆对象实例包含内容分析
- C#实现获取不同对象中名称相同属性的方法
- javascript asp教程第十一课--Application 对象
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子
- VBS教程:对象-正则表达式(RegExp)对象
- C#检查指定对象是否存在于ArrayList集合中的方法
- sql2008启动代理未将对象应用到实例解决方案
- C#编程自学之类和对象
- C++中对象的常引用、动态建立和释放相关知识讲解
- php中将一个对象保存到Session中的方法
- php对象和数组相互转换的方法
- PHP中把对象转换为关联数组代码分享
- C#写入对象或集合类型数据到xml文件的方法
- C#利用反射来判断对象是否包含某个属性的实现方法
- ASP.NET中使用Application对象实现简单在线人数统计功能