1、Spring起步练习

2019-02-26  本文已影响0人  m小萌同学

一、后端开发的概念和技术栈

1.1 什么是后端开发

什么是后端开发-知乎

1.2 Java后端技术图谱

image

二、Java EE概念

Java EE从入门到精通

三、Spring框架特点及构成

  1. 轻量级IoC容器(最重要的两个特性之一)
  2. 采用AOP编程方式(最重要的两个特性之一)
  3. 大量使用批注,简化了配置
  4. 避免重复“造轮子”,减少代码重复使用

Spring构成

四、Spring的起步练习步骤

1. 准备maven环境,并在idea中进行配置
TIM图片20190226151757.png
2. 建立项目,并添加maven支持
3. 在pom中添加SpringContext依赖
4. 编写HelloWorld类
5. 编写beans.xml文件,注入一个bean并传值
6. 编写主类,读入配置文件中的bean并调用方法
7. 观察运行结果
Student类
package com.soft1721.spring.hello;
public class student {
    private String name;
    private int age;
    private Phone phone;
    public Phone getPhone() {
        return phone;
    }
    public void setPhone(Phone phone) {

        this.phone = phone;
    }
    public student() {
    }
    public student(String name, int age) {
        this.name = name;
        this.age = 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;
    }
    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
Phone类
package com.soft1721.spring.hello;
public class Phone {
    private String brand;
    private String price;
    private String color;
    @Override
    public String toString() {
        return "Phone{" +
                "brand='" + brand + '\'' +
                ", price='" + price + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
    public Phone(String brand, String price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getBrand() {
        return brand;
    }
    public String getPrice() {
        return price;
    }
    public String getColor() {
        return color;
    }
}
StudentAPP类
package com.soft1721.spring.hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class studentAPP {
    public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
            student student = (student) context.getBean("student");
    System.out.println(student.getName()+student.getAge()+student.getPhone());
        }
}
beans.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 id="helloWorld" class="com.soft1721.spring.hello.HelloWorld"/>
    <bean id="phone" class="com.soft1721.spring.hello.Phone">
        <constructor-arg name="brand" value="huawei"/>
        <constructor-arg name="color" value="浅艾蓝"/>
        <constructor-arg name="price" value="2799"/>
    </bean>
    <bean id="student" class="com.soft1721.spring.hello.student">
        <property name="age" value="20"/>
        <property name="name" value="Tom"/>
        <property name="phone" ref="phone"/>

        <!--<constructor-arg name="name" value="Tom"/>-->
        <!--<constructor-arg name="age" value="21"/>-->
    </bean>

</beans>

五、学习建议

六、零碎知识点

上一篇 下一篇

猜你喜欢

热点阅读