spring注解详解

spring注解详解

文章分类:Java编程

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>  

<?xmlversion="1.0" encoding="UTF-8"?>

<beans 

.......

xmlns:context="http://www.springframework.org/schema/context"

 

       xsi:schemaLocation="

.......

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd"  >

 

.....

<!--将针对注解的处理器配置好  -->

<context:annotation-config/>

.....

<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>  

@Resource  private PersonDao persondao;

<beanid="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>  

@Resource(name="personDao")  private PersonDao dao;

<beanid="personDao"class="com.hf.dao.impl.PersonDaoBean"></bean>


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

Spring自带注解方式

Java代码

  1. @Autowired @Qualifier("personDao") private PersonDao persondao;  

@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(););  

业务类

@Service

public class PersonServiceBeanimplements PersonService {.....}

输出类

AbstractApplicationContext cxt= new ClassPathXmlApplicationContext("beans.xml");

PersonService personService=(PersonService)cxt.getBean("personServiceBean");

System.out.println(personService);

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 {....}  

@Service("aa") //默认作用域范围 是单例范围

public class PersonServiceBeanimplements PersonService {.....}

输出类

AbstractApplicationContext cxt= new ClassPathXmlApplicationContext("beans.xml");

PersonService personService=(PersonService)cxt.getBean("aa");

System.out.println(personService);

cxt.close();

--------------------------------------------------

@Service("aa")@Scope("prototype")//修改bean的作用域

public class PersonServiceBeanimplements PersonService {....}

-----------------------------------------------------------

Java代码

  1. @PostConstruct   
  2. public void init(){    
  3. System.out.println("初始化");  
  4. }   
  5. @PreDestroy   
  6. public void destory(){     
  7. System.out.println("释放资源");  
  8. }  

@PostConstruct

public void init(){ 

System.out.println("初始化");

}

@PreDestroy

public void destory(){  

System.out.println("释放资源");

}



4.AOP注解方式
(1)准备工作:
.导入common-annotations.jar  aspectjrt.jaraspectweaver.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>  

<?xmlversion="1.0" encoding="UTF-8"?>

<beans 

.......

xmlns:aop="http://www.springframework.org/schema/aop"

 

       xsi:schemaLocation="

.......

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"  >

 

.....

<!-- 配置解释处理器 为@AspectJ注解提供支持  -->

<aop:aspectj-autoproxy />

.....

<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. @Before("andMethod() && args(name)") //带参数 只拦截符合参数类型的方法  
  21. public void doAccessCheck(String name){  
  22. System.out.println("前置通知"+name);      
  23. }  
  24.   
  25. /*   @AfterReturning("andMethod()") 
  26. public void doFaterReturning(){     
  27. System.out.println("后置通知"); 
  28. }*/  
  29.   
  30. @AfterReturning(pointcut="andMethod()",returning="result")//带返回值的 无返回值的方法 result为null  
  31. public void doFaterReturning(String result){//拦截方法执行后 获取返回值对象  
  32. System.out.println("后置通知:"+result);  
  33. }  
  34.   
  35. @After("andMethod()")  
  36. public void doAfter(){  
  37. System.out.println("最终通知");    
  38. }  
  39.   
  40. /*   @AfterThrowing("andMethod()") 
  41. public void doAfterThrowing(){     
  42. System.out.println("例外通知"); 
  43. }*/  
  44.   
  45. @AfterThrowing(pointcut="andMethod()" , throwing="e") //获取例外并打印  
  46. public void doAfterThrowing(Exception e){     
  47. System.out.println("例外通知:"+e);  
  48. }  
  49.   
  50. @Around("andMethod()")//环绕通知  
  51. public Object doBasecProfiling(ProceedingJoinPoint pjp )throws Throwable{  
  52. //if(){//判断是否与权限  
  53. System.out.println("进入通知");  
  54. Object result = pjp.proceed();  
  55. System.out.println("离开 通知");  
  56. //}  
  57. return result;  
  58.   
  59. }  
  60. }  

@Aspect //定义切面类

public class MyInterceptor {

/**

*  @Pointcut("execution(*com.hf.service..*.*(..))")表达式含义

* 第一个* 表示返回值类型为任意类型

* com.hf.service..  两个点表示包路径下的子包的类也要拦截

* com.hf.service..*.* 子包的所有类中的所有方法 第一个*是方法第二个*是类

* (..)代表方法参数随意 可有可无可多可少

* **/

 

   @Pointcut("execution (*com.hf.service.impl.PersonServiceBean.*(..))")// 定义切入点

private void andMethod()//声明一个切入点

{}

 

/*   @Before("andMethod()")

public void doAccessCheck(){

System.out.println("前置通知");   

}

*/

 

@Before("andMethod()&& args(name)") //带参数 只拦截符合参数类型的方法

public voiddoAccessCheck(String name){

System.out.println("前置通知"+name);   

}

 

/*   @AfterReturning("andMethod()")

public voiddoFaterReturning(){   

System.out.println("后置通知");

}*/

 

@AfterReturning(pointcut="andMethod()",returning="result")//带返回值的 无返回值的方法 result为null

public voiddoFaterReturning(String result){//拦截方法执行后 获取返回值对象

System.out.println("后置通知:"+result);

}

 

@After("andMethod()")

public void doAfter(){

System.out.println("最终通知"); 

}

 

/*   @AfterThrowing("andMethod()")

public voiddoAfterThrowing(){   

System.out.println("例外通知");

}*/

 

@AfterThrowing(pointcut="andMethod()", throwing="e") //获取例外并打印

public voiddoAfterThrowing(Exception e){  

System.out.println("例外通知:"+e);

}

 

@Around("andMethod()")//环绕通知

public ObjectdoBasecProfiling(ProceedingJoinPoint pjp )throws Throwable{

//if(){//判断是否与权限

System.out.println("进入通知");

Object result = pjp.proceed();

System.out.println("离开 通知");

//}

return result;

 

}

}


(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. }  

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值