spring基本原理概念

一: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

  1. <span style="font-size:18px;">package com.cloud.service;  
  2. public class UserService {  
  3.     private String name;  
  4.     public String getName() {  
  5.         return name;  
  6.     }  
  7.     public void setName(String name) {  
  8.         this.name = name;  
  9.     }  
  10.     public void sayHello(){  
  11.         System.out.println("hello:"+name);  
  12.     }  
  13. }</span>
2.编写核心配置文件:applicationContext.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.     <!-- 在容器中配置bean对象 -->  
  16.     <!-- 下面这句等价于:UserService userService = new UserService() -->  
  17.     <bean id="userService" class="com.cloud.service.UserService">  
  18.         <!-- 等价于:userService.setName("SpringName"); -->  
  19.         <property name="name">  
  20.             <value>SpringName</value>  
  21.         </property>  
  22.     </bean>  
  23. </beans>

  24. 3.编写测试类:Test  
  1. <span style="font-size:18px;">package com.cloud.test;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. import com.cloud.service.UserService;  
  5. public class Test {  
  6.     public static void main(String[] args) {  
  7.         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");  
  8.         UserService userService = (UserService) ac.getBean("userService");  
  9.         userService.sayHello();  
  10.     }  
  11. }</span>  


四:spring原理总结

1.使用spring ,没有new对象,我们把创建对象的任务交给spring框架
2.spring
实际上是一个容器框架,可以配置各种bean(action/service/domain/dao),并且可以维护beanbean的关系,当我们需要使用某个bean的时候,我们可以getBean(id),使用即可.




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值