Spring之IoC容器:通过xml的方式配置依赖注入

Spring之IoC容器:通过xml的方式配置依赖注入

  • 前言
  • 一、创建Bean对象
    • 1.默认构造函数创建
  • 二、依赖注入
    • 1.通过构造函数注入
    • 2.Set方法注入
  • 总结


前言

参考spring的官网:https://docs.spring.io/spring-framework/docs/5.3.10-SNAPSHOT/reference/html/core.html#spring-core
理解起来spring的IoC容器就等同于一个通过反射去创建对象的工厂类,然后按照配置的id设置对应key(id)-vlue(已实例的对象),存储到Map中,如有理解错误恳请指正。

一、创建Bean对象

使用Bean标签

1.默认构造函数创建

属性说明:
1、id:对象的引用
2、class:类全名
3、scop:作用范围,常用取值有singleton单例的,即只会在内存中创意一个唯一的对象。prototype多例的,即每次用到都会创建一个新的对象。

<bean id="userDaoImpl" class="com.mediacomm.dao.UserDaoImpl" scope="singleton"></bean>

调用

public class Client {
  public static void main(String[] args) {
    //1.获取核心容器对象
    ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
    //ApplicationContext system = new FileSystemXmlApplicationContext("G:\\day02_ioc_spring\\src\\main\\resources\\Spring.xml");
    //2.根据id获取bean对象
    UserDaoImpl userDao = context.getBean("userDaoImpl",UserDaoImpl.class);

二、依赖注入

1.通过构造函数注入

创建一个新的类UserServiceImpl :

@ToString
@Setter
@Getter
public class UserServiceImpl {
  private String username;
  private int age;
  private Date date;
  private UserDaoImpl userDao;
  private String[] arrays;
  private List<String> list;
  private Map<String,String> map;
  private Properties properties;

  public UserServiceImpl(String username , int age , Date date , UserDaoImpl userDao){
    this.username = username;
    this.age = age;
    this.date = date;
    this.userDao = userDao;
  }

配置Spring.xml文件中的bean :
1、type:用于指定对应构造函数中的某一个参数的类型
2、index:用于指定对应构造函数中的参数的位置
3、name:用于指定对应构造函数中参数的名称
4、value:用于基本数据类型和Sring类型赋值
5、ref:用于赋值其他Bean对象,需要指定出现在配置文件中的Bean

<bean id="UserDaoImpl" class="com.mediacomm.dao.UserDaoImpl" scope="singleton"></bean><bean id="now" class="java.util.Date"></bean>
<bean id="UserServiceImpl" class="com.mediacomm.service.UserServiceImpl">
	<constructor-arg type="java.lang.String" value="test"></constructor-arg>
	<constructor-arg index="1" value="112"></constructor-arg>
	<constructor-arg name="date" ref="now"></constructor-arg>
	<constructor-arg name="userDao" ref="UserDaoImpl"></constructor-arg>
	</bean>

2.Set方法注入

类UserServiceImpl中要有空参构造:

@ToString
@Setter
@Getter
public class UserServiceImpl {
  private String username;
  private int age;
  private Date date;
  private UserDaoImpl userDao;
  private String[] arrays;
  private List<String> list;
  private Map<String,String> map;
  private Properties properties;

  public UserServiceImpl(){
    System.out.println("UserServiceImpl对象创建......");
  }

配置Spring.xml文件中的bean :

<bean id="userservice" class="com.mediacomm.service.UserServiceImpl" scope="singleton">
        <property name="username" value="username"></property>
        <property name="age" value="123"></property>
        <property name="date" ref="now"></property>
        <property name="userDao" ref="UserDaoImpl"></property>
        <property name="arrays">
            <array>
                <value>aaa</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>list</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="kj" value="jf"></entry>
            </map>
        </property>
    </bean>

对于集合数据的注入,只要是同种结构类型的集合,就可以使用同种标签,数组、List、Set可用标签list、array、set。Map、Properties可用标签map、propes。


总结

使用set方法注入相对来说会灵活一点,因为可以自定义注入的数据,而使用构造函数来注入则注入的数据是必须的。

你可能感兴趣的:(spring,java,spring)