Spring学习笔记_day01_ioc
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用.
转载请注明 出自 : 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的更多相关文章
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
- Spring学习笔记2——表单数据验证、文件上传
在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面
作者:ssslinppp 异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...
- 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
作者:ssslinppp 1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...
- 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat
作者:ssslinppp 1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...
- 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
作者:ssslinppp 时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...
随机推荐
- ZooKeeper实现配置中心的实例(原生API实现)(转)
说明:要实现配置中心的例子,可以选择的SDK有很多,原生自带的SDK也是不错的选择.比如使用I0Itec,Spring Boot集成等. 大型应用通常会按业务拆分成一个个业务子系统,这些大大小小的子应 ...
- 1. FrogRiverOne 一苇渡江 Find the earliest time when a frog can jump to the other side of a river.
package com.code; public class Test04_3 { public static int solution(int X, int[] A) { int size = A. ...
- 《C# 6.0 本质论》 阅读笔记
<C# 6.0 本质论> 阅读笔记 阅读笔记不是讲述这本书的内容,只是提取了其中一部分我认为比较重要或者还没有掌握的知识,所以如果有错误或者模糊之处,请指正,谢谢! 对于C# 6.0才 ...
- AngularJS入门学习
初识: {{}} 这种双层花括号的语法称之为:插值语法:也可以说是 标识符:AngularJS 主要就是使用这种方法进行数据绑定 ng-module="name" 在ng的 ...
- URAL 1326. Bottle Taps(简单的状压dp)
题目不太好读懂,就是先给你一个n代表要从n个物品中买东西,然后告诉你这n个东西的单位价格,在给你m个集合的情况.就是每一个结合中有x件物品.他们合起来买的价格是k.这x件物品依次是:p1--px.之后 ...
- PowerDesigner中导入MYSQL数据库结构的步骤及问题解决
今天在使用PowerDesigner,要导入MySql的表结构到PowerDesginer里, 记录下详细的操作步骤: 1.首先要确保机器安装了MySql的ODBC驱动,去http://dev.mys ...
- ios14--购物车优化2
// // ViewController.m // 03-综合练习 // // Created by xiaomage on 15/12/28. // Copyright © 2015年 小码哥. A ...
- jsp整合discuz
转自:http://blog.sina.com.cn/s/blog_49298ed001000a99.html 最近在实验室做项目用到的一个东西,拿来介绍一下. 需求:现有行业应用 ...
- 机器视觉: LBP-TOP
之前介绍过机器视觉中常用到的一种特征:LBP http://blog.csdn.net/matrix_space/article/details/50481641 LBP可以有效地处理光照变化,在纹理 ...
- 使用JDBC处理MySQL大文本和大数据
LOB,Large Objects,是一种用于存储大对象的数据类型,一般LOB又分为BLOB与CLOB.BLOB通常用于存储二进制数据,比如图片.音频.视频等.CLOB通常用于存储大文本,比如小说. ...