当前位置: 首页 > news >正文

HelloSpring

HelloSpring

 

1、导包

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.19</version>
        </dependency>

2、编写代码

public class Hello {
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

 

3、编写我们的spring文件,这里命名为beans.xml

  • 使用Spring来创建对象,在Spring这些都称为Bean
  •  类型 变量名 = new 类型()
  •  Hello hello = new Hello();
  •  id = 变量名
  •  class = new 的对象
  •  property 相当于给对象中的属性设置一个值!
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--使用Spring来创建对象,在Spring这些都称为Bean

        类型 变量名 = new 类型()
        Hello hello = new Hello();

        id = 变量名
        class = new 的对象
        property 相当于给对象中的属性设置一个值!
    -->
    <bean id="hello" class="com.gt.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>

</beans>

 

4、测试

public class HelloTest {
    @Test
    public void test() {
        //解析beans.xml文件 , 生成管理相应的Bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //getBean : 参数即为spring配置文件中bean的id .
        Hello helloSpring = (Hello) context.getBean("helloSpring");
        System.out.println(helloSpring);
    }
}

思考:

(1)Hello 对象是谁创建的 ?

        答:hello 对象是由Spring创建的

(2)Hello 对象的属性是怎么设置的 ?

        答:hello 对象的属性是由Spring容器设置的

这个过程就叫控制反转:

  • 控制:谁在控制对象的创建,传统的应用程序的对象是由程序本身控制创建的,使用spring后,对象由spring来创建

  • 反转:程序本身不创建对象,而变成被动的接收对象

  • 依赖注入:就是利用set方法来进行注入的

  • IOC是一种编程思想,由主动的编程变成被动的接收

 

spring-01-ioc1

1、新增一个spring配置文件beans.xml

  • ref:引用Spring容器中创建好的对象
  • value:具体的值,基本数据类型!
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="mysqlImpl" class="com.gt.dao.UserDaoMysqlImpl"/>
    <bean id="oracleImpl" class="com.gt.dao.UserOracleImpl"/>
    <bean id="SqlserverImpl" class="com.gt.dao.UserDaoSqlserverImpl"/>


    <bean id="UserServiceImpl" class="com.gt.service.UserServiceImpl">
        <property name="userDao" ref="SqlserverImpl"/>
    </bean>

    <!--
    ref:引用Spring容器中创建好的对象
    value:具体的值,基本数据类型!

    -->

</beans>

2、测试

import com.gt.dao.UserDaoImpl;
import com.gt.dao.UserDaoMysqlImpl;
import com.gt.dao.UserDaoSqlserverImpl;
import com.gt.service.UserService;
import com.gt.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

//    @Test
//    public void getUser(){
//        UserServiceImpl service = new UserServiceImpl();
//        service.getUser();
//    }

    public static void main(String[] args) {

        用户实际调用的是业务层,dao层他们不需要接触!
//        UserServiceImpl userService = new UserServiceImpl();
//
//        ((UserServiceImpl) userService).setUserDao(new UserDaoSqlserverImpl());
//
//        userService.getUser();

//        获取ApplicationContext:拿到Spring的容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

//        容器在手,天下我有,需要什么,就直接get什么!
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");

        userServiceImpl.getUser();

    }


}

 格式:

    @Test
    public void addUser2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserServiceImpl service = (UserServiceImpl) context.getBean("userServiceImpl");
        service.addUser();
    }

  •         到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改
  •         所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !

相关文章:

  • Vite为啥如此之快
  • 从二值 Mask 获取外接矩形坐标
  • Tomcat 的本地部署及 SmartTomcat 的使用
  • Unity Shader LightMode 标签
  • linux搭建docker镜像服务
  • 解决mybatis用Map返回的字段全变大写的问题
  • 交联剂134272-64-3,MAL-NH2 HCl 在抗体的标记上面效果明显
  • mybatis第一次课
  • 全民拼购模式:社交电商与拼购新玩法
  • Linux笔记系列
  • 机器人冗余自由度优化过程中的零空间概念
  • ActiveMQ如何处理重复消息?如何保证消息的有序性?如何处理消息堆积?
  • 2022 面试必刷 461 道大厂架构面试真题汇总 + 面经 + 简历模板
  • vue中使用base64编码上传文件或者图片,以及base64编码的图片在img标签中使用
  • 企业应用选择租用云服务器还是租用物理服务器
  • SegmentFault for Android 3.0 发布
  • 分享的文章《人生如棋》
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • Angular4 模板式表单用法以及验证
  • Javascript编码规范
  • js继承的实现方法
  • SpiderData 2019年2月13日 DApp数据排行榜
  • uni-app项目数字滚动
  • 警报:线上事故之CountDownLatch的威力
  • 坑!为什么View.startAnimation不起作用?
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 嵌入式文件系统
  • 如何设计一个微型分布式架构?
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 通过npm或yarn自动生成vue组件
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 大数据全解:定义、价值及挑战
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • # .NET Framework中使用命名管道进行进程间通信
  • (C语言)fgets与fputs函数详解
  • (C语言)逆序输出字符串
  • (floyd+补集) poj 3275
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (附源码)ssm高校实验室 毕业设计 800008
  • (附源码)计算机毕业设计SSM智能化管理的仓库管理
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (每日持续更新)jdk api之FileFilter基础、应用、实战
  • (一)SpringBoot3---尚硅谷总结
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)ObjectiveC 深浅拷贝学习
  • . NET自动找可写目录
  • .NET Core 版本不支持的问题
  • .net core 控制台应用程序读取配置文件app.config
  • .Net Core缓存组件(MemoryCache)源码解析
  • .net 无限分类
  • .NET/C# 使用反射调用含 ref 或 out 参数的方法
  • .Net7 环境安装配置
  • @private @protected @public