Skip to content

Latest commit

 

History

History
128 lines (101 loc) · 3.03 KB

第一天笔记.md

File metadata and controls

128 lines (101 loc) · 3.03 KB

spring 快速开始

1、5+1个jar包

spring-core.jar
spring-beans.jar
spring-context.jar
spring-aop.jar
spring-expresion.jar

commons-logging.jar

2、创建spring配置文件

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">

</beans>

3、创建实体类(用作测试IOC效果)

package com.yingside.po;

import java.util.Date;

public class Employee {
    private int empId;
    private String empName;
    private String empTel;
    private String empEducation;
    private Date empBirthday;

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public String getEmpTel() {
        return empTel;
    }

    public void setEmpTel(String empTel) {
        this.empTel = empTel;
    }

    public String getEmpEducation() {
        return empEducation;
    }

    public void setEmpEducation(String empEducation) {
        this.empEducation = empEducation;
    }

    public Date getEmpBirthday() {
        return empBirthday;
    }

    public void setEmpBirthday(Date empBirthday) {
        this.empBirthday = empBirthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empId=" + empId +
                ", empName='" + empName + '\'' +
                ", empTel='" + empTel + '\'' +
                ", empEducation='" + empEducation + '\'' +
                ", empBirthday=" + empBirthday +
                '}';
    }
}

4、在xml文件中配置bean

<!--  Date类型属性的注入  -->
<bean id="dateFormat" class="java.text.SimpleDateFormat">
    <constructor-arg value="yyyy-MM-dd"></constructor-arg>
</bean>

<bean name="employee" class="com.yingside.po.Employee">
    <property name="empId" value="1"></property>
    <property name="empName" value="刘备"></property>
    <property name="empTel" value="13312345671"></property>
    <property name="empEducation" value="本科"></property>
    <property name="empBirthday" value="1999-10-10"></property>
        <!--Date类型属性的注入-->
<!--        <property name="empBirthday">-->
<!--            <bean factory-bean="dateFormat" factory-method="parse">-->
<!--                <constructor-arg value="1997-09-01"></constructor-arg>-->
<!--            </bean>-->
<!--        </property>-->
</bean>

5、调用

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Employee employee = (Employee)context.getBean("employee");
        System.out.println("employee = " + employee);
    }
}