详解Spring基于xml的两种依赖注入方式

发布时间:2020-08-29 09:27:06 作者:ZackGo
来源:脚本之家 阅读:138

1)使用构造器注入bean

实体类:用户类

public class User {
  private String name;
  private UserMessage userMessage;

  public User() {
  }

  public User(String name, UserMessage userMessage) {
    this.name = name;
    this.userMessage = userMessage;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public UserMessage getUserMessage() {
    return userMessage;
  }

  public void setUserMessage(UserMessage userMessage) {
    this.userMessage = userMessage;
  }

  @Override
  public String toString() {
    return "User{" + "name='" + name + '\'' + ", userMessage=" + userMessage + '}';
  }
}

实体类:用户信息类

public class UserMessage {
  private String username;
  private String password;

  public UserMessage() {
  }

  public UserMessage(String username, String password) {
    this.username = username;
    this.password = password;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  @Override
  public String toString() {
    return "UserMessage{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}';
  }
}

配置文件

<?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
    http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- 注册UserMessage类 -->
  <bean id="userMessage" class="entity.UserMessage">
    <constructor-arg name="username" value="scott"></constructor-arg>
    <constructor-arg name="password" value="tiger"></constructor-arg>
  </bean>

  <!-- 注册User类 -->
  <bean id="user" class="entity.User">
    <constructor-arg name="name" value="tom"></constructor-arg>
    <constructor-arg name="userMessage" ref="userMessage"></constructor-arg>
  </bean>

</beans>

测试类

public class TestDI {
  @Test
  public void test() {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    User user = ac.getBean("user", User.class);
    System.out.println(user);
  }
}

测试结果

User{name='tom', userMessage=UserMessage{username='scott', password='tiger'}}

2)使用setter方法注入bean

实体类:用户类

public class User {
  private String name;
  private UserMessage userMessage;

  public User() {
  }

  public User(String name, UserMessage userMessage) {
    this.name = name;
    this.userMessage = userMessage;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public UserMessage getUserMessage() {
    return userMessage;
  }

  public void setUserMessage(UserMessage userMessage) {
    this.userMessage = userMessage;
  }

  @Override
  public String toString() {
    return "User{" + "name='" + name + '\'' + ", userMessage=" + userMessage + '}';
  }
}

实体类:用户信息类

public class UserMessage {
  private String username;
  private String password;

  public UserMessage() {
  }

  public UserMessage(String username, String password) {
    this.username = username;
    this.password = password;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  @Override
  public String toString() {
    return "UserMessage{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}';
  }
}

配置文件

<?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
    http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- 注册UserMessage类 -->
  <bean id="userMessage" class="entity.UserMessage">
    <property name="username" value="scott"></property>
    <property name="password" value="tiger"></property>
  </bean>

  <!-- 注册User类 -->
  <bean id="user" class="entity.User">
    <property name="name" value="tom"></property>
    <property name="userMessage" ref="userMessage"></property>
  </bean>

</beans>

测试类

public class TestDI {
  @Test
  public void test() {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    User user = ac.getBean("user", User.class);
    System.out.println(user);
  }
}

测试结果

User{name='tom', userMessage=UserMessage{username='scott', password='tiger'}}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. Spring基于XML方式配置事务
  2. 解析XML数据的两种方式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring xml 依赖注入

上一篇:JS实现table表格内针对某列内容进行即时搜索筛选功能

下一篇:易语言遍历和删除sqlite数据库信息方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》