本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用.

转载请注明 出自 : luogg的博客园 谢谢配合!

Spring_day01

spring是一站式的框架,对EE的三层有每一层的解决方案,Web层,业务层,数据访问层.Web层:SpringMVC , 持久层:JDBC Template , 业务层 : Spring的Bean管理

IOC(Inverse of Control) : 反转控制,将对象的创建交由Spring来完成,反射+配置文件实现.

AOP(Aspect Oriented Programming) : 面向切面编程.

IOC思想 : 工厂+反射+配置文件,底层原理就是提供一个工厂Bean,然后提供一个配置文件,把一些类全都配置在配置文件中,通过xml解析获得类的全路径,从而反射获得类的实例.

spring优点

方便解耦,简化开发

  • Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理

AOP编程的支持

  • Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能

声明式事务的支持

  • 只需要通过配置就可以完成对事务的管理,而无需手动编程

    方便程序的测试

  • Spring对Junit4支持,可以通过注解方便的测试Spring程序

    方便集成各种优秀框架

  • Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持

    降低JavaEE API的使用难度

  • Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

IOC和DI区别

IOC:控制反转,将对象的创建权交给Spring处理.

DI:依赖注入,在Spring创建对象过程中,把对象依赖属性注入到类中.通过property标签.

Eclipse配置XML提示

window->搜索xml catlog->add 找到schame的位置,将复制的路径copy指定位置,选择schame location.

Bean的3中实现方式

  • 1.默认情况通过无参构造方法实现
  • 2.通过静态方法实例化
  • 3.通过实例工厂实例化

Bean标签的其他配置

id和name的区别

id遵守xml的id的约束,保证这个属性值是唯一的,且必须以字母开头,name没有这些要求,

如果bean标签上没有id,那么name可以作为id.

scope属性

scope属性 :

  • singleton :单例的.(默认的值.)
  • prototype :多例的.
  • request :web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();
  • session :web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();
  • globalSession :一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;

实际开发中主要使用singleton,prototype

Bean属性的注入方式

  • 1.通过构造方法注入
  • 2.通过setter方法注入,最常使用,用property标签,name,value表示普通属性,ref表示引用其他的对象.

若通过构造方法注入,那么bean标签中需要使用

<constructor-arg name="name" value="奔驰S400"></constructor-arg>
<constructor-arg name="price" value="1200000"></constructor-arg>

其中,name也可以换成index,对应的构造方法中的参数的位置.

Bean属性注入:通过P名称空间代替property

Spring2.5版本引入了名称空间p.

p:<属性名>="xxx" 引入常量值

p:<属性名>-ref="xxx" 引用其它Bean对象

引入名称空间:(引入p命名空间)

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd">

通过p配置普通属性和带有对象的属性

<bean id="car2" class="cn.itcast.spring3.demo5.Car2" p:name="宝马" p:price="400000"/>
<bean id="person" class="cn.itcast.spring3.demo5.Person" p:name="童童" p:car2-ref="car2"/>

通过SpEL注入属性

Spring3.0提供注入属性方式:

语法:#{表达式}

<bean id="" value="#{表达式}">

<bean id="car2" class="cn.itcast.spring3.demo5.Car2">
<property name="name" value="#{'大众'}"></property>
<property name="price" value="#{'120000'}"></property>
</bean> <bean id="person" class="cn.itcast.spring3.demo5.Person">
<!--<property name="name" value="#{personInfo.name}"/>-->
<property name="name" value="#{personInfo.showName()}"/>
<property name="car2" value="#{car2}"/>直接使用别的bean中的对象
</bean> <bean id="personInfo" class="cn.itcast.spring3.demo5.PersonInfo">
<property name="name" value="张三"/>
</bean>

集合属性注入

<!-- 集合的注入 -->
<bean id="collectionBean" class="com.luogg.demo3.CollectionBean">
<property name="list">
<list>
<value>小花</value>
<value>小李</value>
</list>
</property>
<property name="map">
<map>
<entry key="1" value="洛哥"></entry>
<entry key="2" value="小美"></entry>
</map>
</property>
<property name="set">
<set>
<value>呵呵</value>
<value>哈哈</value>
</set>
</property>
</bean>

配置文件引入的问题

一种写法:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml",”bean2.xml”);
二种方法:在xml中通过import标签引入
<import resource="applicationContext2.xml"/>

通过注解装配Bean

Spring2.5 引入使用注解去定义Bean

@Component 描述Spring框架中Bean

Spring的框架中提供了与@Component注解等效的三个注解:

  • @Repository 用于对DAO实现类进行标注
  • @Service 用于对Service实现类进行标注
  • @Controller 用于对Controller实现类进行标注

三个注解为了后续版本进行增强的.

先去包中扫描这些带注解的类
<!-- 去扫描注解 装配的Bean -->
<context:component-scan base-package="com.luogg.demo1"></context:component-scan>

在类头部通过注解标识这个类是Spring加载的Bean

@Service("helloService")
public class HelloService { public void sayHello(){
System.out.println("Hello Spring");
}
}

最后测试

@Test
public void test1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService hello = (HelloService) ac.getBean("helloService");
hello.sayHello();
}

Bean的属性的注入

普通属性;

    @Value(value="itcast")
private String info;

对象属性:

@Autowired:自动装配默认使用类型注入.
@Autowired
private UserDao userDao; @Autowired
@Qualifier("userDao") --- 按名称进行注入.
private UserDao userDao;
等价于
@Resource(name="userDao")
private UserDao userDao;

Bean的其他属性的配置

配置Bean初始化方法和销毁方法:

  • init-method 和 destroy-method.

    @PostConstruct 初始化

    @PreDestroy 销毁

配置Bean的作用范围:

@Scope

实际开发中使用XML还是注解?

XML:

  • bean管理

注解;

  • 注入属性的时候比较方便.

两种方式结合;一般使用XML注册Bean,使用注解进行属性的注入.

<context:annotation-config/> 在xml中加上这句话可以识别注解
@Autowired
@Qualifier("orderDao")
private OrderDao orderDao;

Spring整合Web开发

正常整合Servlet和Spring没有问题的

但是每次执行Servlet的时候加载Spring配置,加载Spring环境.

  • 将加载的信息内容放到ServletContext中.ServletContext对象时全局的对象.服务器启动的时候创建的.在创建ServletContext的时候就加载Spring的环境.
  • ServletContextListener:用于监听ServletContext对象的创建和销毁的.
方法

导入;spring-web-3.2.0.RELEASE.jar

在web.xml中配置:

<!-- 服务启动时候加载spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 因为配置文件不在web-info下边,所以需要配置路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

修改程序代码

/*ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");*/
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
HelloService hello = (HelloService) ac.getBean("helloService");
hello.sayHello();

Spring与Junit整合

  • 1.先导入spring-junit包,spring-test-3.2.0.RELEASE.jar
  • 2.在类上标示
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest {
@Autowired
private HelloService helloService;
@Test
public void test1(){
helloService.sayHello();
}
}

Spring学习笔记_day01_ioc的更多相关文章

  1. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  2. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  3. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  4. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  5. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  6. 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面

    作者:ssslinppp       异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...

  7. 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

    作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...

  8. 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat

    作者:ssslinppp       1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...

  9. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

随机推荐

  1. 2014年辛星starphp第一节设置入口文件以及App类

    *********************本节目标**************** 1.首先是我们的框架大致布局,我们即将写成的这个框架.它的入口文件统一为star.php.它须要做的一些事,比方载入 ...

  2. BAT 前端开发面经 —— 吐血总结 前端相关片段整理——持续更新 前端基础精简总结 Web Storage You don't know js

    BAT 前端开发面经 —— 吐血总结   目录 1. Tencent 2. 阿里 3. 百度 更好阅读,请移步这里 聊之前 最近暑期实习招聘已经开始,个人目前参加了阿里的内推及腾讯和百度的实习生招聘, ...

  3. Android应用程序无法读写USB设备的解决方法

    假设android系统中的API或者apk无法读写usb设备.可能是没有加入读写usb的权限,须要依照例如以下方法进行设置: 1. 在android.hardware.usb.host.xml文件里加 ...

  4. cojs1101. [Vijos1369] 难解的问题==codevs 2188 最长上升子序列

    [题目描述] 在你的帮助下,蔚蓝来到了埃及.在金字塔里,蔚蓝看到了一个问题,传说,能回答出这个问题的人就能受到埃及法老的祝福,可是蔚蓝日夜奋战,还是想不出来,你能帮帮他么?(XXX: 胡扯,教主怎么可 ...

  5. java有用的启动参数

    三大类选项: 1. 标准选项: 功能是很稳定的,所有的标准选项都是以-开头,比如-version, -server等. 2. X选项:以-X开头,这类选项的功能还是很稳定,但官方的说法是它们的行为可能 ...

  6. android电池管理系统

    原文:http://www.2cto.com/kf/201408/326462.html 1.概述 随着移动智能设备的快速发屏,电池的续航能力在很大情况下诱导了大众消费者的购买选择,android系统 ...

  7. 【HAOI 2008】 糖果传递

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1045 [算法] 环形均分纸牌问题 [代码] #include<bits/std ...

  8. Dice (HDU 4652)

    题面: m 面骰子,求1. 出现n个连续相同的停止 ;2. 出现n个连续不同的停止的期望次数.(n, m ≤ 10^6 ) 解析: 当然要先列式子啦. 用f[i](g[i])表示出现i个连续相同(不相 ...

  9. PCB MongoDB 监控

    一个数据库监控工具是必不可少的,当然MongoDB安装自带监控啦. 这里将监控工具mongostat.exe与mongotop.exe使用与参数进行讲解说明. 一.监控工具说明: 二.监控工具启用 1 ...

  10. Counterfeit Dollar

    http://poj.org/problem?id=1013 #include<stdio.h> #include<string.h> #include<math.h&g ...