马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
基于set方式的依赖注入
概述
依赖注入的方式分为基于构造器的注入方式和基于set的注入方式,而基于set方式的依赖注入是是依赖注入的核心,本文详细讲解多种类型的数据注入的方式,包括普通注入,bean注入,array|list|map|set|props|null
更详尽内容请查看官网
实践
地址类,用于测试bean注入- package com.kuangstudy.di;
- /**
- * 功能描述
- *
- * @since 2022-06-25
- */
- public class Address {
- private String address;
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @Override
- public String toString() {
- return "Address{" +
- "address='" + address + '\'' +
- '}';
- }
- }
复制代码 学生类,用于测试全部类型- package com.kuangstudy.di;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Set;
- /**
- * 功能描述
- *
- * @since 2022-06-25
- */
- public class Student {
- private String name;
- private Address address;
- private String[] books;
- private List<String> hobbies;
- private Map<String, String> card;
- private Set<String> games;
- private String wife;
- private Properties info;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Address getAddress() {
- return address;
- }
- public void setAddress(Address address) {
- this.address = address;
- }
- public String[] getBooks() {
- return books;
- }
- public void setBooks(String[] books) {
- this.books = books;
- }
- public List<String> getHobbies() {
- return hobbies;
- }
- public void setHobbies(List<String> hobbies) {
- this.hobbies = hobbies;
- }
- public Map<String, String> getCard() {
- return card;
- }
- public void setCard(Map<String, String> card) {
- this.card = card;
- }
- public String getWife() {
- return wife;
- }
- public void setWife(String wife) {
- this.wife = wife;
- }
- public Properties getInfo() {
- return info;
- }
- public void setInfo(Properties info) {
- this.info = info;
- }
- public Set<String> getGames() {
- return games;
- }
- public void setGames(Set<String> games) {
- this.games = games;
- }
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", address=" + address +
- ", books=" + Arrays.toString(books) +
- ", hobbies=" + hobbies +
- ", card=" + card +
- ", games=" + games +
- ", wife='" + wife + '\'' +
- ", info=" + info +
- '}';
- }
- }
复制代码 测试类,用于执行main函数测试- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.kuangstudy.di.Student;
- /**
- * 功能描述
- *
- * @since 2022-06-25
- */
- public class Test01 {
- public static void main(String[] args) {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
- Student s1 = (Student) applicationContext.getBean("s1");
- System.out.println(s1);
- }
- }
复制代码- <?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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="address1" >
- <property name="address" value="xi'an"></property>
- </bean>
- <bean id="s1" >
- <property name="name" value="xiaoming"></property>
- <property name="address" ref="address1"></property>
- <property name="books">
- <array>
- <value>西游记</value>
- <value>红楼梦</value>
- <value>三国演义</value>
- <value>水浒传</value>
- </array>
- </property>
- <property name="hobbies">
- <list>
- <value>swimming</value>
- <value>painting</value>
- <value>playing basketball</value>
- </list>
- </property>
- <property name="card">
- <map>
- <entry key="Student card" value="000010"></entry>
- <entry key="Id card" value="xxxxxxxxxxxx"></entry>
- <entry key="Bank card" value="xxxxxxxxxxxxxxxx"></entry>
- </map>
- </property>
- <property name="games">
- <set>
- <value>LOL</value>
- <value>COC</value>
- <value>BOB</value>
- </set>
- </property>
- <property name="wife">
- <null></null>
- </property>
- <property name="info">
- <props>
- <prop key="username">root</prop>
- <prop key="password">123</prop>
- </props>
- </property>
- </bean>
- </beans>
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |