Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。

控制反转:应用本身不负责依赖对象的创建及维护,依赖对象的创建和维护是由外部容器负责的。控制权的转移就是所谓的反转。

依赖注入:在运行期,由外部容器动态的将依赖注入到组件中。

项目中使用spring的好处:
1.降低组件之间的耦合度,实现软件各层之间的解耦。
2.可以使用容器提供的各种服务,如事务管理服务,消息服务等,开发人员不需要手工控制事务,也不需处理复杂的业务传播。
3.容器提供单例模式支持,开发人员不需要自己编写实现代码。
4.容器提供了AOP(Aspect-Oriented Programming 切面编程)技术,利用它很容易实现如权限拦截、运行期监控等功能。
5.容器提供众多辅助类,使用这些类能够加快应用的开发,如jdbc Template 、Hibernate Template。
6.Spring对主流的应用框架提供了集成支持,如集成Hibernate、JPA、Struts等,更方便应用的开发。

Maven中pom.xml文件所需jar包依赖:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
<scope>runtime</scope>
</dependency>

maven会自动下载所需jar包,包括:spring-core, spring-beans, spring-context, spring-context-support, spring-expression

实例化Spring容器的方法:
ApplicationContext act = new ClassPathXmlApplicationContext(“beans.xml”);
Spring配置文件可指定多个,可通过String数组传入。

下面通过一个例子来演示spring的使用:
项目目录结构:

项目所需jar包,可在maven中定义依赖,会自动下载

action包中的TestAction.java

package action;

import service.TestService;

public class TestAction {
public TestService testService; public void doTest() {
testService.test();
} public void setTestService(TestService testService) {
this.testService = testService;
}
}

service包中的TestService.java

package service;

public interface TestService {
public void test();
}

service.impl中的TesterA.java

package service.impl;

import service.TestService;

public class TesterA implements TestService {

    public TestService testService;

    public void test() {
System.out.println("testA---test");
}
}

service.impl中的TesterB.java

package service.impl;

import service.TestService;

public class TesterB implements TestService{
public TestService testService; public void test(){
System.out.println("testB---test");
}
}

test包中的AppTest.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import action.TestAction;
import service.TestService; public class AppTest { public static void main(String[] args) {
//读取配置文件
ApplicationContext act = new ClassPathXmlApplicationContext("config/beans.xml");
TestAction testAction = (TestAction) act.getBean("testAction");
testAction.doTest();
}
}

config包下beans.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.xsd"> <bean id="testA" class="service.impl.TesterA" />
<bean id="testB" class="service.impl.TesterB"/> <!-- 在TestAction中通过setTestService方法获取testService -->
<bean id="testAction" class="action.TestAction">
<property name="testService" ref="testB">
<!-- 如果需要实例化对象,只需修改ref即可-->
</property>
</bean>
</beans>

运行AppTest.java,控制台结果如下

一月 09, 2016 2:34:07 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1829ae5e: startup date [Sat Jan 09 14:34:07 CST 2016]; root of context hierarchy
一月 09, 2016 2:34:07 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config/beans.xml]
testA---test

总结一下:spring可以创建和管理对象,这样在业务逻辑层就不需要手动创建对象,降低了个层之间的耦合度。

Spring入门实例的更多相关文章

  1. Spring中IoC的入门实例

    Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...

  2. Eclipse安装springsource-tool-suite插件及spring helloworld入门实例

    转载至: https://www.cnblogs.com/aaron-shu/p/5156007.html 一.查看eclipse版本 Help-->About Eclipse,我的版本是4.4 ...

  3. Spring入门1. IoC入门实例

    Spring入门1. IoC入门实例 Reference:Java EE轻量级解决方案——S2SH 前言: 之前学习过关于Spring的一点知识,曾经因为配置出现问题,而总是被迫放弃学习这些框架技术, ...

  4. Spring oxm入门实例

    O/XMapper是什么? Spring3.0的一个新特性是O/XMapper.O/X映射器这个概念并不新鲜,O代表Object,X代表XML.它的目的是在Java对象(几乎总是一个plainoldJ ...

  5. Spring Boot下Spring Batch入门实例

    一.About Spring Batch是什么能干什么,网上一搜就有,但是就是没有入门实例,能找到的例子也都是2.0的,看文档都是英文无从下手~~~,使用当前最新的版本整合网络上找到的例子. 关于基础 ...

  6. Spring入门学习(一)

    SpringMVC基础平台补充(2016.03.03) 如果想要开发SpringMVC,那么前期依次安装好:JDK(jdk-8u74-windows-x64,安装后配置环境变量JAVA_HOME和CL ...

  7. Spring入门(9)-AOP初探

    Spring入门(9)-AOP初探 0. 目录 什么是面向切面编程 AOP常见术语 AOP实例 参考资料 1. 什么是面向切面编程 Aspect Oriented Programming(AOP),即 ...

  8. Spring入门(3)-Spring命名空间与Bean作用域

    Spring入门(3)-Spring命名空间与Bean作用域 这篇文章主要介绍Spring的命名空间和Bean作用域 0. 目录 Spring命名空间 Bean作用域 1. Spring命名空间 在前 ...

  9. spring 入门篇

    spring 入门篇         相对于Hibernate(冬眠),Spring(春天),具有更多的诗意与希望的感觉,是为了解决传统J2EE开发效率过低.开发商之间不统一.没有真正实现“写一次到处 ...

随机推荐

  1. 【刷题】BZOJ 4530 [Bjoi2014]大融合

    Description 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它 ...

  2. 【BZOJ4804】欧拉心算

    Description 给定数字\(n\)(\(n\le 10^7\)),求: \[ \sum_{i=1}^n\sum_{j=1}^n\varphi(\gcd(i,j)) \] ​ 多组数据输入,数据 ...

  3. 洛谷 P1436 棋盘分割 解题报告

    P1436 棋盘分割 题目描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的两部分中的任意一块继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共 ...

  4. Qt 编写多窗口程序

    该文章原创于Qter开源社区(www.qter.org),作者yafeilinux,转载请注明出处! 导语      程序要实现的功能是:程序开始出现一个对话框,按下按钮后便能进入主窗口,如果直接关闭 ...

  5. SPSS数据类型:测量字段、角色字段

    一:测量字段 • 默认值.具有未知存储类型和值的数据(例如,由于其尚未被读取)将显示为<默认值>. •  连续.用于描述整数.实数或日期/时间等数字值,如范围 0 - 100 或 0.75 ...

  6. bzoj 3224

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 16656  Solved: 7255[Submit][St ...

  7. Java入门系列:处理Json格式数据

    本节主要讲解: 1)json格式数据处理方法 2)第三方工具包的使用方法 3)java集合数据类型 [项目任务] 编写一个程序,显示未来的天气信息. [知识点解析] 为了方便后面代码的分析,先需要掌握 ...

  8. Python 函数01

    Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...

  9. openstack组件的数据库操作

    一.基础 SQLAlchemy http://docs.sqlalchemy.org/en/rel_0_9/index.html 对数据库进行操作的工具:xxx-manage db ... 二.数据库 ...

  10. RabbitMQ消息队列里积压很多消息

    1.场景:上千万条消息在mq里积压了几个小时了还没解决 2.解决: 1)先修复consumer的问题,确保其恢复消费速度,然后将现有cnosumer都停掉 2)新建一个topic,partition是 ...