Spring4.0学习1

一、Spring是什么?

1.Spring 是一个开源框架。
Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.

Spring 是一个 IOC(DI)AOP 容器框架.

具体描述:

  • 轻量级:Spring 是非侵入性的。基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
  • 依赖注入(DI — dependency injection、IOC)
  • 面向切面编程(AOP — aspect oriented programming)
  • 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
  • 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
  • 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)

二、安装 SPRING TOOL SUITE

  • SPRING TOOL SUITE 是一个 Eclipse 插件,利用该插件可以更方便的在 Eclipse 平台上开发基于 Spring 的应用。注意:先把该插件下载下来
  • 安装方法说明:
    • Help –> Install New Software…
    • Click Add…
    • In dialog Add Site dialog, click Archive…
    • Navigate to springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip and click Open
    • Clicking OK in the Add Site dialog will bring you back to the dialog ‘Install’
    • Select the xxx/Spring IDE that has appeared
    • Click Next and then Finish
    • Approve the license
    • Restart eclipse when that is asked

三、搭建Spring开发环境

1.把以下 jar 包加入到工程的 classpath 下:

Spring 的配置文件: 一个典型的 Spring 项目需要创建一个或多个 Bean 配置文件, 这些配置文件用于在 Spring IOC 容器里配置 Bean. Bean 的配置文件可以放在 classpath 下, 也可以放在其它目录下.

2.建立Spring项目
注意:这是java项目不是动态web项目,jar包需要手动build path,我就是因为这个坑浪费了不少时间!!!
项目截图:

Hello.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.lizz;
public class Hello {
private String name;
public void setName(String name) {
System.out.println("setName: " + name);
this.name = name;
}
public void hello() {
System.out.println(name);
}
public Hello() {
System.out.println("constructor...");
}
}

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.lizz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
/*
* 原始的java编写方法
* 1.创建Hello的一个对象
* Hello hello = new Hello();
* 2.为name属性赋值
* hello.setName("lizz");
*/
/*
* 使用Spring框架
*/
// 1.创建Spring的IOC容器对象
// ApplicationContext代表Spring里面的IOC容器
// ClassPathXmlApplicationContext表示配置文件在类路径下
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.从IOC容器中获取Bean实例
Hello hello = (Hello) ctx.getBean("hello");
// 调用hello方法
hello.hello();
}
}

注意,一定要把jar包add path进去,不然报错;还有applicationContext.xml文件放在classpath路径下,可以自己打开.classpath文件查看kind=src对应的path(默认是src,即放在src文件夹下)

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
">
<!-- 配置bean -->
<bean id="hello" class="com.lizz.Hello">
<!-- 注意这里的name对应的是Hello.java类里面定义的name属性名,Spring是该属性的初始化值 -->
<property name="name" value="Spring"></property>
</bean>
</beans>

运行结果:
constructor…
setName: Spring
Spring
可以看到,ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");这一步给Hello.java类设置了初始值,调用了setName方法

Newer Post

Spring4.0学习2 --Spring 中的 Bean 配置

IOC和DI IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源. …

继续阅读
Older Post

springMVC学习1

SpringMVC 概述 Spring为展现层提供的基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一。 Spring3.0后全面超越Struts2,成为最优秀的MVC框架 SpringMVC通过一套MVC注解,让POJO成为处理请求的控制器,无需实现任何接口。 支持REST风 …

继续阅读