[原创]spring及springmvc精简版--IOC
本篇博客为自己学习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的更多相关文章
- [原创]spring及springmvc精简版--AOP
接上一篇:[原创]spring及springmvc精简版--IOC 理解AOP.java是一种面向对象的语言.而AOP是面向切面,在我看来是面向逻辑或者业务编程,它是对一组逻辑的抽象和分配. 经典例子 ...
- [原创]spring及springmvc精简版--继承数据源,声明式事物
1.前期:导入c3p0 jar包,相关数据库连接jar包,我用的是mysql 2.关注事物管理器的配置和AOP配置 代码: 核心关注bean配置文件 application.xml <?xml ...
- spring与springMVC的父子容器关系
背景和概述 在spring与springMVC中通过IOC可以管理bean对象,有两个配置文件可以配置ioc spring的配置文件applicationContext.xmlspringMVC的配置 ...
- [原创] RT7 Lite win7旗舰版精简方案
[原创] RT7 Lite win7旗舰版精简方案 墨雪SEED 发表于 2016-1-26 21:23:54 https://www.itsk.com/thread-362912-1-5.html ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第十四天(非原创)
文章大纲 一.淘淘商城总体架构介绍二.淘淘商城重要技术点总结三.项目常见面试题四.项目学习(all)资源下载五.参考文章 一.淘淘商城总体架构介绍 1. 功能架构 2. 技术选型 (1)Sprin ...
- Spring、SpringMVC、SpringData + JPA 整合详解
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7759874.html ------------------------------------ ...
- Spring与SpringMVC的区别
Spring是IOC和AOP的容器框架,SpringMVC是基于Spring功能之上添加的Web框架,想用SpringMVC必须先依赖Spring. 简单点的话可以将SpringMVC类比于Strut ...
- [转帖]spring、springMvc、springBoot和springCloud的联系与区别
spring.springMvc.springBoot和springCloud的联系与区别 -- :: 尘光掠影 阅读数 文章标签: springspringmvcspringbootspringCl ...
- spring、springMvc、springBoot和springCloud的联系与区别
spring和springMvc: 1. spring是一个一站式的轻量级的java开发框架,核心是控制反转(IOC)和面向切面(AOP),针对于开发的WEB层(springMvc).业务层(Ioc) ...
随机推荐
- MySQL同步状态双Yes的假象及 seconds_behind_master的含义
MySQL同步状态双Yes的假象及seconds_behind_master的含义 近期由于特殊原因有一台主库宕机了一个小时没有处理,说起来这是个挺不好啥意思的事情,但是由于这个事情反而发现个比较 ...
- php基本语法之逻辑运算符
百度经验 | 百度知道 | 百度首页 | 登录 | 注册 新闻 网页 贴吧 知道 经验 音乐 图片 视频 地图 百科 文库 帮助 首页 分类 杂志 任务 签到 回享计划 商城 知道 百度经验 > ...
- jQuery实现3D幻灯片
先看下效果图: 看到这个酷炫的效果有没有很眼馋啊!接下来我们就一起来学习实现它吧. 1.看到效果后我们先分析这个dom要怎么实现! 首先我们要用一个大容器包裹内容,其次这个看起来像是3d效果的图片实际 ...
- linux oracle配置开机启动
参考:http://jingyan.baidu.com/article/b2c186c8fe4306c46ef6ff16.html 先以root身份登录到linux系统, 1. 修改vi /etc/o ...
- Webkit内核探究【2】——css简介
注:[转载请注明文章来源.保持原样] 出处:http://www.cnblogs.com/jyli/archive/2010/01/31/1660364.html 作者:李嘉昱 CSS在Webkit中 ...
- 不通过AppStore,在iOS设备上直接安装应用程序的原理
本文转载至 http://mobile.51cto.com/hot-439095.htm 通过itms-services协议,可以通过safari浏览器直接在iOS设备上安装应用程序.利用这种方式, ...
- 对 pthread 做的一个简陋封装
参考自 pthreadcc 库的 ThreadBase 类 用法:继承该类,重写 execute 方法,调用父类的 launchThread 方法启动线程 Thread.h // // Thread. ...
- Coursera课程《Machine Learning》学习笔记(week1)
这是Coursera上比较火的一门机器学习课程,主讲教师为Andrew Ng.在自己看神经网络的过程中也的确发现自己有基础不牢.一些基本概念没搞清楚的问题,因此想借这门课程来个查漏补缺.目前的计划是先 ...
- 导出网页中的table到excel
导出网页中的table到excel的两种简便方法: 1. 纯 JavaScript 方法,缺点只支持IE浏览器 var elTable = document.getElementById(" ...
- 巨蟒django之CRM3 添加和编辑客户&&公户和私户的展示和转换
昨日内容回顾: day66 1. 内容回顾 1. 数据的展示 数据通过ORM查询出来 对象列表 QuerySet 1. 普通的字段 对象.字段名 ——> 数据库中的值 2. choices (( ...