Spring环境搭建

spring 模块 完全体 21个jar包

Spring 第一大核心思想:IOC反转/DI依赖注入

需求:使用Spring完成解耦合

步骤:一.搭建环境

1.1添加坐标

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.20</version>
    </dependency>
</dependencies>

1.2.创建主配置文件   (beans.xml)

二、编写代码

2.1创建需要管理的类:dao、service、controller

2.2使用springIOC容器概念,降需要spring管理的类,注入spring容器 还可以注入系统类:日期

具体实现:一个类配置一个标签

<!--注入bean的语法:
<bean id="注入容器的唯一标识" class="需要注入类的完全限定类名" 属性名="属性值"></bean>
-->
<!--注入dao-->
<bean id="dao" class="com.apeosurce.dao.UserDaoImp"></bean>

<!--注入service-->
<bean id="service" class="com.apeosurce.service.UserServiceImp"></bean>

<!--注入controller-->
<bean id="controller" class="com.apeosurce.controller.UserControllerImp"></bean>

2.3获取spring容器对象

spring容器核心对象:

ApplicationContext:子接口,默认bean管理方式:单例

BeanFactory:父接口,默认bean管理方式:多例

ClassPathXmlApplicationContext=======>通过xml的“相对路径”获取spring容器

FileSystemXmlApplicationContext======>通过xml的“绝对路径”获取spring容器

AnnotationConfigApplicationContext===>加载"配置类"

//spring配置文件名
ApplicationContext application =new ClassPathXmlApplicationContext("beans.xml")
Objiect obj=applicationContext.getBean("id名");

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值