回顾一下今天老师讲的东西,发现有几处知识点:
1.J2EE中的Spring框架中,我们可以通过注解的方式实现组件的注入,还有bean的自动装配,代码如下:
- import org.springframework.stereotype.Service;
- @Service(value = "personService")
- public class PersonServiceImpl implements PersonService {
- @Override
- public String getPersonName() {
- System.out.println("我是getPersonName()方法。。。");
- return "返回结果";
- }
- @Override
- public void save(String name) {
- System.out.println("我是save()方法。。。");
- }
- @Override
- public void update(int name) {
- System.out.println("我是update()方法。。。");
- }
- @Override
- public void throwException() {
- System.out.println("我是throwException()方法。。。");
- throw new RuntimeException("异常了。。");
- }
- }
- 以上的这段代码实现了组件的注入容器,通过简单的注解就完成了
- 2.还有一个重要的知识便是切面的编写,先上代码:
-
- @Aspect
- public class MyInterceptor {
- @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
- private void anyMethod() {}//声明一个切入点
- @Before("anyMethod() && args(name)")
- public void doAccessCheck(String name) {
- System.out.println("前置通知:"+ name);
- }
- @AfterReturning(pointcut="anyMethod()",returning="result")
- public void doAfterReturning(String result) {
- System.out.println("后置通知:"+ result);
- }
- @After("anyMethod()")
- public void doAfter() {
- System.out.println("最终通知");
- }
- @AfterThrowing(pointcut="anyMethod()",throwing="e")
- public void doAfterThrowing(Exception e) {
- System.out.println("例外通知:"+ e);
- }
- @Around("anyMethod()")
- public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
- //if(){//判断用户是否在权限
- System.out.println("进入方法");
- Object result = pjp.proceed();
- System.out.println("退出方法");
- //}
- return result;
- }
- }