Directly on the code:
1. Write a class that outputs Hello World
Package cn.itcat.spring.sh.ioc.creatobject;
public class HelloWorld {public
void SayHello () {
System.out.println ("Hello Xiajun");
}
}
2. Put the class into the spring container
<?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.5.xsd ">
<bean id= "HelloWorld" class= "Cn.itcat.spring.sh.ioc.creatobject.HelloWorld" ></bean>
</ Beans>
3. Testing
package cn.itcast.spring.sh.test;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Cn.itcat.spring.sh.ioc.creatobject.HelloWorld;
public class Creatobjecttest {@Test public void Testcreatobjest () {/** * 1. Start the spring container * 2. Remove objects from the Spring container * 3. Method of Object Invocation */applicationcontext context = new Classpathxmlapplicationconte
XT ("Applicationcontext.xml");
HelloWorld HelloWorld = (HelloWorld) context.getbean ("HelloWorld");
Helloworld.sayhello (); }
}