Spring的注解详细总结

1.准备工作 
(1)导入common-annotations.jar 
(2)导入schema文件 文件名为spring-context-2.5.xsd 
(3)在xml的beans节点中配置 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans    
  3. .......  
  4. xmlns:context="http://www.springframework.org/schema/context "  
  5.   
  6.        xsi:schemaLocation="   
  7. .......  
  8. http://www.springframework.org/schema/context   
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd "  >  
  10.   
  11. .....  
  12. <!--将针对注解的处理器配置好  -->  
  13. <context:annotation-config />   
  14. .....  
  15. <beans>  

2.在java代码中使用@Autowired或@Resource注解方式进行装配 ,这两个注解的区别是: 
@Autowired默认按类型装配,@Resource默认按名称装配,当找不到名称匹配的bean才会按类型装配。 

默认注解 
Java代码   收藏代码
  1. @Resource  private PersonDao persondao;  
  2. <bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>  
首先是判断persondao是否与xml里的personDao名字相同,相同则注入, 
不同则判断persondao是否是com.hf.dao.impl.PersonDaoBean类型,是则注入不是则返回null. 

Java代码   收藏代码
  1. @Resource(name="personDao")  private PersonDao dao;  
  2. <bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>  

判断name名称是否与bean中id相同不同则返回null 

Spring自带注解方式 
Java代码   收藏代码
  1. @Autowired @Qualifier("personDao"private PersonDao persondao;  
默认是按类型注入  加上@Qualifier("personDao")则按名称注入 

3.通过在classpath 自动扫描方式把组件纳入spring容器中管理 
spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component @Service @Controller @Repository 
注解的类,并将这些类纳入进spring容器中管理。它们的作用和xml文件中使用bean 节点配饰组件是一样的。 
(1)使用到了注解的功能(需要注解准备工作的内容) 
(2)在xml中加入 
<context:component-scan base-package="com.hf" /> 
其中base-package 为需要扫描的包(包含子包) 
(3) 
@Service用于标注业务层组件 
@Controller用于标注控制层组件(如struts中的action) 
@Repository用于标注数据访问组件 ,即DAO 组件 
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 

(4) 
Java代码   收藏代码
  1. 业务类  
  2. @Service  
  3. public class PersonServiceBean implements PersonService {.....}  
  4. 输出类  
  5. AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");  
  6. PersonService personService= (PersonService)cxt.getBean("personServiceBean");  
  7. System.out.println(personService);  
  8. cxt.close(););  

使用注解中bean的id默认名称为类名称的首字母小写名称 
-------------------------------------------------- 
自己指定名称 
Java代码   收藏代码
  1. @Service("aa"//默认作用域范围 是单例范围  
  2. public class PersonServiceBean implements PersonService {.....}  
  3. 输出类  
  4. AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");  
  5. PersonService personService= (PersonService)cxt.getBean("aa");  
  6. System.out.println(personService);  
  7. cxt.close();  
  8. --------------------------------------------------  
  9. @Service("aa"@Scope("prototype")//修改bean的作用域  
  10. public class PersonServiceBean implements PersonService {....}  
----------------------------------------------------------- 
Java代码   收藏代码
  1. @PostConstruct   
  2. public void init(){    
  3. System.out.println("初始化");  
  4. }   
  5. @PreDestroy   
  6. public void destory(){     
  7. System.out.println("释放资源");  
  8. }  


4.AOP注解方式 
(1)准备工作: 
.导入common-annotations.jar  aspectjrt.jar aspectweaver.jar cglib-nodep-2.13.jar 
.导入schema文件 文件名为spring-aop-2.0.xsd 
.在xml的beans节点中配置 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans    
  3. .......  
  4. xmlns:aop="http://www.springframework.org/schema/aop "  
  5.   
  6.        xsi:schemaLocation="   
  7. .......  
  8. http://www.springframework.org/schema/aop   
  9. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "  >  
  10.   
  11. .....  
  12. <!-- 配置解释处理器 为@AspectJ注解提供支持  -->  
  13. <aop:aspectj-autoproxy />  
  14. .....  
  15. <beans>  

(2) 
<bean id="myInterceptor" class="com.hf.service.impl.MyInterceptor"></bean> 
<bean id="personService" class="com.hf.service.impl.PersonServiceBean" ></bean> 
将切面和被拦截的类交给spring管理 
(3)切面类 

Java代码   收藏代码
  1. @Aspect //定义切面类  
  2. public class MyInterceptor {  
  3. /** 
  4. *  @Pointcut("execution(* com.hf.service..*.*(..))")表达式含义 
  5. * 第一个* 表示返回值类型为任意类型 
  6. * com.hf.service..  两个点表示包路径下的子包的类也要拦截 
  7. * com.hf.service..*.* 子包的所有类中的所有方法 第一个*是方法第二个*是类 
  8. * (..)代表方法参数随意 可有可无可多可少 
  9. * **/  
  10.   
  11.    @Pointcut("execution (* com.hf.service.impl.PersonServiceBean.*(..))")// 定义切入点  
  12. private void andMethod()//声明一个切入点   
  13. {}  
  14.   
  15. /*   @Before("andMethod()") 
  16. public void doAccessCheck(){ 
  17. System.out.println("前置通知");     
  18. } 
  19. */  
  20.   
  21. @Before("andMethod() && args(name)"//带参数 只拦截符合参数类型的方法  
  22. public void doAccessCheck(String name){  
  23. System.out.println("前置通知"+name);      
  24. }  
  25.   
  26. /*   @AfterReturning("andMethod()") 
  27. public void doFaterReturning(){     
  28. System.out.println("后置通知"); 
  29. }*/  
  30.   
  31. @AfterReturning(pointcut="andMethod()",returning="result")//带返回值的 无返回值的方法 result为null  
  32. public void doFaterReturning(String result){//拦截方法执行后 获取返回值对象  
  33. System.out.println("后置通知:"+result);  
  34. }  
  35.   
  36. @After("andMethod()")  
  37. public void doAfter(){  
  38. System.out.println("最终通知");    
  39. }  
  40.   
  41. /*   @AfterThrowing("andMethod()") 
  42. public void doAfterThrowing(){     
  43. System.out.println("例外通知"); 
  44. }*/  
  45.   
  46. @AfterThrowing(pointcut="andMethod()" , throwing="e"//获取例外并打印  
  47. public void doAfterThrowing(Exception e){     
  48. System.out.println("例外通知:"+e);  
  49. }  
  50.   
  51. @Around("andMethod()")//环绕通知  
  52. public Object doBasecProfiling(ProceedingJoinPoint pjp )throws Throwable{  
  53. //if(){//判断是否与权限  
  54. System.out.println("进入通知");  
  55. Object result = pjp.proceed();  
  56. System.out.println("离开 通知");  
  57. //}  
  58. return result;  
  59.   
  60. }  
  61. }  

(4)业务类 PersonServiceBean 
Java代码   收藏代码
  1. public class PersonServiceBean implements PersonService {  
  2.   
  3.  public void save(String name){  
  4. throw new RuntimeException("纯属例外");  
  5. // System.out.println("我是Save方法"+name);  
  6. }  
  7. public String update() {    
  8. return "我是update方法";  
  9. }  
  10. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值