RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
详解Spring基于xml的两种依赖注入方式

1)使用构造器注入bean

成都创新互联主营东丰网站建设的网络公司,主营网站建设方案,成都App制作,东丰h5小程序制作搭建,东丰网站营销推广欢迎东丰等地区企业咨询

实体类:用户类

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"?>


  
  
    
    
  

  
  
    
    
  



测试类

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"?>


  
  
    
    
  

  
  
    
    
  



测试类

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'}}

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


文章标题:详解Spring基于xml的两种依赖注入方式
新闻来源:http://lswzjz.com/article/pdehsh.html