详解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 + '\'' + '}'; } }
配置文件
<?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'}}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
上一篇:Spring Boot优雅使用RocketMQ的方法实例
栏 目:Java
下一篇:SpringCloud turbine监控实现过程解析
本文地址:https://www.xiuzhanwang.com/a1/Java/8812.html
您可能感兴趣的文章
- 01-10Springboot中@Value的使用详解
- 01-10JavaWeb实现邮件发送功能
- 01-10java基于poi导出excel透视表代码实例
- 01-10基于Java验证jwt token代码实例
- 01-10springboot实现文件上传步骤解析
- 01-10springboot jta atomikos实现分布式事物管理
- 01-10mybatis分页绝对路径写法过程详解
- 01-10SpringBoot使用RabbitMQ延时队列(小白必备)
- 01-10Java搭建RabbitMq消息中间件过程详解
- 01-10如何基于SpringBoot部署外部Tomcat过程解析
阅读排行
本栏相关
- 01-10Java实现动态模拟时钟
- 01-10Springboot中@Value的使用详解
- 01-10JavaWeb实现邮件发送功能
- 01-10利用Java实现复制Excel工作表功能
- 01-10Java实现动态数字时钟
- 01-10java基于poi导出excel透视表代码实例
- 01-10java实现液晶数字字体显示当前时间
- 01-10基于Java验证jwt token代码实例
- 01-10Java动态显示当前日期和时间
- 01-10浅谈Java中真的只有值传递么
随机阅读
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10SublimeText编译C开发环境设置
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 04-02jquery与jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文