开发者

Java - Automatic implementation of service locator pattern using annotations

Spring almost provides what I want. In Spring you can simply annotate classes with @Component and then use "context:component-scan" to have Spring automatically search for components. Then later you can create an application context and call getBean(String, Class) to get an implementation of any interface or class.

For example, if I have an interface "Mp3Service" and I want to register a new implementation called "InternetMp3Service", all I have to do is add the @Component annotation to the class definition.

The only problem with this for me is that I want to use all of this in an applet and so I get a java.lang.RuntimePermission exception for the permission "accessDeclaredMembers". I don't want to sign the applet and I don't want to have to create an explicit configuration for the beans.

What I'm looking for then is a framework/library that will go through the annotations at compile/build time and create a configuration that can then be read at run-time.

For example, if nothi开发者_运维百科ng comes up here and I decide it's worth the trouble, I could write my own program to go throw all the classes and look for a certain annotation and then create a spring xml configuration file. However, I thought someone might now of something already available.

Thanks.


This use case actually makes sense, but i'm sure nobody has implemented it yet. I see two ways of doing it:

a) using the Pluggable annotation Processing API. The problem here is that Spring does very advanced logic when scanning for annotations, and you will probably have to re-implement that for the annotation processor.

b) using a Main Class that does what Spring does (have a look at the source of org.springframework.context.annotation.ComponentScanBeanDefinitionParser to get an idea of where to start). Wire up the main class to a maven or ant build to have it automatically executed at compile-time. The problem here is that you will have to work on compiled classes, not on sources, so your Service Locator class won't be available at compile time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜