Spring4.2第一个小例子

Spring入门

第一步:创建java项目导入如下jar包
commons-logging-1.1.3.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-context-support-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
第二步:在项目的src目录下穿件bean.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-4.2.xsd">
 <bean name="d"  class="java.util.Date"></bean>
 <bean name="df"  class="java.text.SimpleDateFormat">
  <constructor-arg>
    <value>yyyy-MM-dd hh:mm:ss</value>
  </constructor-arg>
 </bean>
 <bean name="dd"  class="java.text.SimpleDateFormat">
  <constructor-arg>
    <value>yyyy年MM月dd日 hh时mm分ss秒</value>
  </constructor-arg>
 </bean>
   <!-- scope="prototype"多例每执行一次创建一个对象 -->
   <bean name="st" class="com.entity.Student" scope="prototype">
   <constructor-arg name="id" value="1"/>
   <constructor-arg name="name" value="张三"/>
   </bean>
   <bean name="st2" class="com.entity.Student">
   <constructor-arg name="id" value="2"/>
   <constructor-arg name="name" value="李四"/>
   </bean>
</beans>

第三步在项目src目录下创建com.entity包,新建实体类Student

package com.entity;

public class Student {
  private int id;
  private String name;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Student(int id, String name) {
    super();
    this.id = id;
    this.name = name;
}
public Student() {
    super();
    // TODO Auto-generated constructor stub
}

}

第四步在项目src目录下创建com.test包,新建测试类Dmeo

package com.test;

import java.text.SimpleDateFormat;
import java.util.Date;

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

import com.entity.Student;

public class Demo {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
        Date d=(Date) ctx.getBean("d");
        System.out.println(d.toLocaleString());
        System.out.println("====================");

        SimpleDateFormat s=(SimpleDateFormat) ctx.getBean("df");
        System.out.println(s.format(d));
        System.out.println("======================");
        SimpleDateFormat ss=(SimpleDateFormat) ctx.getBean("dd");
        System.out.println(ss.format(d));
        System.out.println("----------------------------------");
        Student st=(Student) ctx.getBean("st");
        System.out.println(st.getName());

        Student stt=(Student) ctx.getBean("st2");
        //多例开启,对象不等,每执行一次新建一个对象
        System.out.println(st==stt);
        System.out.println(stt.getName());
        System.out.println("============");
    }
}
第五步:运行java项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值