spring 入门篇
spring 入门篇
- 低侵入式设计,代码污染极低。
- 独立于各种应用服务,真正实现写一次到处都可以使用。
- 用户可选择的自由度高,用户可以选择部分或者是全部SPRING的功能,它并不是设计来取代其它框架,可以和其它的框架(如STRUTS、HIBERNATE)等结合极好。
- 面向接口的编程方式,使得代码的偶合度降到最低。
- 所有的BEAN默认都被会单态化,相同的BEAN只会被初使化一次,因而节省了BEAN初使化的时间及减少垃圾回收,增加了应用效率。
示例一、不同的人说不同的话 —— 简单的例子
package test;
publicinterfacePerson{
publicvoid sayHello();
publicvoid sayBye();
}
//中国人
package test;
publicclassChineseimplementsPerson{
publicvoid sayBye(){
System.out.println("再见");
}
publicvoid sayHello(){
System.out.println("你好");
}
}
//美国人
package test;
publicclassAmericanimplementsPerson{
publicvoid sayBye(){
System.out.println("Bye");
}
publicvoid sayHello(){
System.out.println("hello");
}
}
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.sprintframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="chinese"class="test.Chinese"/>
<beanid="american"class="test.American"/>
</beans>
<beansxmlns="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-3.0.xsd">
<beanid="chinese"class="test.Chinese"/>
<beanid="american"class="test.American"/>
</beans>
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Lantch {
public static void main(String[] args) throws Exception {
new Lantch().doIt();
}
public void doIt() {
/* 获取bean映射配置文件,配置文件放于与当前测试类在同一个目录下 */
ApplicationContext ctx = new FileSystemXmlApplicationContext(getClass().getResource("bean.xml").toString());
/* 配置文件放在 resources目录下面 */
// ApplicationContext ctx = new ClassPathXmlApplicationContext("MATE-INF/bean.xml");
Person person = null;
person = (Person) ctx.getBean("chinese");
person.sayHello();
person.sayBye();
person = (Person) ctx.getBean("american");
person.sayHello();
person.sayBye();
}
}
示例二、设值注入
package test2;
publicinterfacePerson{
publicvoid useAxe();
}
package test2;
publicinterfaceAxe{
publicvoid chop();
}
package test2;
publicclassChineseimplementsPerson{
/* 默认无参构造方法不管为私的还是公有的,都可以访问,并且要保证bean中存在可以被外界访问的无参构造函数 */
privateChinese(){
};
/* 定义需要被使用的斧头接口,具体使用什么斧头这里不管 */
privateAxe axe;
/*
* 定义被注入斧头的set方法,该方法一定要符合JAVABEAN的标准。在运行时候,Sping就会根据配置的<ref
* local=""/>,找到需要注入的实现类
*/
publicvoid setAxe(Axe axe){
this.axe = axe;
}
/* 这个时候使用的axe,就不再是接口Axe本身,而是被注入的子类实例,所以这里的chop()动作就是具体子类的chop动作 */
publicvoid useAxe(){
axe.chop();
}
}
package test2;
publicclassStoneAxeimplementsAxe{
publicvoid chop(){
System.out.println("石斧慢慢砍");
}
}
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.sprintframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="chinese"class="test2.Chinese">
<!-- 声明实现类test2.Chinese中的属性 -->
<propertyname="axe">
<!-- 指定其中声明的属性,需要用本地的那个id对应的class 这里local的值为"stoneAxe",表示axe的属性值在注入的时候,
将会用test2.StoneAxe实例注入,到时在实例类Chinese中使用 axe的时候,实际上调用的时候StoneAxe的实例 -->
<reflocal="stoneAxe"/>
</property>
</bean>
<beanid="stoneAxe"class="test2.StoneAxe"/>
</beans>
package test2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
publicclassCaller{
publicstaticvoid main(String[] args){
Caller caller =newCaller();
caller.doIt();
}
publicvoid doIt(){
String bean = getClass().getResource("bean.xml").toString();
ApplicationContext ctx =newFileSystemXmlApplicationContext(bean);
Person person =(Person) ctx.getBean("chinese");
person.useAxe();
}
}
三、构造注入:
package test2;
publicclassChineseimplementsPerson{
/* 默认无参构造方法不管为私的还是公有的,都可以访问,并且要保证bean中存在可以被外界访问的无参构造函数 */
privateChinese(){
};
/* 定义需要被使用的斧头接口,具体使用什么斧头这里不管 */
privateAxe axe;
publicChinese(Axe axe){
this.axe = axe;
}
/* 这个时候使用的axe,就不再是接口Axe本身,而是被注入的子类实例,所以这里的chop()动作就是具体子类的chop动作 */
publicvoid useAxe(){
axe.chop();
}
}
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.sprintframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="chinese"class="test2.Chinese">
<!--定义需要被构造注入的实现类,同设值注入的结果一样,都是注入接口的实现类-->
<constructor-arg>
<ref bean="stoneAxe"/>
</constructor-arg>
</bean>
<bean id="stoneAxe"class="test2.StoneAxe"/>
</beans>
四、后记
构件maven项目以及依赖:
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
spring 入门篇的更多相关文章
- Spring入门篇总结:
本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 视频传送门:Spring入门篇 该门课程主要从Spring的Bean ...
- SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇
目录 1-1 Spring入门篇 专题一.IOC 接口及面向接口编程 什么是IOC Spring的Bean配置 Bean的初始化 Spring的常用注入方式 专题二.Bean Bean配置项 Bean ...
- Spring入门篇——第1章 概述
第1章 概述 本章对课程的情况进行介绍,并介绍框架和Spring概况. 1-1 Spring入门课程简介 1-2 Spring概况 1-3 Spring框架
- Spring课程 Spring入门篇 6-2 ProxyFactoryBean及相关内容(上)
1 解析 1.1 类的方式实现各种通知需要实现的接口 1.2 创建Spring aop代理的优点及方法 1.3 代理控制切入点和通知的顺序的代码实现(具体完全实现,见代码2.1) 1.4 代理方式选择 ...
- 教妹学Java:Spring 入门篇
你好呀,我是沉默王二,一个和黄家驹一样身高,刘德华一样颜值的程序员(管你信不信呢).从两位偶像的年纪上,你就可以断定我的码龄至少在 10 年以上,但实话实说,我一直坚信自己只有 18 岁,因为我有一颗 ...
- Spring课程 Spring入门篇 7-2 Advice定义及实例
1 解析 1.1 通知:after和afterreturning的区别 1.2 @RunWith 是什么? 2 代码演练 2.1 注解方式配置通知的两种方式 2.2 异常通知 2.3 非异常通知 1 ...
- Spring课程 Spring入门篇 7-1 Aspect介绍及PointCut注解应用
本节主要是理论型: 关键看下节实操. 这个其实只要理解一点:使用AspectJ进行Spring AOP 编程,其实就是使用了JAVA注解的风格进行配置和使用. 不像之前讲的那样在配置文件中配置使用.
- Spring课程 Spring入门篇 6-3 ProxyFactoryBean及相关内容(下)
1 解析 1.1 使用global advisors demo 1.2 jdk代理和cglib代理的选择 1.3 如何强制使用CGLIB实现AOP? 1.4 JDK动态代理和CGLIB字节码生成的区别 ...
- Spring课程 Spring入门篇 6-1 Spring AOP API的PointCut、advice的概念及应用
本节主要是模拟spring aop 的过程. 实现spring aop的过程 这一节老师虽然说是以后在工作中不常用这些api,实际上了解还是有好处的, 我们可以从中模拟一下spring aop的过程. ...
随机推荐
- android手动改动density(dpi)的方法
Android系统中会依据屏幕分辨率范围,制定默认的density,既320(xhdpi),那么我们也能够手动改动density. 改动的方式在system.prop中改动ro.sf.lcd_dens ...
- ASP.NET MVC 学习之路-4
本文在于巩固基础 模型绑定 从URL 获取值 public ActionResult About(int id) { ViewBag.Id = id; return View(); } @{ View ...
- 排序算法 -- 数据结构与算法的javascript描述 第12章
排序是常见的功能,给定一组数据,对其进行排序. 在此之前,我们需要准备个基础工作--自动生成数组,并可以对该组数据做任何处理. /** * 测试类 ,数组 * @param numElements * ...
- SqlServer2008 数据库同步的两种方式 (发布、订阅)
尊重原著作:本文转载自http://www.cnblogs.com/tyb1222/archive/2011/05/31/2064944.html 上篇中说了通过SQL JOB的方式对数据库的同步,这 ...
- oracle tablespace
oracle tablespace 1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_fil ...
- iOS开发那些事儿(二)热补丁
一.热补丁作用:修复导致崩溃的错误.替换/增加方法.替换原来的界面等等 二.实现手段:JSPatch (使用Objective-C Objective-C和JavaScript jspatch桥.你可 ...
- yum update
Linux升级命令有两个分别是yum upgrade和yum update, 这个两个命令是有区别的:代码如下:yum -y update升级所有包同时也升级软件和系统内核 代码如下:yum -y u ...
- 浅谈C中的指针和数组(一)
本文转载地址:http://www.cnblogs.com/dolphin0520/archive/2011/11/09/2242138.html 在原文的基础上加入自己的想法作为修改. 指针是C/C ...
- Customizing Zend Studio Using the Welcome Page
Customizing Zend Studio Using the Welcome Page Zend Studio enables you to add or remove plugins from ...
- tomcat异常
java.lang.UnsupportedClassVersionError: org/apache/lucene/store/Directory : Unsupported major.minor ...