Spring IoC 自动装载 autowire:

自动装载是Spring提供的一种更加简单的方式,来完成DI,不需要手动配置property ,IoC容器会自动选择Bean玩成注入。

自动装载俩种:

  • byName ,通过属性名完成自动装载

  • byType,通过属性对应的数据类型完成自动装载

    byName

    1.创建实体类

    package com.southwind.entity;

    import lombok.Data;
    
    @Data
    public class Presson {
    private Integer Id;
    private String name;
    private Car car; }

2.在spring.xml中配置Car和Person的Bean,通过自动装载完成依赖注入

<bean id="person" class="com.southwind.entity.Presson" autowire="byName">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.southwind.entity.Car">
<constructor-arg name="num" value="1"> </constructor-arg>
<constructor-arg name="brand" value="奥迪"></constructor-arg>
</bean>

测试:

package com.southwind.test;

import com.southwind.entity.Presson;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test7 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-autowire.xml");
Presson preson =(Presson)applicationContext.getBean("person");
System.out.println(preson);
}
}

byType:

1.实体类

2.在spring.xml中配置Car和Person的Bean,通过自动装载完成依赖注入

<bean id="person" class="com.southwind.entity.Presson" autowire="byType">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.southwind.entity.Car">
<constructor-arg name="num" value="1"> </constructor-arg>
<constructor-arg name="brand" value="奥迪"></constructor-arg>
</bean>

注意:

使用byType进行自动装载时,多个会有异常。

Spring IoC基于注解的开发

SpingIoC的作用帮助开发者创建项目中所需要的Bean,同时完成Bean之间的依赖注入关系。DI

实现功能有两种方式:

  • 基于xml
  • 基于注解

    基于注解的配置有两部:

    1. 配置自动扫包



      <context:component-scan base-package="com.southwind.entity"></context:component-scan>

    2. 添加注解: @Component (默认首字母类第一个字母小写为名字)

      package com.southwind.entity;

      import lombok.Data;
      import org.springframework.stereotype.Component; @Data
      @Component
      public class Repository {
      private DataSouse dataSouse;
      }

      改名字:@Component(value = "myrepo")

      package com.southwind.entity;

      import lombok.Data;
      import org.springframework.stereotype.Component; @Data
      @Component(value = "myrepo")
      public class Repository {
      private DataSouse dataSouse;
      }

      分析注入得到类:

      String[] s= applicationContext.getBeanDefinitionNames();

      for(String name ){

      System.out.println(name);

      }

DI:

package com.southwind.entity;

import lombok.Data;
import org.springframework.stereotype.Component; @Data
@Component
public class DataSouse {
private String user;
private String password;
private String url;
private String driverName;
}

自动装载:@Autowired(默认时byType)

package com.southwind.entity;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Data
@Component
public class Repository {
@Autowired
private DataSouse dataSouse;
}

要改变为byName()配合 @Qualifier(value="DS")

@Data
@Component
public class Repository {
@Autowired
@Qualifier(value="DS")
private DataSouse dataSouse;
}

实体类中的普通的成员变量可以通过@Value进行赋值

@Data
@Component(value = "DS")
public class DataSouse {
@Value("root")
private String user;
@Value("root")
private String password;
@Value("jdbc:mysql://localhost:3306/library")
private String url;
@Value("com.mysql.cj.jdbc.Driver")
private String driverName;
}

最新文章

  1. IDEA tomcat乱码
  2. String字符串去掉最后一个&quot;,&quot;号的几种方式
  3. 20145304 Java第四周学习报告
  4. 使用javap反编译class文件
  5. SQL 比较时间大小
  6. C语言计算开方
  7. SoapUI中Groovy的实用方法
  8. extjs tablepanel 高度自适应有关问题
  9. html 中head显示 在标题栏里面的图片
  10. 常见LINQ语句学习
  11. ssh下常用操作汇总(good)
  12. ubuntu 安装Matlab 解决显示中文乱码
  13. 汽车Vin码识别——可以嵌入到手机里的新OCR识别技术
  14. oh-my-zsh配置
  15. class DELPHICLASS TObject
  16. http协议下载文件
  17. 强化学习---A3C
  18. c++ 类前置声明【转】
  19. BZOJ.3105.[CQOI2013]新Nim游戏(线性基 贪心 博弈论)
  20. ZOJ3770Ranking System 2017-04-14 12:42 52人阅读 评论(0) 收藏

热门文章

  1. Atcoder补题计划
  2. 【Devexpres】spreadsheetControl冻结行
  3. 干货 | 如何快速实现 BitSail Connector?
  4. Java 中的接口还可以这样用,你知道吗?
  5. 软件开发目录规范、python常用内置模块
  6. 微软跨平台maui开发chatgpt客户端
  7. HMS Core 3D流体仿真技术,打造移动端PC级流体动效
  8. 痞子衡嵌入式:探讨i.MXRT下FlexSPI driver实现Flash编程时对于中断支持问题
  9. Java基础篇——JUC初步
  10. [Leetcode]在排序数组中查找元素的第一个和最后一个位置