Loading
 
What's Next: View All Tutorial →

Spring Bean Auto-wiring @Component using annotation and setting value @Value

A simple program to autowire bean using annotation and setting and retrieving value from  Spring bean Refer Spring Hello World for project setup   1. Spring bean (Java class)   AutoWireBean.java (in src/m...

Published Date: 29/12/2014  Last Modified Date: 29/12/2014    New Post



A simple program to autowire bean using annotation and setting and retrieving value from  Spring bean



Refer Spring Hello World for project setup
 

1. Spring bean (Java class)
 

AutoWireBean.java (in src/main/java/com/autowire/)
package com.tutorial.autowire;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AutoWireBean {

    @Value("120")
    private int productCode;
    @Value("Laptop")
    private String productName;

    public int getProductCode() {
        return productCode;
    }

    public void setProductCode(int productCode) {
        this.productCode = productCode;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    @Override
    public String toString() {
        return "AutoWireBean [productCode=" + productCode + ", productName="
                + productName + "]";
    }

    public AutoWireBean(int productCode, String productName) {
        super();
        this.productCode = productCode;
        this.productName = productName;
    }

    public AutoWireBean() {
        super();

    }

}


@Value - To set the value

@Component – Indicates a auto scan component.


5. Spring bean configuration file


Spring bean configruation file contains all the bean creation, instantiation releated information

Create a new file SpringBean.xml in the resources folder
 
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>

    <bean id="autoWireBean" class="com.tutorial.autowire.AutoWireBean"/>
</beans>

 

<context:annotation-config/> - to scan the annotated java file




6. Program Execution

Run Application.java, it will load the Spring bean configuration file (SpringBean.xml) and retrieve the Spring bean via getBean() method and print the product code and product name.

File: Application.java  (in src/main/java/com/tutorial/autowire)
package com.tutorial.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("SpringBean.xml");

        AutoWireBean obj = (AutoWireBean) context.getBean("autoWireBean");

        System.out.println(obj.getProductCode());
        System.out.println(obj.getProductName());
    }
}



 

7. Output

Right click and Run the Application.java file
Dec 29, 2014 7:23:59 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [SpringBean.xml]
120
Laptop

 
Awaiting for Administrator approval





Tags: Spring Core

← Back to list


Related Post




×