Spring框架的 学习先列个大纲:
第一部分:
1、Spring简介
2、建立Spring环境
第二部分:
1、IOC/DI
2、Spring的核心容器Bean
3、AOP
4、Spring对事物的处理
5、持久层封装
6、web框架,MVC
7、定时器
第三部分:与其他工具的结合
1、Spring+Struts
2、Spring+Hibernate
3、Spring+Ant
4、Spring——JUnit
第四部分:开发实例
第一个基于Spring框架的java程序:
HelloWorld
1、创建一个HelloWorld的bean。
2、在配置文件中配置bean。
3、编写测试类从xml配置文件中获取bean,然后执行bean的方法。
一般程序的处理方式和Spring bean的处理方式比较:
/**一般的处理方式**/
1、创建一个bean
package com.gc.acion;
public class HelloWorld {
private String msg = null;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2、不需要通过xml配置文件获取bean,直接new出来进行处理。
package com.gc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.acion.HelloWorld;
public class HelloWorldTest {
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMsg("wangyj");
System.out.println(helloWorld.getMsg());
}
}
这样每次涉及要调用HelloWorld的方法如果msg有变动的话则要修改很多相关联赋值的程序,很麻烦。
/**spring bean的处理方式**/
1、创建一个bean
package com.gc.acion;
public class HelloWorld {
private String msg = null;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2、新增bean的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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!--在配置文件里给HelloWorld的msg赋值为wangyj-->
<bean id="HelloWorld" class="com.gc.acion.HelloWorld">
<property name="msg">
<value>wangyj</value>
</property>
</bean>
</beans>
3、通过ApplicationContext获取bean
package com.gc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.acion.HelloWorld;
public class HelloWorldTest {
public static void main(String[] args) {
// HelloWorld helloWorld = new HelloWorld();
// helloWorld.setMsg("wangyj");
//通过Spring.xml配置文件获取属性
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("HelloWorld");//通过bean得到HelloWorld
System.out.println(helloWorld.getMsg());
}
}
这样只需要简单的修改配置文件就可以修改msg的内容。
例子2:
bean的配置指向不同的实现类。
1、编写一个接口
2、编写两个类实现这个接口
3、xml的配置中通过指向不同的路径实现不同的逻辑
package com.gc.impl;
public interface Hello {
public abstract String Format();
}
package com.gc.acion;
import com.gc.impl.Hello;
public class ChHello implements Hello{
private String msg = null;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String Format() {
// TODO Auto-generated method stub
return "你好 "+this.msg;
}
}
package com.gc.acion;
import com.gc.impl.Hello;
public class EnHello implements Hello{
private String msg = null;
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg = msg;
}
@Override
public String Format() {
// TODO Auto-generated method stub
return "hello "+this.msg;
}
}
<bean id="Hello" class="com.gc.acion.EnHello">//根据不同的配置实现不同的接口
<property name="msg">
<value>laoda</value>
</property>
</bean>
package com.gc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.impl.Hello;
public class HelloTest {
public static void main(String[] args) {
//通过application获取bean
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
//通过bean获取Hello
Hello hello = (Hello) context.getBean("Hello");
System.out.println(hello.Format());
}
}