Spring入门(一)之初识

2018-12-02  本文已影响0人  穿梭在围城

一、认识Spring

了解一个技术要先知道它是干嘛的,我的理解上Spring的本质就是一个对象容器。我们把对象交给Spring创建和管理(IOC)和在用到对象的时候由Spring帮我们从容器中取出并注入(DI)到我们使用的地方。

这两个核心技术的实现就成就了Spring的大半江山。

二、Spring项目中的jar包认识(简单)

image.png

三、Spring项目的简单搭建

image.png
package cn.demo.domain;
/**
 * 
 * @ClassName:  User   
 * @Description:TODO(定义User类)   
 * @date:   2018年12月2日 下午2:42:20   
 */
public class User {

    private String name;//姓名
    private int age;//年龄

    public User() {
        super();
    }

    public User(String name, int age) {
        super();
        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 "User [name=" + name + ", age=" + age + "]";
    }

}

<?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-4.2.xsd ">

    <bean name="user" class="cn.demo.domain.User">
        <property name="name" value="XiaoMing"></property>
        <property name="age" value="18"></property>
    </bean>

</beans>
package cn.demo.test;

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

import cn.demo.domain.User;

public class HelloSpring {
    
    @Test
    public void fun01(){
        //创建Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("application-context.xml");
        //从容器获取User对象
        User user = (User) ac.getBean("user");
        //验证
        System.out.println(user);
    }

}

[图片上传失败...(image-f96d43-1543735360043)]

四、Spring对象的创建方式(三种)

五、Spring属性注入的方式

  1. 构造方法注入
image.png
  1. set方法注入

    • 简单类型的注入

      • <property>标签注入
      image.png
      • p命名空间注入
image.png
    <!-- 复杂类型的注入 -->
    <bean name="collectionBean" class="cn.zw.domain.CollectionBean">
        <!-- 
            数组 :
                1.数组中只需要注入一个值得话,当成简单类型注入就行
                 <property name="arr" ref="car"></property>
                2.装多值的情况    
         -->
        <property name="arr">
            <array>
                <value>tom</value>
                <value>19</value>
                <ref bean="car"/>
            </array>
        </property>
        
        <!-- List
                1.只需要注入一个值得话,当成简单类型注入就行
                <property name="list" value="car"></property>
                2.装多值的情况 -->
        <property name="list">
            <list>
                <value>zhaowei</value>
                <value>19</value>
                <ref bean="car"/>
            </list>
        </property>
        
        <!-- Map -->
        <property name="map" >
            <map>
                <entry key="name" value="zhaowei"></entry>
                <entry key="car" value-ref="car"></entry>
            </map>
        </property>
        
        <!-- properties类型的注入 -->
        
        <property name="props">
            <props>
                <prop key="name">zhaowei</prop>
            </props>
        </property>
        
     </bean>
上一篇 下一篇

猜你喜欢

热点阅读