Followers

springIOC: SI with primitive and String

Employee.java
==============

package myspring;
public class Employee {
    private int id;
    private String name;
    public Employee(){
        System.out.println("Constructor...");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        System.out.println("setId...");
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("setName...");
        this.name = name;
    }
}

Beans.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-3.0.xsd">

    <bean id="emp" class="myspring.Employee" lazy-init="true">
        
        <property name="id" value="111"></property>
        <property name="name"><value>Thomas</value></property>
        <!-- -->
    </bean>
</beans>

Test.java
==========

package myspring;

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

public class Test {

      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
          Employee emp = (Employee)context.getBean("emp");
          emp.setId(123);
          System.out.println(emp.getId()+"  "+emp.getName());
          
    }    
}

No comments:

Post a Comment