package com.xu.pojo;
public class Bye {
private String bye;
@Override
public String toString() {
return "再见啦!";
}
public String getBye() {
return bye;
}
public void setBye(String bye) {
this.bye = bye;
}
}
package com.xu.pojo;
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "你好呀!";
}
}
<?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.xsd">
<!--使用Spring来创建对象,在Spring这些都称为Bean
类型 变量名= new 类型();
Hello hello=new Hello();
id=> 变量名
class=>new的对象
property=>相当于给对象中的属性赋值
-->
<bean id="hello1" class="com.xu.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
<bean id="bye1" class="com.xu.pojo.Bye">
<property name="bye" value="Bye~"/>
</bean>
</beans>
import com.xu.pojo.Bye;
import com.xu.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello1");
System.out.println(hello.toString());
Bye bye= (Bye) context.getBean("bye1");
System.out.printf(bye.toString());
}
}
