spring 基本操作总结主要是aop以及依赖注入的基本配置
一所需架包
spring
commom-logging.jar spring.jar
注解
common-annotation.jar
aop面向切面
aspectjrt.jar aspectjweaver.jar cglibb-nodep.ja(权限带代理proxy)
jdbc database
common-pool.1.6.jar common-dbhp.jar mysql-connector-bin-*.*.*.jar
二
配置文件的模板beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>
以上模板引入了面向切面(aop),权限代理(context)以及数据库操作(tx)的命名空间
三基本使用
1.创建bean对象
1.1默认通过set
<bean id="persionBean" class="com.my.PersionBean.impl.PersionBean" destroy-method="destory" lazy-init="true" init-method="init"></bean>
1.2通过静态工厂(factory-method)
<bean id="persionBean2" class="com.my.PersionBean.impl.PersionBeanFactory" lazy-init="true" factory-method="creBeanFactory"></bean>
1.3通过工厂非静态
<bean id="persionBean3" class="com.my.PersionBean.impl.PersionFactory" ></bean>
<bean id="persionBean4" factory-bean="persionBean3" scope="prototype" factory-method="createOrderFactory"></bean>
2.注入bean
<property name="persion" ref="persionBean4" ></property>
3.注入其他属性
<property name="name" value="校长"></property>
<property name="age" value="15"></property>
<property name="habits">
4.注入List<>属性
<property name="habits">
<list>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</list>
</property>
5.通过<constructor-arg index="0" value="xiaozhang"></constructor-arg>(构造函数index参数索引)
6.对象的创建scope
scope="prototype" 实体模式(多个对象)
scope="singleton"单例模式(default)在容器只有一个对象
另外还有session,request 域
通过注解方式注入
<context:annotation-config/>//开启注解
通过@Resource(name="student")指定bean的id注入 通过@Autowired@Qualifier(value="student")(命名id)注入
通过注解方式创建bean
@Component (任何)
@Repository(value="student")(数据访问)
@Controller(控制)
@Service(服务)
. @Scope("prototype")(指定域)
@PostConstruct(初始化方法)
@PreDestroy(销毁方法)
7.aop切面
使用jdk进行权限代理
必须有接口
@Aspect
public class MyIntercepter {
@Pointcut("execution (* com.my.aop.impl.PersionServiceBean.*(..))")
private void anyMethod() {//声明切入点
}
@Before("anyMethod()&&args(name)")
private void doAccessCheck(String name){
System.out.println("这是前置通知");
}
@AfterReturning(pointcut="anyMethod()",returning="result")
private String doAfterReturn(String result){
System.out.println("这是后置通知"+result);
return result;
}
@AfterThrowing("anyMethod()")
private void doAfterThrow(){
System.out.println("这是例外通知");
}
@After("anyMethod()")
private void doAfter(){
System.out.println("这是最终通知");
}
@Around("anyMethod()")
public Object around(ProceedingJoinPoint point) throws Throwable{
System.out.println("这是环绕通知");
Object resultObject=point.proceed();
System.out.println("推出");
return resultObject;
}
使用cglib注解方式 <aop:aspectj-autoproxy/>(启动注解)
public class MyIntercepter {
@Pointcut("execution (* com.my.aop.impl.PersionServiceBean.*(..))")
private void anyMethod() {//声明切入点
}
@Before("anyMethod()&&args(name)")
private void doAccessCheck(String name){
System.out.println("这是前置通知");
}
@AfterReturning(pointcut="anyMethod()",returning="result")
private String doAfterReturn(String result){
System.out.println("这是后置通知"+result);
return result;
}
@AfterThrowing("anyMethod()")
private void doAfterThrow(){
System.out.println("这是例外通知");
}
@After("anyMethod()")
private void doAfter(){
System.out.println("这是最终通知");
}
@Around("anyMethod()")
public Object around(ProceedingJoinPoint point) throws Throwable{
System.out.println("这是环绕通知");
Object resultObject=point.proceed();
System.out.println("推出");
return resultObject;
}
}
使用xml
<aop:aspectj-autoproxy/>
<bean id="persionServiceBean" class="com.my.aop.impl.PersionServiceBean"></bean>
<bean id="interceptor" class="com.my.aop.impl.myInterceptor"> </bean>
<aop:config>
<aop:aspect id="aspect" ref="interceptor">
<aop:pointcut expression="execution (* com.my.aop.impl.PersionServiceBean.*(..))" id="mycut"/>
<aop:before method="doAccessCheck" pointcut-ref="mycut"/>
<aop:around method="around" pointcut-ref="mycut"/>
</aop:aspect>
</aop:config>
spring对jdbc操作
<context:property-placeholder location="classpath:jdbc.properties"/>(引入属性文件)
(创建dataSource) <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="initialSize" value="${initialSize}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
</bean>
<bean id="txManager" (创建事物bean)class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
以注解方式
<tx:annotation-driven transaction-manager="txManager" />(启动注解)
以xml
<aop:config>
<aop:pointcut expression="execution(* com.my.jdbc.service.impl.StudentService.*(..))" id="transactionPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointCut"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="NOT_SUPPORTED"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
详细见例子
spring 基本操作总结主要是aop以及依赖注入的基本配置的更多相关文章
- Spring IOC源代码具体解释之容器依赖注入
Spring IOC源代码具体解释之容器依赖注入 上一篇博客中介绍了IOC容器的初始化.通过源代码分析大致了解了IOC容器初始化的一些知识.先简单回想下上篇的内容 加载bean定义文件的过程.这个过程 ...
- 三大框架 之 Spring(IOC控制反转、DI依赖注入)
目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...
- Spring详解(三)------DI依赖注入
上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建.初始化.销毁等工作交给spring容器来做.那么创建对象的时候,有可 ...
- Spring升级案例之IOC介绍和依赖注入
Spring升级案例之IOC介绍和依赖注入 一.IOC的概念和作用 1.什么是IOC 控制反转(Inversion of Control, IoC)是一种设计思想,在Java中就是将设计好的对象交给容 ...
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...
- spring(一、原理、IOC、AOP、依赖注入)
1.spring原理 内部最核心的就是IOC了,动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射,反射其实就是在运行时动态的去创建.调用对象,Spring就是在运 ...
- 类比Spring框架来实现OC中的依赖注入
如果你之前使用过JavaEE开发中的Spring框架的话,那么你一定对依赖注入并不陌生.依赖注入(DI: Dependency Injection)是控制反转(IoC: Inversion of Co ...
- Spring-初识Spring框架-IOC控制反转(DI依赖注入)
---恢复内容开始--- IOC :控制反转 (DI:依赖注入)使用ioc模式开发 实体类必须有无参构造方法1.搭建Spring环境下载jarhttp://maven.springframework. ...
- Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)
目录 1 什么是spring框架 2 spring框架的特点 3 spring容器 3.1 什么是spring容器 3.2 spring容器创建对象的编程步骤 3.4 spring容器创建对象的方式 ...
随机推荐
- 从100PV到1亿级PV网站架构演变
如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 一个网站就像一个人,存在一个从小到大的过程.养一个网站和养一个人一样,不同时期需要不 ...
- 应用OpenCV进行OCR字符识别
opencv自带一个字符识别的例子,它的重点不是OCR字符识别,而主要是演示机器学习的应用.它应用的是UCI提供的字符数据(特征数据). DAMILES在网上发布了一个应用OpenCV进行OCR的例子 ...
- 关于hook d3d在war3上绘图的几点疑问
学到了. 你得记住,com接口全是stdcall调用方式,不是thiscall,不要搞错了,不信,你看接口定义 因为com调用得兼容c调用,而c没有thiscall调用方式stdcall时,this指 ...
- java web学习
一直想下决心好好学下java web,但是总是间断,虽然我的方向是ios,但是觉得后台也是相当重要,毕竟移动端实际上更多也就是展示后台拉取到的信息,搞移动端的不能总是受制于后台,各位看官觉得呢? 这两 ...
- Leetcode026. Remove Duplicates from Sorted Array
water class Solution { public: int removeDuplicates(vector<int>& nums) { for(vector<int ...
- PHP获取今天、昨天、明天的日期
<?php echo "今天:".date("Y-m-d")."<br>"; echo "昨天:".d ...
- CSS 之 Opacity多浏览器透明度兼容处理
用来设定元素透明度的 Opacity 是CSS 3里的一个属性.当然现在还只有少部分浏览器支持. 不过各个浏览器都有自己的私有属性来支持,其中包括老版本的Mozilla和Safari: IE: fil ...
- 为js和css文件自动添加版本号
web应用必然要面对缓存问题,无论前台后台都会涉足缓存.特别是对于前端而言,缓存利用的是否得当直接关系到应用的性能. 通常情况下,我们会倾向于使用缓存,因为缓存一方面可以减少网络开销,一方面可以减轻服 ...
- Python之路【第八篇】:堡垒机实例以及数据库操作
Python之路[第八篇]:堡垒机实例以及数据库操作 堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient ...
- 简单linux块设备驱动程序
本文代码参考<LINUX设备驱动程序>第十六章 块设备驱动程序 本文中的“块设备”是一段大小为PAGE_SIZE的内存空间(两个扇区,每个扇区512字节) 功能:向块设备中输入内容,从块设 ...