spring初步了解1

srping简介:

  springJ2EE应用程序框架,是轻量级的IoCAOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框架,ibatis框架等组合使用。

spring框架优点: 

  轻量级的容器框架没有侵入性

  使用IoC容器更加容易组合对象直接间关系,面向接口编程,降低耦合

  Aop可以更加容易的进行功能扩展,遵循ocp开发原则

  创建对象默认是单例的,不需要再使用单例模式进行处理

1 spring框架分层架构,每一层都是分离开的组件。

spring aop spring orm spring web spring web mvc
spring dao spring context
spring core

spring core:核心容器主要组件BeanFactory,他是工厂模式的实现,他应用控制反转ioc将配置和依赖关系分离开。

spring context:spring上下文配置文件,提供spring上下文信息。

spring aop:面向切面编程,通过配置信息,提供对事务,日志记录等与业务逻辑不是紧密关系的统一管理。

spring dao:数据访问对象。

spring orm:对象关系映射。

spring web:建立在应用程序上下文之上,简化请求参数绑定到域对象的工作。

spring web mvc:全功能mvc实现,通过策略接口,mvc框架变成高度可配置,容纳了大量的视图技术。

2 构建spring项目过程

  1 导入spring jar包支持

  2 创建模型类 HelloWorld.java

package org.model;

public class HelloWorld {

private String message;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

}
HelloWorld

  3 创建spring核心配置文件 config.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-2.5.xsd">

<bean id="HelloWorld" class="org.model.HelloWorld">

<property name="message" >

<value>Hello World!</value>

</property>

</bean>

</beans>
config.xml

  4 创建测试类 HelloWorldTest.java

package org.test;

import org.model.HelloWorld;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class HelloWorldTest {

public static void main(String[] args) {

//获取ApplicationContext对象

ApplicationContext ac=

new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/classes/config.xml");

//根据ApplicationContact对象获得HelloWorld对象,getBean方法中的参数即为

//配置文件中的Bean的id值

HelloWorld helloWorld=(HelloWorld) ac.getBean("HelloWorld");

//输出

System.out.println(helloWorld.getMessage());

}

}
HelloWorldTest
原文地址:https://www.cnblogs.com/pangdudu/p/5722489.html