spring注解之——@Service


Spring框架中的注解@Service用于将类标记为Spring应用程序上下文中的服务组件。它主要用于指示带注释的类充当应用程序中的服务层组件。

以下是关于注释的一些要点@Service

  1. 业务逻辑:通常,带有注释的类@Service包含业务逻辑。这些类负责封装和实现应用程序的业务规则和流程。

  2. Spring Bean:当 Spring 扫描应用程序中的组件(使用组件扫描或显式配置)时,它会识别带有注释的类,@Service并将它们注册为应用程序上下文中的 Spring bean。

  3. 依赖注入:服务通常使用依赖注入注入到其他 Spring 组件中,例如控制器、其他服务或存储库。这有助于实现应用程序不同层之间的松散耦合。

  4. 事务行为:Spring的事务管理能力常常应用在服务层。默认情况下,Spring 将@Service带注释的类中的方法标记为事务性的,允许您以声明方式控制事务边界。

    package org.springframework.stereotype;
    
    @java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
    @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
    @java.lang.annotation.Documented
    @org.springframework.stereotype.Component
    public @interface Service {
        @org.springframework.core.annotation.AliasFor(annotation = org.springframework.stereotype.Component.class)
        java.lang.String value() default "";
    }

  • @java.lang.annotation.TargetService:该注释指定可以应用该注释的程序元素的类型。在这种情况下,指定注释Service只能应用于类型,例如类、接口或枚举。
  • @java.lang.annotation.Retention:该注解指定带有该Service注解的注解应保留多长时间。该值RUNTIME指示注释Service应该在运行时通过反射可用。
  • @java.lang.annotation.Documented:此注释指示用 注释的元素Service应包含在生成的 JavaDoc 文档中。
  • @org.springframework.stereotype.Component:该注解表明它是Spring Framework 提供的注解Service的特化。Component这意味着Service在组件扫描过程中,带有 注解的类将被自动检测并注册为 Spring bean。
  • @org.springframework.core.annotation.AliasFor:该注解表示value()该注解的属性是该注解属性的Service别名。这允许用户在使用或注释类时可以互换使用属性。value()ComponentvalueServiceComponent
  • java.lang.String value() default "";value:这定义了注释中命名的属性Service。它允许用户在使用 注释类时指定自定义值Service。如果未显式提供值,则默认为空字符串。
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP是Spring框架中的一个重要模块,它提供了面向切面编程(AOP)的支持。AOP是一种编程思想,它可以在不改变原有代码的情况下,通过在程序运行时动态地将代码“织入”到现有代码中,从而实现对原有代码的增强。 Spring AOP提供了基于注解的AOP实现,使得开发者可以通过注解的方式来定义切面、切点和通知等相关内容,从而简化了AOP的使用。 下面是一个基于注解的AOP实现的例子: 1. 定义切面类 ```java @Aspect @Component public class LogAspect { @Pointcut("@annotation(Log)") public void logPointcut() {} @Before("logPointcut()") public void beforeLog(JoinPoint joinPoint) { // 前置通知 System.out.println("执行方法:" + joinPoint.getSignature().getName()); } @AfterReturning("logPointcut()") public void afterLog(JoinPoint joinPoint) { // 后置通知 System.out.println("方法执行完成:" + joinPoint.getSignature().getName()); } @AfterThrowing(pointcut = "logPointcut()", throwing = "ex") public void afterThrowingLog(JoinPoint joinPoint, Exception ex) { // 异常通知 System.out.println("方法执行异常:" + joinPoint.getSignature().getName() + ",异常信息:" + ex.getMessage()); } } ``` 2. 定义业务逻辑类 ```java @Service public class UserService { @Log public void addUser(User user) { // 添加用户 System.out.println("添加用户:" + user.getName()); } @Log public void deleteUser(String userId) { // 删除用户 System.out.println("删除用户:" + userId); throw new RuntimeException("删除用户异常"); } } ``` 3. 在配置文件中开启AOP ```xml <aop:aspectj-autoproxy/> <context:component-scan base-package="com.example"/> ``` 在这个例子中,我们定义了一个切面类LogAspect,其中通过@Aspect注解定义了一个切面,通过@Pointcut注解定义了一个切点,通过@Before、@AfterReturning和@AfterThrowing注解分别定义了前置通知、后置通知和异常通知。 在业务逻辑类中,我们通过@Log注解标注了需要增强的方法。 最后,在配置文件中,我们通过<aop:aspectj-autoproxy/>开启了AOP功能,并通过<context:component-scan>扫描了指定包下的所有组件。 这样,当我们调用UserService中的方法时,就会触发LogAspect中定义的通知,从而实现对原有代码的增强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值