Spring IOC

本文详细介绍了Spring框架的IOC(依赖注入)原理及其在项目中的应用,包括配置文件编写、多种bean实例化方式(构造器、工厂方法)、扫描器的使用以及如何通过Spring简化组件管理和解耦。同时涵盖了静态工厂、集合类型注入和构造器注入的实例演示。
摘要由CSDN通过智能技术生成

Spring IOC

Spring 框架介绍

Spring 是众多开源java项目中的一员,基于分层的javaEE应用一站式轻量级开源框架,主要核心是
IOC(控制反转/依赖注入)与 AOP(面向切面)两大技术,实现项目在开发过程中的轻松解耦,提高项
目的开发效率。
在项目中引入 Spring 立即可以带来下面的好处 降低组件之间的耦合度,实现软件各层之间的解耦。可
以使用容器提供的众多服务,如:事务管理服务、消息服务等等。当我们使用容器管理事务时,开发人
员就不再需要手工控制事务.也不需处理复杂的事务传播。 容器提供单例模式支持,开发人员不再需要自
己编写实现代码。 容器提供了AOP技术,利用它很容易实现如权限拦截、运行期监控等功能。

Spring 框架环境搭建

  1. 建立maven项目
  2. 引入Spring 依赖
  3. 在main方法下的Resourses里添加spring.xml配置文件
  4. 写bean对象并再配置文件中添加
  5. 加载文件,并且实例化

Spring IOC 配置文件

  1. 加载配置文件

    ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    
  2. spring 多配置文件加载

    ApplicationContext ac = new
    ClassPathXmlApplicationContext("spring.xml","dao.xml");
    
  3. 通过在spring.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">
    <!--导入需要包含的资源文件-->
    <import resource="service.xml"/>
    <import resource="dao.xml"/>
    </beans>
    

Spring IOC 容器 bean对象实例化

  1. 通过构造器调用

    注意默认调用空构造器

    • 配置文件

      <!--通过构造器创建-->
          <bean id="user" class="com.yjxxt.bean.User">
              <property name="uname" value="张三"></property>
              <property name="id" value="001"></property>
          </bean>
      
    • 调用

       User user = ac.getBean("user", User.class);
              System.out.println(user);
      
  2. 静态工厂

    • 静态工厂

         public static Car getCar(){
              return new Car(100,"BMW",500000.0);
          }
      
    • 配置文件

      <!--通过静态工厂创建-->
          <bean id="car" class="com.yjxxt.factory.CarFactory" factory-method="getCar"></bean>
      
    • 调用

      Car car = ac.getBean("car", Car.class);
              System.out.println(car);
      
  3. 实例化工厂实例化

    • 非静态工厂

       public Car getCar(){
              return new Car(101,"BMW",500000.0);
          }
      
    • 配置文件

       <bean id="car" class="com.yjxxt.factory.CarFactory" factory-method="getCar"></bean>
      
    • 调用

      Car car02 = ac.getBean("car02", Car.class);
              System.out.println(car02);
      

Spring IOC 注入

set注入

  1. javabean 注入

    • 属性字段必须需要提供set方法

        <bean id="user" class="com.yjxxt.bean.User">
              <property name="uname" value="张三"></property>
              <property name="id" value="001"></property>
          </bean>
      

      如上User类里面uname,id的注入必须有相对应得方法

  2. 集合类型注入

    • bean对象

      private Integer eid;
          private String ename;
          private Double salary;
          private Dept dept;
          private Date birth;
          private String[] hobby;
          private List<String> elist;
          private Set<String> eset;
          private Map<String,Object> emap;
          private Properties properties;
      
    • 配置文件

       <bean id="Emp" class="com.yjxxt.bean.Emp">
              <property name="eid" value="001"></property>
              <property name="salary" value="1500"></property>
              <property name="ename" value="张三"></property>
              <property name="elist">
                  <list>
                      <value>上海</value>
                      <value>北京</value>
                      <value>广州</value>
                  </list>
              </property>
              <property name="hobby">
                  <array>
                  <value>吸烟</value>
                  <value>喝酒</value>
                  <value>烫头</value>
                  </array>
              </property>
              <property name="eset">
                  <set>
                      <value>语文</value>
                      <value>数学</value>
                      <value>英语</value>
                  </set>
              </property>
              <property name="emap">
                  <map>
                      <entry key="黄伟" value="18"></entry>
                      <entry key="张斌" value="18"></entry>
                      <entry key="知道" value="18"></entry>
                  </map>
              </property>
              <property name="properties">
                  <props>
                      <prop key="driver">com.yjxxt.driver</prop>
                      <prop key="url">url</prop>
                      <prop key="username">root</prop>
                      <prop key="password">root</prop>
                  </props>
              </property>
              <property name="birth" ref="date"></property>
              <property name="dept" ref="dept"></property>
          </bean>
          <bean id="date" class="java.util.Date">
              <property name="year" value="2021"></property>
              <property name="month" value="10"></property>
              <property name="date" value="9"></property>
          </bean>
          <bean id="dept" class="com.yjxxt.bean.Dept">
              <property name="did" value="100"></property>
              <property name="dname" value="研发部"></property>
          </bean>
      

构造器注入

单个构造器注入

  • bean对象

    public class Student {
        private int sid;
        private String sname;
        private Car car;
    
     public Car getCar(String brand){
            Car car =null ;
            if ("QQ".equals(brand)){
                return new Car(103,brand,60000);
            }
            if ("FLL".equals(brand)){
                return new Car(104,brand,888888888);
            }
            car = new Car();
            return car;
        }
    
  • 配置文件

    <bean id="factory" class="com.yjxxt.factory.CarFactory02"></bean>
        <bean id="car" factory-bean="factory" factory-method="getCar">
            <constructor-arg type="java.lang.String" value="QQ"></constructor-arg>
        </bean>
        <bean id="stu" class="com.yjxxt.bean.Student">
            <property name="sname" value="张三"></property>
            <property name="sid" value="100"></property>
            <property name="car" ref="car">
            </property>
        </bean>
    

Spring扫描器

  1. 介绍

​ 实际的开发中,bean的数量非常多,采用手动配置bean的方式已无法满足生产需要,Spring这时候
同样提供了扫描的方式,对扫描到的bean对象统一进行管理,简化开发配置,提高开发效率。

  1. 扫描器的配置

    Spring IOC 扫描器
    作用:bean对象统一进行管理,简化开发配置,提高开发效率
    1、设置自动化扫描的范围
    如果bean对象未在指定包范围,即使声明了注解,也无法实例化
    2、使用指定的注解(声明在类级别) bean对象的id属性默认是 类的首字母小写
    Dao层:
    @Repository
    Service层:
    @Service
    Controller层:
    @Controller
    任意类:
    @Component
    注:开发过程中建议按照指定规则声明注解
    
  2. 设置自动扫描范围(配置文件中)

    <!-- 设置自动化扫描的范围 -->
    <context:component-scan base-package="com.yjxxt"/>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值