本篇博客为自己学习spring和springmvc的一个总结。主要以代码为主,至于文字性描述理解性东西,可以自行百度。有认识不妥的地方,还望指出,相互学习。

以前很困惑spring中的一些概念,在学习过程中遇到了很都问题,目前基本解决。解决的方法:

① 总结Spring框架的运行流程:在框架中,程序如何运行的?流程是什么?  可以适当的参考一部分源码解析

②  理解IOC本质。因为spring是一个容器框架,所以就是用来装东西的,就像tomcat,作用服务器一样。而IOC就是spring通过主配置文件(applicationContext.xml)的相关配置,为我们程序中自动的(底层通过反射方式,反射在框架中用的很过,私底下自己也在继续的深入的研究)为程序创建我们想要的bean对象。在spring中称之为注入,而所依赖的正是applicationContext.xml的配置。

配置方式:spring中提供了两种配置方式:基于xml方式的配置和基于注解的配置。对于前者可以实现对bean的统一的,清晰的,管理,但是需要手动的写,很麻烦(程序天生的就是为人的懒惰而服务);对于后者当然开发时候自己写的东西就少了,但是在统一管理方面稍差。凡事存在即合理。可以根据自己的需求选择自己的配置方式。

③  理解AOP。java是一种面向对象的语言。而AOP是面向切面,在我看来是面向逻辑或者业务编程,它是对一组逻辑的抽象和分配

经典例子,很多系统都有日志。以登录为例子。常规编程流程大致如下:点击登录--->写入日志--->后台处理--->写入日志。因为我们的系统中会有很多功能逻辑代码都是如此去处理日志。假设有一天,需求改变不需要日志了。那么我们如何去处理这些已经存在于整体逻辑单元中的日志代码?无非是找到每一个使用日志的地方,逐一删除。大家可想这样的效率?代码的耦合度?

而AOP变成。就是为了高度的解耦儿产生。它将登录的整个流程进行按照特定的,共同的业务逻辑进行切割,从而抽象出来了一组公共的逻辑单元。然后在根据不同业务模块的需求,在某些业务指定的地方将公共的业务逻辑植入其中,从而形成了一个整体的业务逻辑单元,实现某一模块功能。(这些是自己思考,总结的,刚开始接触的时候,没有理解到这点,也吃了很多闭门羹)有了这样的认识和理解,我们理解spring AOP中的一些常用的概念就很简单:

    横切关注点:哪些业务需要拦截,拦截后干什么?

切面:若干个横切关注点的抽象结合。即:抽象出来的公共的业务逻辑单元

   连接点:需要拦截的业务。原本剩下的业务逻辑

切入点:连接点的表达式。和通知相似。

     通知:将共同的逻辑代码植入的提示。前置,后置,异常,返回,环绕。

IOC:控制反转,依赖注入。配置bean

1.注入方式:setter方法注入,构造器注入,复杂类型注入

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!--
person p = new person();
p.setname("lisi");
-->
<!-- setter方法注入:1-空构造 2-属性提供setter方法 -->
<bean id="p" class="com.bena.Person">
<property name="anme" value="lisi"></property>
</bean>
<!-- person p1 = new person("lisi",23); -->
<!-- 构造器注入:前提:必须提供对应的构造器 -->
<bean id="p1" class="com.bena.Person">
<constructor-arg value="lisi"></constructor-arg>
<constructor-arg value="23"></constructor-arg>
</bean>
<!-- 复杂类型注入 -->
<bean id="addr" class="com.bena.Addrs">
<property name="sheng" value="shanxi"></property>
<property name="shi" value="xian"></property>
</bean>
<bean id="person1" class="com.bena.Person1">
<property name="name" value="zhangsan"></property>
<property name="age" value="23"></property>
<!-- <property name="addr" ref="addr"></property> -->
<property name="addr">
<!-- 匿名对象 -->
<bean class="com.bena.Addrs">
<property name="sheng" value="shanxi"></property>
<property name="shi" value="xian"></property>
</bean>
</property>
</bean> </beans>

2.继承,模式改变,自动注入(byName,byType)

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" default-autowire="byName">
<!-- 继承 -->
<bean id="animal" class="com.bean2.Animal" abstract="true">
<property name="name" value="123"></property>
<property name="age" value="23"></property>
</bean>
<!-- 默认单例,通过:scope改变模式:prototype:原型(非单例) -->
<bean id="student" class="com.bean2.Student" parent="animal">
<property name="num" value="123456"></property>
</bean> <bean id="teacher" class="com.bean2.Teacher" parent="animal">
<property name="jixiao" value="12312"></property>
</bean> <!-- 自动注入:在beans里添加:default-autowire属性
值:1-byName:根据名字注入:bean的id要跟类中的属性名相同
2-byType:根据类型注入:只能有一个该类型的bean跟属性的类型匹配
-->
<bean id="dao" class="com.bean3.Dao"></bean>
<bean id="service" class="com.bean3.Service"></bean>
<bean id="handler" class="com.bean3.Handler"></bean> </beans>

3.自动扫描

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" default-autowire="byName"> <!-- 配置扫描路径 -->
<context:component-scan base-package="com.bean4"></context:component-scan> </beans>

3.注解方式:举例我所接触的注解

@component :标识为spring组件,由spring容器管理

  @Autowired:设置自动注入bean。 默认名字为与当前bean的类一致,首字母小写。

  @Service:业务层Bean。一般作用于服务层的类,dao层,service层

  @Controller:展示层Bean。一般用于控制层的类,controller层

  @Repository:应存储层Bean。一般用于model层的类

详细的可以参照: Spring 注解总结

步骤:

1.下载相应的jar包。搭建环境。本人用的是:spring-framework-4.0.0.RELEASE-dist

2.新建java工程:在工程的根目录新建 lib目录,将相关jar导入

           

3.创建bean

Goods

 package com.bean;

 public class Goods {

     private int gid;
private int gprice;
private int gnum;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public int getGprice() {
return gprice;
}
public void setGprice(int gprice) {
this.gprice = gprice;
}
public int getGnum() {
return gnum;
}
public void setGnum(int gnum) {
this.gnum = gnum;
} }

Person

 package com.bean;

 public class Person {

     private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }

Users

 package com.bean;

 public class Users {

     private int uid    ;
private String uname;
private int umoney;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getUmoney() {
return umoney;
}
public void setUmoney(int umoney) {
this.umoney = umoney;
} }

  

[原创]spring及springmvc精简版--IOC的更多相关文章

  1. [原创]spring及springmvc精简版--AOP

    接上一篇:[原创]spring及springmvc精简版--IOC 理解AOP.java是一种面向对象的语言.而AOP是面向切面,在我看来是面向逻辑或者业务编程,它是对一组逻辑的抽象和分配. 经典例子 ...

  2. [原创]spring及springmvc精简版--继承数据源,声明式事物

    1.前期:导入c3p0 jar包,相关数据库连接jar包,我用的是mysql 2.关注事物管理器的配置和AOP配置 代码: 核心关注bean配置文件 application.xml <?xml ...

  3. spring与springMVC的父子容器关系

    背景和概述 在spring与springMVC中通过IOC可以管理bean对象,有两个配置文件可以配置ioc spring的配置文件applicationContext.xmlspringMVC的配置 ...

  4. [原创] RT7 Lite win7旗舰版精简方案

    [原创] RT7 Lite win7旗舰版精简方案 墨雪SEED 发表于 2016-1-26 21:23:54  https://www.itsk.com/thread-362912-1-5.html ...

  5. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第十四天(非原创)

    文章大纲 一.淘淘商城总体架构介绍二.淘淘商城重要技术点总结三.项目常见面试题四.项目学习(all)资源下载五.参考文章 一.淘淘商城总体架构介绍 1. 功能架构   2. 技术选型 (1)Sprin ...

  6. Spring、SpringMVC、SpringData + JPA 整合详解

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7759874.html ------------------------------------ ...

  7. Spring与SpringMVC的区别

    Spring是IOC和AOP的容器框架,SpringMVC是基于Spring功能之上添加的Web框架,想用SpringMVC必须先依赖Spring. 简单点的话可以将SpringMVC类比于Strut ...

  8. [转帖]spring、springMvc、springBoot和springCloud的联系与区别

    spring.springMvc.springBoot和springCloud的联系与区别 -- :: 尘光掠影 阅读数 文章标签: springspringmvcspringbootspringCl ...

  9. spring、springMvc、springBoot和springCloud的联系与区别

    spring和springMvc: 1. spring是一个一站式的轻量级的java开发框架,核心是控制反转(IOC)和面向切面(AOP),针对于开发的WEB层(springMvc).业务层(Ioc) ...

随机推荐

  1. LeetCode560. Subarray Sum Equals K

    Description Given an array of integers and an integer k, you need to find the total number of contin ...

  2. tomcat 测试页面显示

    首先下载匹配jdk版本的tomcat 解压即可使用 将完成的html文件直接放置到webapps目录下的子目录中是无法使用的 原因是tomcat默认加载的是jsp文件,且需要文件配置 所以,除去在we ...

  3. POJ 3304 Segments(计算几何:直线与线段相交)

    POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...

  4. 关于 SQLNET.AUTHENTICATION_SERVICES 验证方式的说明

    今天去客户那里巡检,客户提出为了提高数据库安全性考虑,须要改动sys/systempassword,并通过数据库验证方式来代替默认的操作系统方式,如今我来把这两种验证方式总结一下. 操作系统验证.即通 ...

  5. 在NodeJS中使用流程控制工具Async

    本文介绍一款流程控制工具async,在编写接口或者编写测试用例的过程中总是会用到它. 由于nodejs是异步编程模型,有一些在同步编程中很容易做到的事情,现在却变得很麻烦.Async的流程控制就是为了 ...

  6. Android-ViewPagerIndicator框架使用——Circle

    前言:Circle适用于应用新功能的展示页和商品的多张图片的展示功能. 1.定义布局文件:SampleCirclesDefault中添加了一个布局:simple_circles. 布局中定义一个Lin ...

  7. ios--后台返回信息有字符串和数字组成的,如何获取电话号码,让用户能够点击并且进行拨打?

    -(void)callPhone:(NSString*)phoneNumber{ NSString *phoneStr=[NSString stringWithFormat:@"tel:// ...

  8. array_sum的用法

    众所周知,PHP中函数是功能很强大的,那么今天就说下array_sum的功能吧. 函数功能:返回数组中所有值的和. 举例: <?php $a = array(1,2); $b = array_s ...

  9. Laravel5.1 模型--查询作用域

    所谓的查询作用域就是允许你自定义一个查询语句 把它封装成一个方法. 1 定义一个查询作用域 定义查询作用域就是在模型中声明一个scope开头的方法: public function scopeHotA ...

  10. react import改为绝对路径

    最近在使用react时发现路径用../../很不方便,特别是修改项目结构时,加减../都能改到吐血, 所有在网上找了半天webpack的配置,特此记录下 module.exports = (webpa ...