Create the Person (see sample code below).
Add @Component annotation on Person class (see line 5 below).
Create members name and email (see lines 9-10 below)
Create accessor methods for name and email (see lines 11-22 below).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| package com.studytrails.tutorials.springannotationautodiscovery; import org.springframework.stereotype.Component; @Component public class Person { private String name = "Alba" ; private String email = "alba@gmail.com" ; public String getName() { return name; } public void setName(String name) { this .name = name; } public String getEmail() { return email; } public void setEmail(String email) { this .email = email; } } |
Create the spring-config.xml file (see below).
Let Spring know that annotation based auto-wiring will be used by declaring the tag context:annotation-config tag(see line 14 below)
Declare the tag context:component-scan tag and provide the name of the package that Spring should look into for automatic discovery of beans(see line 16 below)
Note that there are no bean declarations in spring-config.xml file thereby highlighting the automatic bean discovery mechanism of Spring.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| <? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation=" < context:annotation-config /> < context:component-scan base-package = "com.studytrails.tutorials.springannotationautodiscovery" /> </ beans > |
Finally, we need a java program to test the our setup.This is done by TestAnnotationAutoDiscovery (see source code below).
We need to tell Spring framework to use the 'spring-config.xml' to load our beans (see line 11 below).
We get the reference to Person class through Spring using the bean name 'person' (see line 12 below).
We access the getName() and getEmail() methods of Person class and print the output to verify that automatic discovery of beans using @Component annotation has occured successfully (see lines 13-14 below).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| package com.studytrails.tutorials.springannotationautodiscovery; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAnnotationAutoDiscovery { public static void main(String[] args) { System.out.println( "************** BEGINNING PROGRAM **************" ); ApplicationContext context = new ClassPathXmlApplicationContext( "spring-config.xml" ); Person person = (Person)context.getBean( "person" ); System.out.println( "Person's name = " + person.getName()); System.out.println( "Person's email = " + person.getEmail()); System.out.println( "************** ENDING PROGRAM **************" ); } } |
Running Sample Program
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.
No comments:
Post a Comment