- Hello World
HelloWorld.java
package com.test.spring01; public class HelloWorld { private String name; public HelloWorld() { System.out.println("调用构造函数..."); } public String getName() { return name; } public void setName2(String name) {//对应name="name2" this.name = name; } public void sayHello() { System.out.println("Hello," + name); } }
Main.java
package com.test.spring01; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); System.out.println("创建对象"); HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("a1"); helloWorld.sayHello(); } }
applicationContext.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="a1" class="com.test.spring01.HelloWorld"> <property name="name2" value="spring"/> </bean> </beans>
运行结果
调用构造函数...
创建对象
Hello,spring
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1547187
时间: 2024-12-30 05:03:37