Spring配置及应用
来源:互联网 发布:手机绘图软件artrage 编辑:程序博客网 时间:2024/05/29 07:19
Spring配置及应用
1、 spring的ioc原理,在spring项目中是不需要new对象的,这样耦合性会比较高,不利于后期维护修改,ioc就是用来解耦的。Ioc原理就是使用dom4j解析xml配置文件,然后建立工厂,使用反射原理来对bean对象进行管理操作。
2、 ioc操作分两部分:
a) ioc的配置文件方式. (xml。不常用)
i. 使用类的无参构造函数来创建(常用)
1. 只需要在配置文件中添加如下标签,<bean id="user"class="Entity.UserEntity"></bean>。其中id属性值随意起,class属性值为该bean对应的类的全路径。如果类里面没有无参构造,会抛异常。
ii. 使用静态工厂创建
1. 准备好类对象以后,还需要创建他的工厂类,在工厂中写静态方法,返回类对象。还需要在配置文件中添加如下标签,<bean id="userfactory"class="Entity.UserFactory"factory-method="getUserEntity"></bean>。其中id属性值随意起,class属性值为工厂全路径,factory-method属性值为工厂中的静态方法名称。
iii. 使用实例工厂创建
1. 准备好类对象以后,还需要创建他的工厂类,在工厂中写普通方法,返回类对象。
b) ioc的注解方式(重点)
i. 简介。
1. 代码里面特殊标记,使用注解可以完成功能
ii. 写法。
1. @注解名称(属性名称=属性值)
步骤(准备工作):
(1) 在配置文件中引入相关约束(beans约束、context约束)
<beansxmlns="http://www.springframework.org/schema/beans"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
(2) 在配置文件中添加如下标签
<!--开启注解扫描它会到包里面扫描类、方法、属性上面扫描是否有注释。base-package属性值为 bean对象所在的包名-->
<context:component-scanbase-package="Entity"></context:component-scan>
注解创建对象
只需在类上面写上注解即可
注解注入属性
对象类型注入
首先编写两个类,在一个类中定义将另一个类定义成属性。
@Autowired与@Resource功能一致,@Resource便于区分
3、 使用set方法注入属性(xml配置文件方式的时候比较常用)
a) 字符串注入
b) 对象类型注入
首先编写类(略)。Service类包含dao这个属性
Spring的AOP操作(了解,不常用)
需要使用aspectj和spring一起进行aop操作,使用aspectj实现aop有两种方式
(1) xml配置
a) 需要在配置文件中导入相关约束
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
b) 使用表达式配置切入点
i. 切入点:实际增强的方法
ii. 常用的表达式
1. 语法格式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
2. 实例
a) execution(* 方法名全路径(..))
b) execution(* 类名全路径.*(..))
c) execution(* *.*(..))
iii. 例
<!--配置对象-->
<bean id="user"class="Entity.UserEntity"></bean>
<bean id="person"class="Entity.PersonEntity"></bean>
<!--配置aop操作-->
<aop:config>
<!--配置切入点(要增强的方法) id属性值为随意起的-->
<aop:pointcut id="pointcut1" expression="execution(*Entity.UserEntity.add(..))"/>
<!--配置切面(增强)ref属性值为增强类实例化对象名称 method属性值为增强类中增强的方法 pointcut-ref属性值为上面的id属性值-->
<aop:aspect ref="person">
<aop:before method="zengqiang"pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
会抛异常,因为缺少包
(2) 注解
a) 创建对象、导入约束(略)
b) 在spring配置文件中开启aop操作
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
c) 在增强类上使用注解完成操作
Spring事务管理
Xml配置方式(不常用)
1、 引约束
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
2、 配置事务管理器(此处配置的是jdbc,配置别的数据库框架需要做修改)
<!--此处id随意起,class根据所使用的数据库框架来决定-->
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入dataSource属性,数据库框架不同,注入的属性也不同-->
<propertyname="dataSource" ref="dataSource"></property>
</bean>
3、 配置事务增强
<!--此处id随意起,transaction-manager为上面的id-->
<tx:advice id="txadvice"transaction-manager="transactionManager">
<!--做事务操作-->
<tx:attributes>
<!--设置进行事务操作的方法的匹配规则,name可以直接写方法名(过于局限),将所有方法统一命名规则用*做匹配
例方法名为insertxxx和insertsss就可以写成insert*来匹配-->
<tx:methodname=""/>
</tx:attributes>
</tx:advice>
4、 配置切面
<aop:config>
<!--切入点 id随意起 expression匹配的是service类中所有方法-->
<aop:pointcutid="pointcut1" expression="execution(*Service.TestService.*(..))"/>
<!--切面 advice-ref值为事务增强的id值,pointcut-ref值为上面的切入点id-->
<aop:advisoradvice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
注解方式
1、配置事务管理器(与xml配置方式一样)
<!--此处id随意起,class根据所使用的数据库框架来决定-->
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入dataSource属性,数据库框架不同,注入的属性也不同-->
<propertyname="dataSource" ref="dataSource"></property>
</bean>
2、开启事务注释
<!--开启事务注释,此处transaction-manager值与事务管理器id一致-->
<tx:annotation-driventransaction-manager="transactionManager"/>
3、在要使用事务的方法所在类上面添加注释
@Transactional
public class TestService {
spring所需约束
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
- Spring配置及应用
- Struts+Spring+Hibernate配置及应用
- Spring Web 简单应用及配置
- Struts+Spring+Hibernate配置及应用
- Spring MVC 4 学习6: JdbcTemplate配置及应用
- Spring应用的运行环境及属性配置综述
- spring框架中定时器的配置及应用
- Spring quartz 应用配置
- spring quarz 配置应用
- Spring 事务管理及配置
- spring简介及配置
- ibatis配置及应用
- 配置环境变量 及 应用
- OpenWRT配置及应用
- pdsh配置及应用
- wget配置及应用
- bash配置及应用
- structs2配置及应用
- 触发器创建表日志
- Spring—模块详解
- centos7搭建pptp服务器
- 委托和事件的学习
- JMeter 格式化输出rest服务返回json数据
- Spring配置及应用
- Hadoop数据迁到MaxCompute
- Objective Runtime(3)
- ubuntu16.04 给chromium网页浏览器安装adobe flashplayer
- 导出Excel工具类—POI
- window下强制杀死某个进程用taskkill /pid 进程号 -t -f命令
- Unity与Android间的交互
- 【Baltic2008】黑手党(Mafia)(BSOI2891)
- spring-mvc ajax post请求乱码