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

网友投稿 366 2023-07-02

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

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

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 + '\'' + '}';

}

}

配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsdhttp://">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsdhttp://">

测试类

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 + '\'' + ", userMessocphJaqxhhage=" + 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 + '\'' + '}';

}

}

配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

测试类

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot集成RabbitMQ实现用户注册的示例代码
下一篇:Spring实战之使用ClassPathResource加载xml资源示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~