spring注解详解
1.准备工作
(1)导入common-annotations.jar
(2)导入schema文件 文件名为spring-context-2.5.xsd
(3)在xml的beans节点中配置
<?xml version="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>
@Resource private PersonDao persondao;<bean id="personDao" name="code">@Resource(name="personDao") private PersonDao dao;<bean id="personDao" name="code">@Autowired @Qualifier("personDao") private PersonDao persondao;默认是按类型注入 加上@Qualifier("personDao")则按名称注入
业务类@Servicepublic class PersonServiceBean implements PersonService {.....}输出类AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");PersonService personService= (PersonService)cxt.getBean("personServiceBean");System.out.println(personService);cxt.close(););
@Service("aa") //默认作用域范围 是单例范围public class PersonServiceBean implements 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 PersonServiceBean implements PersonService {....}-----------------------
@PostConstruct public void init(){ System.out.println("初始化");} @PreDestroy public void destory(){ System.out.println("释放资源");}
<?xml version="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>
@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 void doAccessCheck(String name){System.out.println("前置通知"+name); }/* @AfterReturning("andMethod()")public void doFaterReturning(){ System.out.println("后置通知");}*/@AfterReturning(pointcut="andMethod()",returning="result")//带返回值的 无返回值的方法 result为nullpublic void doFaterReturning(String result){//拦截方法执行后 获取返回值对象System.out.println("后置通知:"+result);}@After("andMethod()")public void doAfter(){System.out.println("最终通知"); }/* @AfterThrowing("andMethod()")public void doAfterThrowing(){ System.out.println("例外通知");}*/@AfterThrowing(pointcut="andMethod()" , throwing="e") //获取例外并打印public void doAfterThrowing(Exception e){ System.out.println("例外通知:"+e);}@Around("andMethod()")//环绕通知public Object doBasecProfiling(ProceedingJoinPoint pjp )throws Throwable{//if(){//判断是否与权限System.out.println("进入通知");Object result = pjp.proceed();System.out.println("离开 通知");//}return result;}}
public class PersonServiceBean implements PersonService { public void save(String name){throw new RuntimeException("纯属例外");// System.out.println("我是Save方法"+name);}public String update() { return "我是update方法";}}