Spring框架速通笔记

理解两个概念

  • 依赖注入:对象依赖框架进行注入(注解)(不需要写代码声明,但是在xml文件中约定好)(针对应用程序来说)
  • 控制反转:被创建的对象的控制权移交给框架(针对对象来说)

明白一个问题

为什么要用Spring?spring的好处是?

业务发生变动时不需要去修改代码,只需要去修改各类配置文件即可(给代码解耦合)

代码示例1-通过配置文件声明对象

在这里插入图片描述在这里插入图片描述

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Master;
public class Springtest {
    public static void main(String[] args) {
//        传统方法
        Master testmas=new Master();
        testmas.sayhello();
        System.out.println("------------------------------------");
//        spring方法
        ApplicationContext ac=new ClassPathXmlApplicationContext("demo.xml");
        Master ms=(Master) ac.getBean("suoying");
        ms.sayhello();
        System.out.println("------------------------------------");
    }
}
代码示例2 通过配置文件进行不同类型的属性注入
  1. 基本数据类型(property标签内嵌name=“属性名” value=” 属性设定值“的语法)
  2. 引用数据类型 (property标签内嵌name=“属性名” 或或<map <entry key"…" value " …"/> />
  3. 自定义类 (property标签内嵌name=“属性名” ref=” 引用类型标记“+bean注明标记类名关系
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述
<?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="suoying" class="pojo.Master">
        <property name="name" value="jack"></property>
        <property name="age" value="18"></property>
        <property name="height" value="180"></property>
        <property name="kitty" ref="kitty"></property>
        <property name="demolis">
            <list>
                <value>123</value>
                <value>135</value>
                <value>247</value>
                <value>359</value>
            </list>
        </property>
        <property name="demoset">
            <set>
                <ref bean="kitty"/>
                <value>"kitty"</value>
                <value>"wamg"</value>
                <value>"101"</value>
            </set>
        </property>
        <property name="demomap">
            <map>
                <entry key="111" value="v1"/>
                <entry key="2222" value="v2"/>
                <entry key="2232" value="v3"/>
            </map>
        </property>
    </bean>
    <bean id="kitty" class="pojo.Pet"></bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Master;
public class Springtest {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("demo.xml");
        Master ms=(Master) ac.getBean("suoying");
        ms.sayhello();
        ms.getKitty().shout();
        for (Object demoli : ms.demolis) {
            System.out.println(demoli);
        }
        for (Object o : ms.demoset) {
            System.out.println(o);
        }
        for (Object o : ms.demomap.keySet()) {
            System.out.println(o+"____"+ms.demomap.get(o).toString());
        }
        System.out.println("------------------------------------");
    }
}
工厂模式:静态工厂(类中的方法用static修饰,无需实例化即可调用)/实例工厂(反之)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值