使用注解配置spring
一、步骤
1.为主配置文件引入新的命名空间(约束)
导入spring-context-4.2.xsd schema约束
2.开启使用注解代理配置文件
// 在applicationcontext.xml中 // 指定扫描cn.zhli13.bean包下所有类的注解 // 扫描时会扫描指定包下的所有子孙包 <context:component-scan base-package="cn.zhli13.bean"></context:component-scan>
3.在类中使用注解完成配置
// @componet等
二、将对象注册到容器
// 将user注册到spring容器中,相当于<bean name="user" class="cn.zhli13.bean.user"></bean> @componet("user") @service("user") // service层 @controller("user") // web层 @repository("user") // dao层
三、修改对象的作用范围
// 指定对象的作用域 @scope(scopename="prototypo") // 非单例模式
四、值类型注入
// 1.通过反射的field赋值,破坏了封装性 @value("tom") private string name; // 2.通过set方法赋值,推荐使用 @value("tom") public void setname(string name) { this.name = name; }
五、引用类型注入
@autowired // 自动装配 // 问题:如果匹配多个类型一致的对象,将无法选择具体注入哪一个对象 @qualifier("car2")// 使用@qualifier注解告诉spring容器自动装配哪个名称的对 private car car;
六、初始化、销毁方法
@postconstruct // 在对象创建后调用,xml配置中的init-method public void init () { system.out.println("init"); } @predestory // 在对象销毁之前调用,xml配置中的destory-method public void destory () { system.out.println("destory"); }
spring与junit整合测试
一、导包
额外导入
二、配置注解
// 帮我们创建容器 @runwith("springjunit4classrunner") // 指定创建容器时使用哪个配置文件 @contextconfiguration("classpath:applicationcontext.xml") public class demo { // 将名为user的对象注入到变量u中 @resource(name="user") private user u; }
三、测试
@test public void fun1() { system.out.println(u); }
spring中的aop
一、概念
aop思想:横向重复、纵向抽取
aop概念:spring能够为容器中管理的对象生成动态代理
二、spring实现aop的原理
1.动态代理(优先)
被代理对象必须要实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术
2.cglib代理(没有接口)
第三方代理技术,cglib代理.可以对任何类生成代理.代理的原理是对目标对象进行继承代理. 如果目标对象被final修饰.那么该类无法被cglib代理.
三、aop名词学习
spring aop的使用
一、导包
// spring的aop包 spring-aspects-4.2.4.release.jar spring-aop-4.2.4.release.jar // spring需要第三方aop包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.release.jar
二、准备目标对象
public class userserviceimpl implements userservice { @override public void save() { system.out.println("保存用户!"); } @override public void delete() { system.out.println("删除用户!"); } @override public void update() { system.out.println("更新用户!"); } @override public void find() { system.out.println("查找用户!"); } }
三、准备通知
// 1.使用注解方式 // 表示该类是一个通知类 @aspect public class myadvice { @pointcut("execution(* cn.zhli13.service.*serviceimpl.*(..))") public void pc(){} //前置通知 //指定该方法是前置通知,并制定切入点 @before("myadvice.pc()") public void before(){ system.out.println("这是前置通知!!"); } //后置通知 @afterreturning("execution(* cn.zhli13.service.*serviceimpl.*(..))") public void afterreturning(){ system.out.println("这是后置通知(如果出现异常不会调用)!!"); } //环绕通知 @around("execution(* cn.itcast.zhli13.*serviceimpl.*(..))") public object around(proceedingjoinpoint pjp) throws throwable { system.out.println("这是环绕通知之前的部分!!"); object proceed = pjp.proceed();//调用目标方法 system.out.println("这是环绕通知之后的部分!!"); return proceed; } //异常通知 @afterthrowing("execution(* cn.zhli13.service.*serviceimpl.*(..))") public void afterexception(){ system.out.println("出事啦!出现异常了!!"); } //后置通知 @after("execution(* cn.itcast.zhli13.*serviceimpl.*(..))") public void after(){ system.out.println("这是后置通知(出现异常也会调用)!!"); } } // 2.使用xml配置 // 移除上述通知类的注解就是xml配置的通知类
四、配置进行织入,将通知织入目标对象中
// 1.使用注解配置 <!-- 准备工作: 导入aop(约束)命名空间 -->
<!-- 1.配置目标对象 --> <bean name="userservice" class="cn.zhli13.service.userserviceimpl" ></bean> <!-- 2.配置通知对象 --> <bean name="myadvice" class="cn.zhli13.aop.myadvice" ></bean> <!-- 3.开启使用注解完成织入 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> // 2.使用xml配置 <!-- 准备工作: 导入aop(约束)命名空间 --> <!-- 1.配置目标对象 --> <bean name="userservice" class="cn.zhli13.service.userserviceimpl" ></bean> <!-- 2.配置通知对象 --> <bean name="myadvice" class="cn.zhli13.aop.myadvice" ></bean> <!-- 3.配置将通知织入目标对象 --> <aop:config> <!-- 配置切入点 public void cn.zhli13.service.userserviceimpl.save() void cn.zhli13.service.userserviceimpl.save() * cn.zhli13.service.userserviceimpl.save() * cn.zhli13.service.userserviceimpl.*() * cn.zhli13.service.*serviceimpl.*(..) * cn.zhli13.service..*serviceimpl.*(..) --> <aop:pointcut expression="execution(* cn.zhli13.service.*serviceimpl.*(..))" id="pc"/> <aop:aspect ref="myadvice" > <!-- 指定名为before方法作为前置通知 --> <aop:before method="before" pointcut-ref="pc" /> <!-- 后置 --> <aop:after-returning method="afterreturning" pointcut-ref="pc" /> <!-- 环绕通知 --> <aop:around method="around" pointcut-ref="pc" /> <!-- 异常拦截通知 --> <aop:after-throwing method="afterexception" pointcut-ref="pc"/> <!-- 后置 --> <aop:after method="after" pointcut-ref="pc"/> </aop:aspect> </aop:config>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
before社区电量是什么意思 Before社区电量获得方法
RecycleView入门详解(教你全面掌握RecycleView用法)
动态权限请求框架RxPermissions(几行代码搞定权限)
URL路径@PathVariable出现点号“.“时值遭截断问题
网友评论