[Java] Configuring Spring IoC using @Annotations

In previous articles (Spring IoC – example, Spring IoC – autowiring) I have showed you how to configure Spring IoC by writing xml code. This thing could be much simpler – you can use Java @Annotations and no xml-code to do it (I suppose you don’t like xml-hell like me ;) ).

1. Maven preparation (if you don’t use maven – just download jars and place them in appropriate place in your project).
Type into your pom.xml:

        
                3.0.6.RELEASE
        
        
                
                        org.springframework
                        spring-core
                        ${org.springframework.version}
                
                
                        org.springframework
                        spring-expression
                        ${org.springframework.version}
                
                
                        org.springframework
                        spring-beans
                        ${org.springframework.version}
                
                
                        org.springframework
                        spring-context
                        ${org.springframework.version}
                
                
                        org.springframework
                        spring-asm
                        ${org.springframework.version}
                
                
                        org.springframework
                        spring-context-support
                        ${org.springframework.version}
                
        

2. Example Java code.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class SpringAnnotationsTest {

        public static void main(String[] args) {
                // Creating ApplicationContext
                ApplicationContext context = new AnnotationConfigApplicationContext(
                                SpringConfiguration.class);
                // Lets print out if everything is OK
                System.out.println(context.getBean(Bike.class));
        }

}

// @Configuration tells that this class keeps bean's configuration.
@Configuration
class SpringConfiguration {
        // @Bean annotation =  in xml!
        @Bean
        public Wheel frontWheel() {
                return new Wheel("The name of front wheel");
        }

        @Bean
        public Wheel rearWheel() {
                return new Wheel("The name of rear wheel");
        }

        @Bean
        public Bike bike() {
                Bike bike = new Bike("CodeSmuggler's bike");
                bike.setDescription("Bike description," + "I'm using Spring IoC "
                                + "with annotations!");
                return bike;
        }
}

/**
 * Simple example class representing Bike:
 * has name, description and 2 wheels.
 */
class Bike {
        private String name;
        // @Autowired in this case means the same as in xml
        //  !
        @Autowired
        private Wheel frontWheel;
        @Autowired
        private Wheel rearWheel;
        private String description;

        public Bike(String name) {
                this.name = name;
        }
        
        // WARNING: getters and setters removed to provide readability

        @Override
        public String toString() {
                return "NAME: " + getName() + "\nDESCRIPTION: " + getDescription()
                                + "\nFRONTWHEEL: " + getFrontWheel() + "\nREARWHEEL: " + getRearWheel();
        }
}

/**
 * Bike's wheel.
 * Has name.
 */
class Wheel {
        private String name;

        public Wheel(String name) {
                this.name = name;
        }

        public String getName() {
                return name;
        }

        @Override
        public String toString() {
                return "wheelname: " + getName();
        }
}

3. Results.

NAME: CodeSmuggler's bike
DESCRIPTION: Bike description,I'm using Spring IoC with annotations!
FRONTWHEEL: wheelname: The name of front wheel
REARWHEEL: wheelname: The name of rear wheel

4. Explanation.
I have replaced all xml from Spring IoC – autowiring and used @Annotations to wire components. I hope that the code is self-explanatory to you, but if you will have problem understanding it – please write a comment.

This entry was posted in coding, enterprise edition, java and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: