Spring注解

Spring的常见注解及其作用:

1. @Component 创建类对象,相当于配置<bean/>

bean的默认为类名首(字母小写),也可以指定id,例如@Component("stu")

2. @Service 与@Component功能相同.

2.1 写在ServiceImpl类上.

3. @Repository 与@Component功能相同.

3.1 写在数据访问层类(mapper)上.

4. @Controller 与@Component功能相同.

4.1 写在控制器类(controller)上.

5. @Resource(不需要写对象的get/set)

5.1 java中的注解

5.2 默认按照byName注入,如果没有名称对象,按照byType注入

      5.2.1 建议把对象名称和Spring容器中对象名相同

6. @Autowired(不需要写对象的get/set)

6.1 Spring的注解

6.2 默认按照byType注入.

7. @Value() 获取properties文件中内容

 

 

 

AOP

8. @Pointcut() 定义切点

9. @Aspect() 定义切面类

10. @Before() 前置通知

11. @After 后置通知

12. @AfterReturning 后置通知,必须切点正确执行

13. @AfterThrowing 异常通知

14. @Arround 环绕通知

@Component
@Aspect
public class AspectJ {
    @Before("execution(* com.gakki.entity.Admin.A())")
    public void beforeMethodA(){
        System.out.println("--前置通知--");
    }
    @After("execution(* com.gakki.entity.Admin.A())")
    public void afterMethodA(){
        System.out.println("--后置通知--");
    }
    @Around("execution(* com.gakki.entity.Admin.A())")
    public void roundMethodA(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知--前");
        Object proceed = joinPoint.proceed();
        System.out.println("环绕通知--后");
    }
    @AfterThrowing("execution(* com.gakki.entity.Admin.A())")
    public void ThrowMethodA(){
        System.out.println("异常通知");
    }
}
public class Test06 {
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext05.xml");
        Admin admin = app.getBean("admin", Admin.class);
        admin.A();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值