These annotations are class-level annotations, which get registered with the Spring container. The following figure shows stereotype annotations:
Let's use @Component in the application. We will use Ch02_Reference_DI as a base project, and perform the following steps:
- Create Ch02_Demo_Annotation, add all the jars that we added earlier, and along with them, add spring-aop.jar to it as well.
- Create or copy Address.java in the com.ch02.beans package.
- Create or copy Customer.java in the com.ch02.stereotype.annotation package. Rename it as Customer_Component.java.
- Add to it the default constructor, as the code to which we refer does not have any default constructor. The code can be as follows:
public Customer_Component() { // TODO Auto-generated constructor stub cust_id=100; cust_name="cust_name"; cust_address=new Address(); cust_address.setBuild_no(2); cust_address.setCity_name("Delhi"); }
- Annotate the class with @Component, as shown in the following piece of code:
@Component public class Customer_Component { }
Here, we configured a component that is auto-scannable. By default, the id of this component will be a decapitalized class name. In our case, it's customer_Component. If we want to use customized id , we can modify the configuration. In case we want to use myObject as the bean id, we need to update the code as follows:
@Component(value="myObject")
Moreover, there is no need to configure it in XML as we did earlier. However, we will still need XML for some other configurations.
- Create or copy customer.xml in classpath.
- Add the following code to consider the annotations:
<context:annotation-config/>
We have already seen how to configure the context namespace.
- Create TestCustomer_component, and get the bean of Customer_Component, as shown in the following piece of code, in order to find out what is the output of our configuration:
public class TestCustomer_component { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("customer.xml"); Customer_Component customer = (Customer_Component) context.getBean("customer_Component"); System.out.println(customer.getCust_name() + " " + customer.getCust_id() + customer.getCust_address()); } }
On execution, we will get the following stack trace of exception:
- If we did everything right, then why do we still get an exception? The reason is that although we made the components auto-scannable, we forgot to tell the container where to scan for annotations. Let's configure where to scan for components, as shown in the following lines of code:
<context:component-scan base-package="com.ch02.stereotype.*"> </context:component-scan>
Here, all the subpackages of com.ch02.streotype will be scanned to find out the bean which has id as customer_Component.
- After adding the configuration, we will get the following output, which displays the data member values:
In the same way, we can use the other annotations, but we will discuss them one by one in the upcoming chapters.