简体   繁体   中英

How can I create a new annotation?

How can I create a new annotation based on a Spring @Service annotation or @Component ?

I want just to change the name for a more semantic use: for example change the name to @TransactionelService .

You can create your own annotation (eg @MyComponent ) annotated with the corresponding spring annotation. For example:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface MyComponent {
}

Meta-annotations:

Annotations which can be used to annotate other annotations.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Service
@Transactional(timeout = 60)
public @interface MyTranscationalService{
}

--this recognizes the above code and scans the below code also

and we can use MyTranscationalService as annotation for other classes

@MyTranscationalService
public class TransferImpl implements TransferService{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM