Spring入门

来源:互联网 发布:淘宝在线云客服门户 编辑:程序博客网 时间:2024/05/22 12:22

 首先要导入spring必须的jar包,建议在build-path里建一个属于Spring的类库Spring-core,以便日后方便使用。

第一步:创建bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="person" class="Person">
        <property name="name" value="xingoo"/>
        <property name="age" value="12"/>
    </bean>
 </beans>

第二步:创建实体类


public class Person {
 private String name;
 private int age;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
 public void info()
 {
    System.out.println("name:"+getName()+" age:"+getAge());

 }

}

第三步 创建测试类,使用配置的xml

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
  ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
  Person p=ctx.getBean("person",Person.class);
  p.info();
 }
}

 想一想这个简单的例子究竟实现了什么?

其实就是通过配置文件,让我们可以给一个实体类的属性赋值。

 

 

0 0
原创粉丝点击