SpringWord格式文档下载.docx

上传人:b****1 文档编号:1540264 上传时间:2023-04-30 格式:DOCX 页数:20 大小:23.57KB
下载 相关 举报
SpringWord格式文档下载.docx_第1页
第1页 / 共20页
SpringWord格式文档下载.docx_第2页
第2页 / 共20页
SpringWord格式文档下载.docx_第3页
第3页 / 共20页
SpringWord格式文档下载.docx_第4页
第4页 / 共20页
SpringWord格式文档下载.docx_第5页
第5页 / 共20页
SpringWord格式文档下载.docx_第6页
第6页 / 共20页
SpringWord格式文档下载.docx_第7页
第7页 / 共20页
SpringWord格式文档下载.docx_第8页
第8页 / 共20页
SpringWord格式文档下载.docx_第9页
第9页 / 共20页
SpringWord格式文档下载.docx_第10页
第10页 / 共20页
SpringWord格式文档下载.docx_第11页
第11页 / 共20页
SpringWord格式文档下载.docx_第12页
第12页 / 共20页
SpringWord格式文档下载.docx_第13页
第13页 / 共20页
SpringWord格式文档下载.docx_第14页
第14页 / 共20页
SpringWord格式文档下载.docx_第15页
第15页 / 共20页
SpringWord格式文档下载.docx_第16页
第16页 / 共20页
SpringWord格式文档下载.docx_第17页
第17页 / 共20页
SpringWord格式文档下载.docx_第18页
第18页 / 共20页
SpringWord格式文档下载.docx_第19页
第19页 / 共20页
SpringWord格式文档下载.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

SpringWord格式文档下载.docx

《SpringWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《SpringWord格式文档下载.docx(20页珍藏版)》请在冰点文库上搜索。

SpringWord格式文档下载.docx

将业务类交给Spring容器管理和维护(通过配置文件中的bean标签来配置)

使用业务类时就可以从Spring容器中获取业务类对象

Spring的使用步骤:

一、向项目中添加jar包,并创建配置文件

a)Bean标签中的id:

为bean在spring中起了个名字,该名字是唯一的,不能由特殊字符,专属XML,可以由XML解析器校验

b)Bean标签中的name:

同样为bean在spring中起个名字,但它可以由特殊字符

二、创建业务类

三、将业务类交给spring容器来管理和维护

四、从spring容器中获得业务类对象,进行操作

a)创建spring容器对象

i.(ApplicationContextctx=newClassPathXmlApplicationContext(“配置文件的名字”))<

读取配置文件,并创建对象>

b)从spring容器中获得业务对象

i.ctx.getBean(“配置文件中bean的名字”)

Spring中实例化Bean的三种方式:

一、使用类构造器实例化

<

beanid=”orderService”class=”com.OrderServiceBean”/>

(默认实例化方式,多数使用)

二、使用静态工厂方法实例化

beanid=”orderService”class=”com.OrderServiceFactory”factory-method=”createOrder”/>

(实例化OrderServiceFactory对象,并调用createOrder方法,获得bean实例)

//创建工厂类

PublicclassOrderServiceFactory{

PublicstaticOrderServiceBeancreateOrder(){

ReturnnewOrderServiceBean();

}

三、使用实例工厂方法实例化

beanid=”orderServiceFactory”class=”com.OrderServiceFactory”/>

(创建工厂实例)

beanid=”orderService”factory-bean=”orderServiceFactory”factory-method=”createOrder”/>

(根据工厂的实例调用createOrder方法获得Bean实例)

PublicOrderServiceBeancreateOrder(){

Bean的作用域

Bean的作用域在spring中有五种:

1、singleton:

Spring中默认的作用域为sigleton既每次从Spring容器中获得的对象是同一个

2、prototype:

每次从Spring容器中获得的对象都是新对象

3、request

4、session

5、globalSession

Bean的生命周期:

Bean在默认作用域中,当容器创建就被创建,若作用域设置为prototype则在通过getBean方法获得对象时实例化,

Bean标签的属性:

一、属性

1、id为bean的名字

2、class那个类创建的实例

3、scope:

作用域

4、lazy-init:

是否进行懒加载初始化,若进行了懒加载初始化则在容器初始化时不创建实例

5、init-method:

容器创建类实例时执行的初始化方法(用于初始化资源)

6、destroy-method:

对象销毁时执行的方法(用于销毁资源)

7、autowire:

设置该bean的属性为自动装配,它会将该bean的所有属性进行装配

二、子标签(property)

实现依赖注入的标签

属性:

1、name-:

表示将要注入给那个属性

2、ref:

指定将谁注入到name属性

常用方法:

ApplicationContext:

3、getBean()--------------获得bean对象

AbstractApplicationContext:

1、close()--------------关闭容器

Sping的依赖注入方式:

1、构造器注入(通过构造方法注入)

!

--构造方法注入-->

<

beanid="

userBiz"

class="

org.spring.service.impl.UserBizImpl"

>

<

constructor-argindex="

0"

ref="

userDao"

/>

--构造器中第一个参数,类型是UserDao类型-->

1"

type="

java.lang.String"

value="

shanshi"

--构造器中第二个参数,类型是String类型-->

/bean>

2、Setter注入(通过set方法注入)(使用property标签完成)

a)Property注入:

(将userDao对象注入到userBiz对象的userDao属性中,userDao属性是UserDao接口类型,可以在任何地方使用userDaobean对象)

beanid=”userDao”class=”org.spring.dao.impl.UserDaoImpl”>

beanid=”userBiz”class=”org.spring.biz.impl.UserBizImpl”>

propertyname=”userDao”ref=”userDao”/>

b)使用内部bean注入:

(使用内部bean注入只能在本bean中使用内部bean对象)

propertyname=”userDao”>

beanclass=”org.spring.dao.impl.UserDaoImpl”/>

/property>

c)为基本类型注入:

propertyname=”name”value=”shanshi”/>

d)集合注入:

i.Set集合

<

propertyname=”sets”>

<

set>

<

value>

sssssss<

/value>

/set>

ii.List集合

propertyname=”lists”>

list>

/list>

iii.Properties集合

propertyname=”properties”>

props>

propkey=”key1”>

value1<

/prop>

propkey=”key2”>

value2<

propkey=”key3”>

value3<

/props>

iv.Map集合

propertyname=”maps”>

map>

entrykey=”key1”value=”value1”/>

/map>

3、使用注解的方式注入依赖对象。

(2.0以后提供,就是在类的属性或方法中注入)

使用注解方式注入:

1、在xml中加入命名空间:

beansxmlns="

http:

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

xmlns:

xsi="

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

context=http:

//www.springframework.org/schema/context<

—命名空间-

xsi:

schemaLocation="

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/context《<

--schema文件-->

//www.springframework.org/schema/context/spring-context-2.5.xsdhttp:

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

2、打开注解的配置。

注册对多个注解进行解析处理的处理器:

AutowiredAnnotationBeanPostProcessor(解析@Autowired)、CommonAnnotationBeanPostProcessor(解析@Resource)

context:

annotation-config/>

-打开注解的配置-

3、使用注解进行依赖注入,

在java中使用@Autowired(Spring提供)和@Resource(javaee提供,已经纳入jdk1.6中)都可以将依赖对象注入,区别:

@Autowired默认按类型装配、@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。

@Resource的name属性可以指定依赖对象的名称,如果找不到该名称则返回null,如果不设置name属性,则会按照默认的名称查找依赖对象,如果找不到则按类型装配

@Autowired、@Resource可以在属性上使用也可以在setter方法上使用。

@AutowiredprivatePersonDaopersonDao//字段上使用

@Resource(name=personDao)

privatePersonDaopersonDao;

@Autowired//setter方法上使用

PublicvoidsetPersonDao(PersonDaopersonDao){}

@Autowired默认是按类型进行装配的,可以通过方法:

@Autowired@Qualifier(“name”)将@Autowired改为按名称装配

@Autowired(required=true)表示必须给该属性注入值,如果没有注入则报异常

@Autowired(required=false)表示给该属性注入值是可选的,如果没有注入则会注入一个null

三、通过在classpath自动扫描方式把组件纳入spring容器中管理(2.5以后提供)

1、在xml中加入命名空间:

2、<

component-scanbase-package="

package"

//打开自动扫描器

其中base-package为需要扫描的包(含子包),可以注册基本上所有的注解处理器。

3、使用注解方式标注哪些类将要交给spring管理,它的作用和在xml文件中定义bean是一样的。

4、在类中的注解方式有四个(在类上标注):

@Service用于标注业务层组件、

@Controller用于标注控制层组件(如struts中的action)、

@Repository用于标注数据访问组件,即DAO组件。

@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

4、这时bean的名称默认的为类名并将首字母改为小写,当然我们可以通过设置bean的名称来获得值(@Service(“name”)),并且可以在类上通过@scope(“prototype”)注解设置bean的作用域范围

5、初始化方法注解:

@PostConstruct(javaee提供),在方法上面指定这个注解,表示该方法在初始化时执行。

6、销毁方法注解:

@PreDestroy,在销毁时要执行的方法上面标记

AOP

一、AOP中的概念:

Aspect(切面):

指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象。

(将要对那些方法进行拦截,拦截后将要做什么,这些就是所谓的横切性关注点)

Joinpoint(连接点):

所谓连接点是指那些被拦截到的点,在spring中,这些点指的是方法。

(既拦截的方法),spring中只支持方法类型的连接点。

Pointcut(切入点):

切入点是指我们要对那些joinpoint进行拦截的定义,(既对那些方法进行拦截)

添加命名空间:

Advice(通知):

通知是指拦截到joinpoint之后所要做的事情。

通知分为:

前置通知(拦截到的方法执行前执行)、后置通知(拦截到的方法执行后执行)、异常通知(当拦截到的方法出现异常时执行)、最终通知(无论方法是否出现异常都会执行)及环绕通知(方法执行前、后都可以执行)

Target(目标对象):

代理的目标对象

Weave(织入):

将aspect应用到target对象并导致proxy对象创建的过程

Introduction(引入):

在不修改类代码的前提下,Introduction可以在运行期为类(代理类)动态地添加一些方法或Field.

引入命名空间

xmlns:

aop="

//www.springframework.org/schema/aop"

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd

//www.springframework.org/schema/aophttp:

//www.springframework.org/schema/aop/spring-aop-2.5.xsd"

Spring提供了两种切面声明方式,实际开发中我们可以选用其中一种:

●基于XML配置方式声明切面。

●基于注解方式声明切面。

一、注解方式:

1、注册AOP注解的处理类。

aop:

aspectj-autoproxy/>

2、使用注解定义切面(定义类,切面类中包含切入点)

3、只用注解定义切入点(定义类中的方法)

4、将切面交给spring进行管理

Interceptor.java(切面类)

packageorg.springaop.service.impl;

importorg.aspectj.lang.ProceedingJoinPoint;

importorg.aspectj.lang.annotation.After;

importorg.aspectj.lang.annotation.AfterReturning;

importorg.aspectj.lang.annotation.AfterThrowing;

importorg.aspectj.lang.annotation.Around;

importorg.aspectj.lang.annotation.Aspect;

importorg.aspectj.lang.annotation.Before;

importorg.aspectj.lang.annotation.Pointcut;

/**

*定义切面

*

***/

@Aspect//将类声明为切面

publicclassMyInterceptor{

/**定义切入点*/

@Pointcut("

execution(*org.springaop.service.impl.PersonServiceImpl.*(..))"

)//定义切入点表达式,表示将要对那些方法类中的方法进行拦截

//声明切入点的名称,声明方法和定义一个方法类似,切入点的名称myPoint

privatevoidmyPoint(){}

/**定义前置通知

@Before("

myPoint()"

publicvoidbeforeDemo(){

System.out.println("

前置通知"

);

}

*/

/**后置通知**/

@AfterReturning("

publicvoidafterReturningDemo(){

后置通知"

/***最终通知*/

@After("

publicvoidafterDemo(){

最终通知"

/**例外通知*/

@AfterThrowing("

publicvoidafterThrowingDemo(){

例外通知"

/**

*获得输入参数的通知

*注解中的参数名称和方法声明的参数名称一致,

*而且这时只能拦截到存在一个参数并且参数类型为String类型的方法

**/

myPoint()&

&

args(uname)"

publicvoiddoBefore(Stringuname){

前置通知获得输入参数:

"

+uname);

*获得返回值的通知

@AfterReturning(pointcut="

returning="

result"

publicvoiddoAfterReturning(Stringresult){

获得返回值的后置通知:

+result);

*获得异常信息的通知

@AfterThrowing(pointcut="

throwing="

e"

publicvoiddoAfterThrowing(Exceptione){

获得异常:

+e);

*环绕通知

*环绕通知的方法声明部分是固定的,只有方法名和访问权限可以改变

*@throwsThrowable

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

当前位置:首页 > 医药卫生 > 预防医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2