Spring框架学习笔记.docx

上传人:b****4 文档编号:4464104 上传时间:2022-12-01 格式:DOCX 页数:14 大小:22.45KB
下载 相关 举报
Spring框架学习笔记.docx_第1页
第1页 / 共14页
Spring框架学习笔记.docx_第2页
第2页 / 共14页
Spring框架学习笔记.docx_第3页
第3页 / 共14页
Spring框架学习笔记.docx_第4页
第4页 / 共14页
Spring框架学习笔记.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

Spring框架学习笔记.docx

《Spring框架学习笔记.docx》由会员分享,可在线阅读,更多相关《Spring框架学习笔记.docx(14页珍藏版)》请在冰豆网上搜索。

Spring框架学习笔记.docx

Spring框架学习笔记

Spring_day01总结

今日内容

●Spring框架的概述

●Spring的快速入门

●Spring工厂接口

●在MyEclipse配置Spring的xml文件提示

●IoC容器装配Bean(xml配置方式)

●Ioc容器装配Bean(注解方式)

●在web项目中集成Spring

●Spring整合junit4测试

1.1Spring框架学习路线:

Spring的Ioc

Spring的AOP,AspectJ

Spring的事务管理,三大框架的整合.

1.2Spring框架的概述:

1.2.1什么是Spring:

Spring是分层的JavaSE/EEfull-stack(一站式)轻量级开源框架

*分层:

*SUN提供的EE的三层结构:

web层、业务层、数据访问层(持久层,集成层)

*Struts2是web层基于MVC设计模式框架.

*Hibernate是持久的一个ORM的框架.

*一站式:

*Spring框架有对三层的每层解决方案:

*web层:

SpringMVC.

*持久层:

JDBCTemplate

*业务层:

Spring的Bean管理.

1.2.2Spring的核心:

IOC:

(InverseofControl反转控制)

*控制反转:

将对象的创建权,交由Spring完成.

AOP:

AspectOrientedProgramming是面向对象的功能延伸.不是替换面向对象,是用来解决OO中一些问题.

IOC:

控制反转.

1.2.3Spring的版本:

Spring3.x和Spring4.xSpring4需要整合hibernate4.

1.2.4EJB:

企业级JavaBean

EJB:

SUN公司提出EE解决方案.

2002:

ExpertOne-to-OneJ2EEDesignandDevelopment

2004:

ExpertOne-to-OneJ2EEDevelopmentwithoutEJB(EE开发真正需要使用的内容.)

1.2.5Spring优点:

方便解耦,简化开发

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

AOP编程的支持

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

声明式事务的支持

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

方便程序的测试

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

方便集成各种优秀框架

*Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:

Struts、Hibernate、MyBatis、Quartz等)的直接支持

降低JavaEEAPI的使用难度

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

1.3Spring的入门的程序:

1.3.1下载Spring的开发包:

spring-framework-3.2.0.RELEASE-dist.zip---Spring开发包

*docs:

spring框架api和规范

*libs:

spring开发的jar包

*schema:

XML的约束文档.

spring-framework-3.0.2.RELEASE-dependencies.zip---Spring开发中的依赖包

1.3.2创建web工程引入相应jar包:

spring-beans-3.2.0.RELEASE.jar

spring-context-3.2.0.RELEASE.jar

spring-core-3.2.0.RELEASE.jar

spring-expression-3.2.0.RELEASE.jar

开发的日志记录的包:

mons.logging-1.1.1.jar---用于整合其他的日志的包(类似Hibernate中slf4j)

com.springsource.org.apache.log4j-1.2.15.jar

1.3.3创建Spring的配置文件:

在src下创建一个applicationContext.xml

引入XML的约束:

*找到xsd-config.html.引入beans约束:

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beanshttp:

//www.springframework.org/schema/beans/spring-beans.xsd">

1.3.4在配置中配置类:

1.3.5创建测试类:

@Test

//Spring开发

publicvoiddemo2(){

//创建一个工厂类.

ApplicationContextapplicationContext=newClassPathXmlApplicationContext(

"applicationContext.xml");

HelloServicehelloService=(HelloService)applicationContext.getBean("userService");

helloService.sayHello();

}

1.3.6IOC和DI(*****)区别?

IOC:

控制反转:

将对象的创建权,由Spring管理.

DI:

依赖注入:

在Spring创建对象的过程中,把对象依赖的属性注入到类中.

*面向对象中对象之间的关系;

*依赖:

publicclassA{

privateBb;

}

*继承:

isa

*聚合:

*聚集:

*组合:

1.3.7Spring框架加载配置文件:

ApplicationContext应用上下文,加载Spring框架配置文件

加载classpath:

newClassPathXmlApplicationContext("applicationContext.xml");:

加载classpath下面配置文件.

加载磁盘路径:

newFileSystemXmlApplicationContext("applicationContext.xml");:

加载磁盘下配置文件.

1.3.8BeanFactory与ApplicationContext区别?

ApplicationContext类继承了BeanFactory.

BeanFactory在使用到这个类的时候,getBean()方法的时候才会加载这个类.

ApplicationContext类加载配置文件的时候,创建所有的类.

ApplicationContext对BeanFactory提供了扩展:

*国际化处理

*事件传递

*Bean自动装配

*各种不同应用层的Context实现

*****早期开发使用BeanFactory.

1.3.9MyEclipse配置XML提示:

Window--->xmlcatalog--->add找到schema的位置,将复制的路径copy指定位置,选择schemalocation.

1.4IOC装配Bean:

1.4.1Spring框架Bean实例化的方式:

提供了三种方式实例化Bean.

*构造方法实例化:

(默认无参数)

*静态工厂实例化:

*实例工厂实例化:

无参数构造方法的实例化:

--默认情况下使用的就是无参数的构造方法.-->

静态工厂实例化:

--第二种使用静态工厂实例化-->

实例工厂实例化:

--第三种使用实例工厂实例化-->

1.4.2Bean的其他配置:

id和name的区别:

id遵守XML约束的id的约束.id约束保证这个属性的值是唯一的,而且必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号

name没有这些要求

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

*****开发中Spring和Struts1整合的时候,/login.

现在的开发中都使用id属性即可.

类的作用范围:

scope属性:

*singleton:

单例的.(默认的值.)

*prototype:

多例的.

*request:

web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();

*session:

web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();

*globalSession:

一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;

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

Bean的生命周期:

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

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

*init-method=”setup”

*destroy-method=”teardown”

执行销毁的时候,必须手动关闭工厂,而且只对scope=”singleton”有效.

Bean的生命周期的11个步骤:

1.instantiatebean对象实例化

2.populateproperties封装属性

3.如果Bean实现BeanNameAware执行setBeanName

4.如果Bean实现BeanFactoryAware或者ApplicationContextAware设置工厂setBeanFactory或者上下文对象setApplicationContext

5.如果存在类实现BeanPostProcessor(后处理Bean),执行postProcessBeforeInitialization

6.如果Bean实现InitializingBean执行afterPropertiesSet

7.调用指定初始化方法init

8.如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization

9.执行业务处理

10.如果Bean实现DisposableBean执行destroy

11.调用指定销毁方法customerDestroy

在CustomerService类的add方法之前进行权限校验?

1.4.3Bean中属性注入:

Spring支持构造方法注入和setter方法注入:

构造器注入:

--

-->

setter方法注入:

--标签中name就是属性名称,value是普通属性的值,ref:

引用其他的对象-->

setter方法注入对象属性:

名称空间p:

注入属性:

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

p:

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

p:

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

引入名称空间:

//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/beanshttp:

//www.springframework.org/schema/beans/spring-beans.xsd">

name="宝马"p:

price="400000"/>

name="童童"p:

car2-ref="car2"/>

SpEL:

属性的注入:

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

语法:

#{表达式}

---->

1.4.4集合属性的注入:

--注入List集合-->

童童

小凤

--注入set集合-->

杜宏

如花

--注入map集合-->

root

123

1.4.5加载配置文件:

一种写法:

ApplicationContextapplicationContext=newClassPathXmlApplicationContext("bean1.xml",”bean2.xml”);

二种方法:

1.5IOC装配Bean(注解方式)

1.5.1Spring的注解装配Bean

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

@Component描述Spring框架中Bean

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

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

@Service用于对Service实现类进行标注

@Controller用于对Controller实现类进行标注

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

1.5.2Bean的属性注入:

普通属性;

@Value(value="itcast")

privateStringinfo;

对象属性:

@Autowired:

自动装配默认使用类型注入.

@Autowired

@Qualifier("userDao")---按名称进行注入.

@Autowired

@Qualifier("userDao")

privateUserDaouserDao;

等价于

@Resource(name="userDao")

privateUserDaouserDao;

1.5.3Bean其他的属性的配置:

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

*init-method和destroy-method.

@PostConstruct初始化

@PreDestroy销毁

配置Bean的作用范围:

@Scope

1.5.4Spring3.0提供使用Java类定义Bean信息的方法

@Configuration

publicclassBeanConfig{

@Bean(name="car")

publicCarshowCar(){

Carcar=newCar();

car.setName("长安");

car.setPrice(40000d);

returncar;

}

@Bean(name="product")

publicProductinitProduct(){

Productproduct=newProduct();

product.setName("空调");

product.setPrice(3000d);

returnproduct;

}

}

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

XML:

*bean管理

注解;

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

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

annotation-config/>

s

@Autowired

@Qualifier("orderDao")

privateOrderDaoorderDao;

1.6Spring整合web开发:

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

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

*解决办法:

在Servlet的init方法中加载Spring配置文件?

*当前这个Servlet可以使用,但是其他的Servlet的用不了了!

!

!

*将加载的信息内容放到ServletContext中.ServletContext对象时全局的对象.服务器启动的时候创建的.在创建ServletContext的时候就加载Spring的环境.

*ServletContextListener:

用于监听ServletContext对象的创建和销毁的.

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

在web.xml中配置:

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:

applicationContext.xml

修改程序的代码:

WebApplicationContextapplicationContext=WebApplicationContextUtils.getWebApplicationContext(getServletContext());

WebApplicationContextapplicationContext=(WebApplicationContext)getServ

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1