Annotation base configuration in standalone Java program

267 Views Asked by At

I was made aware with the annotation base configuration (the @Resource) feature in Java EE which I really like. Then I noticed that the annotation was actually part of Java SE.

So I wonder if I can use it with Java SE. I can surely use the annotation in a standalone program but I am find how to actually configure it. All the examples I found involve creating a Java EE configuration file. Can any one give me a confirmation (or give me a reference to one) that it can be used outside of Java EE environment? and how to do that?

2

There are 2 best solutions below

0
On

Annotations have specific purposes and can only be used for those purposes. So Java EE annotations will in most cases have no function outside Java EE. But for instance jUnit and JPA annotations should work in a Java SE applications. You can also roll your own if you feel like it.

0
On

I'm thinking the same thing. Something must process annotations--this is what Spring does, but currently I'm not using Spring.

It wouldn't be too difficult to add custom annotation processing to handle the @Resource, @PostConstruct and @PreDestroy annotations (It would probably be a few dozen lines of code) but at some point you gotta think "Well, Spring is already doing all that and a heck of a lot more".

They are also implemented by Tomcat for WebServices, so you can just use them but only on properly configured @WebService classes.