Spring 的Helloworld

来源:互联网 发布:硕鼠mac下载 编辑:程序博客网 时间:2024/06/03 10:49

导入jar 包



1. 项目结构:


~~~~~~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Person.java

package com.baidu.spring.beans.test;public class Person {private String name;public void setName(String name) {this.name = name;}public void showName() {System.out.println("Person's name: " + name);}}
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.xsd"><!-- 配置Bean --><bean id="person" class="com.baidu.spring.beans.test.Person"><property name="name" value="LiLei"></property></bean></beans>
TestSpringPerson.java
package com.baidu.spring.beans.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpringPerson {public static void main(String[] args) {//1. 创建Spring 的IOX 容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//2. 从容器中 获取bean 实例 Person person = (Person) ac.getBean("person");//3. 调用person de showName()方法person.showName();}}



三者的对应关系:
































0 0
原创粉丝点击