Spring入门实例

  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可以创建和管理对象,这样在业务逻辑层就不需要手动创建对象,降低了个层之间的耦合度。

posted on 2016-01-06 20:53  LG一直在努力  阅读(149)  评论(0编辑  收藏  举报

导航