I am trying to learn Spring and Hibernate and i am really struggling to understand Annotations and how they work. Most of the example i am seeing on the Internet are annotation based examples so i need to understand how the annotations work first before i can learn Spring or Hibernate
I have an idea of what they are and what they are used for. I know that they replace the xml configuration. I.e. You can configure beans directly within Java code using annotations. What i dont understand is how to use them and when they can be used.
Trying to understand how this can be done i think it would be helpful if i see the difference between the two. I have here a simple Spring program. If i was to convert this sample program to use annotations what would i need to do?
The reason i want to do it this way is because the program i have provided below is one that i understand very well (an example from the Spring in Action book that i am currently reading). If it is converted to an annotations version i will get an idea as to how and where annotations can be used.
Any suggestions?
Thanks in advance
instrumentalist.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="saxophone" class="com.sia.ch1.instrumentalist.Saxophone" />
<bean id="piano" class="com.sia.ch1.instrumentalist.Piano" />
<!-- Injecting into bean properties Ken 1 -->
<bean id="kenny" class="com.sia.ch1.instrumentalist.Instrumentalist">
<property name="song" value="Jingle Bells"/>
<property name="instrument" ref="piano"/>
</bean>
</beans>
Instrumentalist interface
package com.sia.ch1.instrumentalist;
public interface Instrument {
void play();
}
Instrumentalist implementor
package com.sia.ch1.instrumentalist;
import com.sia.ch1.performer.PerformanceException;
import com.sia.ch1.performer.Performer;
public class Instrumentalist implements Performer{
private Instrument instrument;
private String song;
public Instrumentalist(){}
public void perform() throws PerformanceException{
System.out.print("Playing " + song + " : ");
instrument.play();
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public void setSong(String song) {
this.song = song;
}
}
Instruments - Piano
package com.sia.ch1.instrumentalist;
public class Piano implements Instrument{
public Piano(){}
public void play(){
System.out.println("PLINK PLINK");
}
}
Instruments - Saxophone
package com.sia.ch1.instrumentalist;
public class Saxophone implements Instrument{
public Saxophone(){}
public void play(){
System.out.println("TOOT TOOT TOOT");
}
}
Main class
package com.sia.ch1.instrumentalist;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.sia.ch1.performer.PerformanceException;
import com.sia.ch1.performer.Performer;
public class InstrumentalistApp {
public static void main(String[] args){
ApplicationContext ctx = new FileSystemXmlApplicationContext("c:\\projects\\test\\conf\\instrumentalist.xml");
Performer performer = (Performer) ctx.getBean("kenny");
try {
performer.perform();
} catch (PerformanceException e) {
e.printStackTrace();
}
}
}
Exception
package com.sia.ch1.performer;
public class PerformanceException extends Exception {
public PerformanceException() {
super();
// TODO Auto-generated constructor stub
}
public PerformanceException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public PerformanceException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public PerformanceException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
Edit 1
To try and convert the above i am going through these two simple examples:
Ex1: http://jroller.com/habuma/entry/reducing_xml_with_spring_2
Ex2: http://www.theserverside.com/tutorial/Spring-Without-XML-The-Basics-of-Spring-Annotations-vs-Spring-XML-Files
I kind of understand the examples in the first URL but the second one confused me a bit. In the example in the second URL, what is purpose of the SummaryConfig
class? It looks as though the SummaryConfig
class is a Java version of the XML file. This approach was not used in the example in the first example. What is the difference between the two?
Could it be that when you using annotations you can put the configuration details in a Java class (e.g. SummaryConfig
) and you can also put the annotations in the beans themselves as in the examples in the first URL?
Thanks
Edit 2
Here is what i have done so far,
I have modified the xml document to remove the configuration and enable the auto-scan of components (Note: i changed the package name for the modified versions)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.sia.ch1.instrumentalist.annotate" />
</beans>
Added the @Component annotation to the Piano and Saxophone classes. I think this tells the container that this class should be included in the classes to be auto-scanned. right?
package com.sia.ch1.instrumentalist.annotate;
import org.springframework.stereotype.Component;
@Component
public class Piano implements Instrument{
public Piano(){}
public void play(){
System.out.println("PLINK PLINK");
}
}
package com.sia.ch1.instrumentalist.annotate;
import org.springframework.stereotype.Component;
@Component
public class Saxophone implements Instrument{
public Saxophone(){}
public void play(){
System.out.println("TOOT TOOT TOOT");
}
}
This is where i am stuck (the Instrumentalist class).
Is the @Component annotation required in this class? Or is it only required if the class is to be referenced from another class?
I know that i need to @Autowire the instrument and song properties but how do i know if i want to autowire byname or bytype etc
How would i autowire the String property if in this class there is no bean that represents it? i.e the instrument property would refer to the piano class but what would the song property be autowired with?
package com.sia.ch1.instrumentalist.annotate;
//
import org.springframework.stereotype.Component;
import com.sia.ch1.performer.PerformanceException;
import com.sia.ch1.performer.Performer;
//
@Component
public class Instrumentalist implements Performer{
private Instrument instrument;
private String song;
public Instrumentalist(){}
public void perform() throws PerformanceException{
System.out.print("Playing " + song + " : ");
instrument.play();
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public void setSong(String song) {
this.song = song;
}
}
I think i am right in that no annotations are required on any of the other classes.
Thanks