1, a new Java project: Springjavatest.
2, add spring2.5 Package (2.5 version can be injected with the annotation)
3, new two JavaBean:Room.java and House.java, as follows:
Package Com.bean; public class Room {private String area = "100 square meters";//Action public string say () {return "Me area is" +area;} Public String Getarea () {return area;} public void Setarea (String area) {This.area = area;}}
Package Com.bean; Import org.springframework.beans.factory.annotation.Autowired; public class House {@Autowired private Room Room, @Override public String toString () {return Room.say ()}}
4, modify the configuration file Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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.5.xsd "> <!--the beanpostprocessor will automatically function, to the callout @ autowired beans are automatically injected--> <bean class= " Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> <bean id=" Room "class=" "Com.bean.Room" > <property name= "area" value= "860 sqm/> </bean> <bean id=" house "class=" Com.bean.House "> </bean> </beans>
5, New test class Springtest.java
Package com.test import org.springframework.context.ApplicationContext; import Org.springframework.context.support.ClassPathXmlApplicationContext; Import Com.bean.House; public class Springtest {public static void main (string[] args) {string[] locations = {"Applicationcontext.xml"}; ApplicationContext context = new Classpathxmlapplicationcontext (locations); House House = [House] Context.getbean ("house"); System.out.println (house); } }