1.1 Add two packages com.huizhi.hello and com.huizhi.test to src
1.2 Create a new Class file in com.huizhi.hello.
package com.huizhi.hello; public class hello { private String name; public void setName(String name) { this.name = name; } public String say() { return "hello :" + name ; } }
1.3 Create a new Class file in com.huizhi.test. Then run the test to see if you can print something to the console.
package com.huizhi.test; import com.huizhi.hello.hello; public class test { public static void main(String[] args) { hello h1 = new hello(); h1.setName("Zhang San"); System.out.println(h1.say()); } }
2. Copy the Spring jar package to the project.
3. Create a new XML configuration file applicationContext.xml in the src directory.
<?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="helloWorld" class="com.huizhi.hello.hello"> <property name="name" value="李四"/> </bean> </beans>
4. Go back to the com.huizhi.test.test.java file and comment out the original test instance. Added new test methods. Run the test and see the console output
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml"); hello h1 = (hello) appContext.getBean("helloWorld"); System.out.println(h1.say());