一:spring基本概念 1)struts2是web框架,hibernate是orm框架 2)spring是容器框架,建立bean,維護bean之間的關係 3)spring能夠管理web層,持久層,業務層,dao層,spring能夠配置各個層的組件,而且維護各個層的關係 二:spring核心原理 1.IOC控制反轉 概念:控制權由對象自己轉向容器,由容器根據配置文件建立對象實例並實現各個對象的依賴關係。 核心:bean工廠 2.AOP面向切面編程 a.靜態代理 根據每一個具體類分別編寫代理類 根據一個接口編寫一個代理類 b.動態代理 針對一個方面編寫一個InvocationHandler,而後借用JDK反射包中的Proxy類爲各類接口動態生成相應的代理類 三:簡單的Spring入門案例 1.編寫一個類:UserService <span style="font-size:18px;">package com.cloud.service; public class UserService { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void sayHello(){ System.out.println("hello:"+name); } }</span> 2.編寫核心配置文件:applicationContext.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 在容器中配置bean對象 --> <!-- 下面這句等價於:UserService userService = new UserService() --> <bean id="userService" class="com.cloud.service.UserService"> <!-- 等價於:userService.setName("SpringName"); --> <property name="name"> <value>SpringName</value> </property> </bean> </beans> 3.編寫測試類:Test <span style="font-size:18px;">package com.cloud.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cloud.service.UserService; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) ac.getBean("userService"); userService.sayHello(); } }</span> 四:spring原理總結 1.使用spring ,沒有new對象,咱們把建立對象的任務交給spring框架 2.spring其實是一個容器框架,能夠配置各類bean(action/service/domain/dao),而且能夠維護bean與bean的關係,當咱們須要使用某個bean的時候,咱們能夠getBean(id),使用便可.