您好,欢迎访问一九零五行业门户网

使用注解配置spring的详细介绍

一、使用注解配置spring1.步骤 1.1 导包4+2+spring-aop
4代表:
2代表:
日志包:com.springsource.org.apache.commons.logging-1.1.1.jar
可选:com.springsource.org.apache.log4j-1.2.15.jar(老版本要导入的,导入可以保证一定能运行)
1.2 为主配置文件引入新的命名空间(约束)
1.3 开启使用注解代替配置文件
 1.4 在类中使用注解完成配置
2.将对象注册到容器//<bean name="user" class="cn.itcast.bean.user" />//@component(user)//    @service(user) // service层//    @controller(user) // web层@repository(user)// dao层
3.修改对象的作用范围//指定对象的作用范围@scope(scopename=singleton)
4.值类型注入 通过反射的field赋值,破坏了封装性:
    @value(tom)    private string name;
通过set方法赋值,推荐使用.:
    @value(tom)    public void setname(string name) {this.name = name;     }
5.引用类型注入    //@autowired //自动装配//问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.//@qualifier(car2)//使用@qualifier注解告诉spring容器自动装配哪个名称的对象private car car;
推荐方式:
    @resource(name=car)//手动注入,指定注入哪个名称的对象private car car;
6.初始化|销毁方法    @postconstruct //在对象被创建后调用.init-methodpublic void init(){         system.out.println(我是初始化方法!);     }     @predestroy //在销毁之前调用.destory-methodpublic void destory(){         system.out.println(我是销毁方法!);     }
二、sts插件1.手动安装插件(成功率低)
步骤1:
步骤2:
步骤3:
2.直接使用spring装好插件的eclipse
三、spring与junit整合测试1.导包4+2+aop+test2.配置注解//帮我们创建容器@runwith(springjunit4classrunner.class)//指定创建容器时使用哪个配置文件@contextconfiguration(classpath:applicationcontext.xml)public class demo {//将名为user的对象注入到u变量中@resource(name=user)private user u;
3.测试    @testpublic void fun1(){                  system.out.println(u);              }
四、spring中的aop1.aop思想介绍
2.spring中的aop概念
3.spring实现aop的原理  3.1 动态代理(优先)
被代理对象必须要实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术
3.2 cglib代理(没有接口)
第三方代理技术,cglib代理.可以对任何类生成代理.代理的原理是对目标对象进行继承代理. 如果目标对象被final修饰.那么该类无法被cglib代理.
4.aop名词学习
五、spring中的aop演示1.步骤(xml配置) 1.1 导包4+2 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
1.2 准备目标对象public class userserviceimpl implements userservice {     @overridepublic void save() {         system.out.println(保存用户!);//int i = 1/0;    }     @overridepublic void delete() {         system.out.println(删除用户!);     }     @overridepublic void update() {         system.out.println(更新用户!);     }     @overridepublic void find() {         system.out.println(查找用户!);     } }
1.3 准备通知//通知类public class myadvice {    //前置通知     //        |-目标方法运行之前调用//后置通知(如果出现异常不会调用)//        |-在目标方法运行之后调用//环绕通知//        |-在目标方法之前和之后都调用//异常拦截通知//        |-如果出现异常,就会调用//后置通知(无论是否出现 异常都会调用)//        |-在目标方法运行之后调用//----------------------------------------------------------------//前置通知public void before(){         system.out.println(这是前置通知!!);     }//后置通知public void afterreturning(){         system.out.println(这是后置通知(如果出现异常不会调用)!!);     }//环绕通知public object around(proceedingjoinpoint pjp) throws throwable {         system.out.println(这是环绕通知之前的部分!!);         object proceed = pjp.proceed();//调用目标方法system.out.println(这是环绕通知之后的部分!!);return proceed;     }//异常通知public void afterexception(){         system.out.println(出事啦!出现异常了!!);     }//后置通知public void after(){         system.out.println(这是后置通知(出现异常也会调用)!!);     } }
1.4 配置进行织入,将通知织入目标对象中<?xml version="1.0" encoding="utf-8"?><beans xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://www.springframework.org/schema/beans" 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/context http://www.springframework.org/schema/aop "><!-- 准备工作: 导入aop(约束)命名空间 --><!-- 1.配置目标对象 --><bean name="userservice" class="cn.itcast.service.userserviceimpl" ></bean><!-- 2.配置通知对象 --><bean name="myadvice" class="cn.itcast.d_springaop.myadvice" ></bean><!-- 3.配置将通知织入目标对象 --><aop:config><!-- 配置切入点 public void cn.itcast.service.userserviceimpl.save() void cn.itcast.service.userserviceimpl.save() * cn.itcast.service.userserviceimpl.save() * cn.itcast.service.userserviceimpl.*() * cn.itcast.service.*serviceimpl.*(..) * cn.itcast.service..*serviceimpl.*(..)--><aop:pointcut expression="execution(* cn.itcast.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></beans>
2.步骤(注解配置) 前面的1,2,3步和xml配置一样 2.4 配置进行织入,将通知织入目标对象中 applicationcontext.xml:
<?xml version="1.0" encoding="utf-8"?><beans xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://www.springframework.org/schema/beans" 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/context http://www.springframework.org/schema/aop "><!-- 准备工作: 导入aop(约束)命名空间 --><!-- 1.配置目标对象 --><bean name="userservice" class="cn.itcast.service.userserviceimpl" ></bean><!-- 2.配置通知对象 --><bean name="myadvice" class="cn.itcast.e_annotationaop.myadvice" ></bean><!-- 3.开启使用注解完成织入 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
通知类:
//通知类@aspect//表示该类是一个通知类public class myadvice {     @pointcut(execution(* cn.itcast.service.*serviceimpl.*(..)))public void pc(){}//前置通知//指定该方法是前置通知,并制定切入点@before(myadvice.pc())public void before(){         system.out.println(这是前置通知!!);     }//后置通知@afterreturning(execution(* cn.itcast.service.*serviceimpl.*(..)))public void afterreturning(){         system.out.println(这是后置通知(如果出现异常不会调用)!!);     }//环绕通知@around(execution(* cn.itcast.service.*serviceimpl.*(..)))public object around(proceedingjoinpoint pjp) throws throwable {         system.out.println(这是环绕通知之前的部分!!);         object proceed = pjp.proceed();//调用目标方法system.out.println(这是环绕通知之后的部分!!);return proceed;     }//异常通知@afterthrowing(execution(* cn.itcast.service.*serviceimpl.*(..)))public void afterexception(){         system.out.println(出事啦!出现异常了!!);     }//后置通知@after(execution(* cn.itcast.service.*serviceimpl.*(..)))public void after(){         system.out.println(这是后置通知(出现异常也会调用)!!);     } }
以上就是使用注解配置spring的详细介绍的详细内容。
其它类似信息

推荐信息