Spring _学习_1

Spring _学习_1

一、Spring Framwork简介

Spring 基础框架,可以视为Spring 基础设施,基本上任何其他 Spring 项目都是以 SpringFramework 为基础的。

img

Spring设计理念:Spring是面向Bean的编程

Spring 两大核心技术:
控制反转(IoC:Inversion of Control ) /依赖注入(DI:Dependency Injection )
面向切面编程(AOP:Aspect Oriented Programming)

什么是IoC (Inversion of Control) : 控制反转?

IoC (Inversion of Control) : 控制反转, 是一个理论,概念,思想。把对象的创建,赋值,管理工作都交给代码之外的容器实现, 也就是对象的创建是有其它外部资源完成,这样做实现了解耦合。

正转:对象的创建、赋值等操作交由程序员手动完成,即使用类似new Xxx(Xxx Xxx)、Xxx.setXxx()语句完成对象的创建与赋值,缺点是一旦程序功能发生改变,涉及到的类就要修改代理,耦合度高,不便于维护和管理。

反转:对象的创建、赋值等操作交由代码之外的容器实现,有容器代替程序员完成对象的创建、赋值;且当程序功能发生变化时,只需要修改容器的配置文件即可。

为什么用Spring框架?

Spring的优点
低侵入式设计
独立于各种应用服务器
依赖注入特性将组件关系透明化,降低了耦合度
面向切面编程特性允许将通用任务进行集中式处理
与第三方框架的良好整合

实现一个HelloSpring的代码:运用的是控制反转的思想

第一步:创建实体类HelloSpring

package com.sunsky.demo1;

/**
 * @className: ${大数据学习}.
 * @description: hellospring的实例
 * @Theme:
 * @author:xgh
 * @create-day: 2024-01-27 10:32
 */
public class HelloSpring {

    /**
     * 属性
     */
    private String who;

    public void print(){
        System.out.println("hello" + this.getWho()); // this值当前类
    }

    public String getWho() {
        return who;
    }

    public void setWho(String who) {
        this.who = who;
    }
}

第二步:配置applicationContext.xml

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

第三步:在applicationContext.xml中进行声明组件【运用IOC的思想】

相当于将在java程序中创建对象(例如:new XXX),放入到组件中,进行声明。把对象的创建,赋值,管理工作都交给代码之外的容器实现, 也就是对象的创建是有其它外部资源完成,这样做实现了解耦合。

    <bean id="HelloSpring" class="com.sunsky.demo1.HelloSpring">
        <property name="who">
            <value>你好,开始学习spring</value>
        </property>
    </bean>

第四步:在test包下进行“飒”一把

public class test1 {
    @Test
    public void method1(){
        // 通过ClassPathXmlApplicationContext实例化spring的上下文,得到上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       // 通过ApplicationContex的getBean()方法,根据id来获取bean的实例
        HelloSpring helloSpring = (HelloSpring) context.getBean("HelloSpring");
        // 执行print()方法
        helloSpring.print();
    }
}

实现依赖注入的思想的“小兵张嘎,杀鬼子的Demo”

第一步:创建People实体类和Talk实体类

package com.sunsky.demo1;

/**
 * @className: ${大数据学习}.
 * @description: 人的实体类
 * @Theme:
 * @author:xgh
 * @create-day: 2024-01-27 10:41
 */
public class People {

    /**
     * 名字
     */
    private String name;

    /**
     * 人说的话
     */
    private String say;

    public String getName() {
        return name;
    }

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

    public String getSay() {
        return say;
    }

    public void setSay(String say) {
        this.say = say;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", say='" + say + '\'' +
                '}';
    }
}

Talk实体类

package com.sunsky.demo1;

/**
 * @className: ${大数据学习}.
 * @description: 说话类
 * @Theme:
 * @author:xgh
 * @create-day: 2024-01-27 10:45
 */
public class Talk {
  public People people;

  public void method(){
      System.out.println(people.getName() +"说:"+people.getSay() );
  }

    public People getPeople() {
        return people;
    }

    public void setPeople(People people) {
        this.people = people;
    }

    @Override
    public String toString() {
        return "Talk{" +
                "people=" + people +
                '}';
    }
}

第二步:配置applicationContext.xml

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

第三步:在applicationContext.xml中进行声明组件【运用依赖注入 DI 的思想】

  <!--创建一个People组件-->
   <bean id="p" class="com.sunsky.demo1.People"></bean>
   <!--创建一个Talk组件-->
   <bean id="talk" class="com.sunsky.demo1.Talk">
        <!--给people属性[注入],关联引用-->
        <property name="people" ref="p"></property>
    </bean>

第四步:在test中进行创建测试类TalkTest

package com.sunsky;

import com.sunsky.demo1.People;
import com.sunsky.demo1.Talk;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @className: ${大数据学习}.
 * @description: 谈话类的测试类
 * @Theme:
 * @author:xgh
 * @create-day: 2024-01-30 20:19
 */
public class TalkTest {

    @Test
    public void method(){
        //创建上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过上下文对象调用方法,得到Talk类
        //得到people对象
        People p1 = context.getBean(People.class);
        p1.setName("小兵张嘎");
        p1.setSay("小爷我:三天不打小鬼子,手都痒痒!");

        Talk talk1 =context.getBean(Talk.class); //将对象的创建交给了容器完成
        talk1.method();
    }
}

显示结果

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

常见遇到的问题

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

解决方法

.xml文件路径不对
  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值