spring 3 annotation dependency injection
Solution 1
Your best bet is to start with the Spring 3.0 Documentation. If you already are familiar with Dependency Injection using xml configuration, take a look at Spring's annotation support in Section 3.9 Annotation-based container configuration
. There should be plenty of details there to help get you started.
If this is not a WebApp, and you need to load the ApplicationContext
yourself, you should take a look at Section 3.11 Java-based container configuration
. This section details how you can create your ApplicationContext
with your xml configuration.
I would recommend using the @Autowired
annotations for wiring with @Component
annotation for defining beans within the ApplicationContext
, and use the scan(String)
method provided by the AnnotationConfigApplicationContext
to find all annotated components:
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("your.package.here");
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
//use myService
}
Solution 2
Here is a simple and clear one:
http://www.simplespringtutorial.com/annotations.html
Solution 3
I suggest going with adding: <context:annotation-config />
to your xml context file to have it load the annotations. Then use: <context:component-scan base-package="my.package" />
to import all the annotated classes. This requires no code whatsoever and is incredibly clean. You can also add include/exclude filters to the scan in the xml file.
Then, just use whatever annotations you want in your classes. Make sure of the annotation construction to give them IDs for referencing later, i.e. @Component("mycomponentbean").
Comments
-
TechFind almost 2 years
For a new legacy application I have to use Latest Spring framework 3.x with Hibernate 3.x. Earlier I used Spring IoC with xml configuration. Now i would like to go for annotation based IoC.
Below link from rose india shows use annotation without xml file. rose india
but i have found some other way of implementing in another site: blog
I am confused here which way to implement IoC with latest technology. Please refer some good site for sample. Also please share some sample code for IoC with annotation.
EDIT:
Spring reference document 3.x says use @Configuration or xml configuration i.e. with annotation <context:annotation-config/>. Which one is better to use? Why?
-
TechFind over 12 yearsdo i need to use new AnnotationConfigApplicationContext(); to load xml file? only bean declaration is sufficient in xml file?
-
TechFind over 12 yearsok, in my context xml file do i need to specify bean id and its property for IoC?
-
Tony over 12 yearsYou can just add: <context:annotation-config /> to your context.xml file to have it load the annotations. Then use: <context:component-scan base-package="my.package" /> to import all the annotated classes. This requires no code whatsoever.
-
hellojava almost 8 years@gupta59 Thanks. Fixed