spring框架——springIOC

此系列将从四个方面讲述spring框架的一些学习经验:
springIoC、spring的注解开发、spring的AOP、spring的事务管理

	今天讲述springIoc的学习经历(基于idea开发工具,使用maven项目)

一、springIOC

1. 概述: 让spring帮我们管理对象(实体类 os:并不单指)的创建
2. 流程:
①、配置需要被创建的类
②、spring读取配置文件、根据配置文件创建对象
③、获取并使用对象
3. 具体步骤:
、导入pom依赖

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency>

②、编写配置文件:

<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>

③、创建spring的核心容器对象,获取bean`

// 加载配置文件,创建spring核心容器 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取bean // UserService userService = (UserService) ac.getBean("userService2"); // (通过id获取) // UserService userService = ac.getBean(UserService.class); //(通过类型获取) UserService userService = ac.getBean("userService2", UserService.class); // 通过ID和类型共同获取 // 使用bean userService.save();

4. bean属性的配置:
①、id:起一个名字,每个bean的id值不同,可以通过id值获取对象
②、name:起了个别名,也可以通过别名去获取对象
③、class:指定类型,必须是全类名(包名+类名)
④、scope:
1. singleton(默认值)(1. 一个bean只有一个对象(单例)2. 容器创建后直接会把bean也创建出来)
2. prototype(1.一个bean可以有多个对象 2. 当获取bean的时候,才创建bean的对象).
⑤destroy-method=“方法名”:
当对象被销毁后,执行对应的方法(注意:要想销毁方法执行,需要指定为单例,并且需要容器调用close。
⑥、init-method=“方法名”:当对象被创建后,执行对应的方法
⑦、静态工厂创建bean:<bean class="静态工厂的全类名" factory-method="获取对象的静态方法"/>
⑧、实力工厂创建bean:<bean id="实例工厂的id" class="实例工厂的全类名"/> <bean factory-bean="实例工厂的id" factory-method="获取对象的方法"/>

二、Spring-DI

1.概述: 依赖注入(给bean的属性赋值)
2.注入方式
方式一:set方式注入( 提供成员变量对应的set方法)在bean中定义子标签

普通类型:<property name="属性名" value="属性值">
引用类型:<property name="属性名" ref="其他bean的id">
注意:普通类型是指基本类型+包装类+String,用value赋值。其他的都用ref引用bean

方式二:构造方法注入(提供对应的构造方法)在bean中定义子标签

<constructor-arg name="构造方法参数名" ref="其他bean的id"/>
总结:在配置的时候与方式1的区别就是标签名由property改为了constructor-arg ,其他都一样

三、spring整合mybatis

1.导入pom依赖

  <!-- 数据库连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>${druid.version}</version>
    </dependency>
 <!-- MySQL相关包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
   <!-- 添加 mybatis 依赖包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis.spring.version}</version>
    </dependency>
        <!-- Spring相关包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

2.编写基本的三层架构代码:
①、dao层
②、service接口和实现类
③主程序、测试service

3.编写配置文件

<!--spring整合mybatis-->
<!--产生连接对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.itheima.domain"/>
</bean>
<!--映射扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.itheima.dao"/>
</bean>

四、properties配置文件加载

1.引入context名称空间

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

2.通过标签加载配置文件

<context:property-placeholder location="classpath:jdbc.properties"/>

3、通过${键}配置文件名

<property name="username" value="${jdbc.username}"/>

4、导入其他配置文件

<import resource="classpath:配置文件名"/>

附上springIOC的一个入门小案例

applicationContext.xml配置文件

<?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">
    <!-- 1.创建spring控制的资源-->
    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
</beans>

UserService文件

public interface UserService {
    public void save();
}

UserServiceImpl文件

public class UserServiceImpl implements UserService {
    public void save() {
        System.out.println("user service running...");
    }
}

Test类

public class UserApp {
    public static void main(String[] args) {
        //2.加载配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //3.获取资源
        UserService userService = (UserService) ctx.getBean("userService");
        userService.save();
    }
}

以上就是今天springIOC的学习分享啦!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值