Spring快速入门

1.开发步骤

(1)导入spring开发的基础jar包beans,context,core和SpEL(expression)

//默认导入核心包的一个,其他会联动导入 
<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.9</version>
        </dependency>
    </dependencies>
//junit用于测试
   <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>

(2)编写相关的Dao接口及其实现类

//Dao
public interface StudentDao {
    public abstract void self_introduction();
}
//Imp
public class StudentService implements StudentDao {
    private Student student;

    public StudentService(Student student) {
        this.student = student;
    }

    public void self_introduction() {
        System.out.println(student);
    }
}
//相关domain
package com.domain;

import java.util.Map;

public class Student {
    private String name;
    private Map<String,Integer> map;
    private int age;
    private Book book;
    public Student() {
    }

    public Student(String name, Map<String, Integer> map, int age, Book book) {
        this.name = name;
        this.map = map;
        this.age = age;
        this.book = book;
    }

    public Student(String name, Map<String, Integer> map, int age) {
        this.name = name;
        this.map = map;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, Integer> getMap() {
        return map;
    }

    public void setMap(Map<String, Integer> map) {
        this.map = map;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }
}
package com.domain;

public class Book {
    private String name;
    private int price;

    public Book() {
    }

    public Book(String name, int price) {
        this.name = name;
        this.price =price;
    }
}

  (3)编写Spring的配置文件

<?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 id="Book1" class="com.domain.Book">
    <constructor-arg name="price" value="25"/>
    <constructor-arg name="name" value="西游记"/>
</bean>
    <bean id="Student1" class="com.domain.Student">
    <property name="name" value="张三"/>
    <property name="age" value="20"/>
    <property name="book" ref="Book1"/>
        <property name="map">
            <map>
                <entry key="brithyear" value="1999"/>
                <entry key="brithmoonth" value="9"/>
                <entry key="brithday" value="9"/>
            </map>
        </property>
</bean>
    <bean name="StudentService" id="service" class="com.Dao.Imp.StudentService">
        <constructor-arg name="student" ref="Student1"/>
    </bean>
</beans>

(4)在ioc容器中获取相关类并测试

import com.Dao.Imp.StudentService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Test
    public void test1(){
        ApplicationContext app=new ClassPathXmlApplicationContext("StudentApplication.xml");
        StudentService service=(StudentService) app.getBean("service");
        service.self_introduction();
        System.out.println("***********");
        service=(StudentService)app.getBean("StudentService");
        service.self_introduction();
        System.out.println("***********");
        service=app.getBean(StudentService.class);
        service.self_introduction();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值