Spring 不同类型参数(字符串、数组、Bean对象、list、map、set、空字符串、properties)的注入方式(IDEA)

    科技2022-07-16  99

    Spring 不同类型参数的注入方式(IDEA)

    对应的pojo中的实体类: import com.sun.javafx.collections.MappingChange; import org.springframework.core.annotation.MergedAnnotationPredicates; import java.util.*; public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private String wife; private Properties properties; public String getName() { return name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public void setName(String name) { this.name = name; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobbys() { return hobbys; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", properties=" + properties + '}'; } } 在beans.xml中对应的注入方式如下: <?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-3.0.xsd"> <bean id="address" class="com.whut.pojo.Address"> <property name="address" value="武汉"/> </bean> <bean id="student.name" class="com.whut.pojo.Student"> <!--普通值注入--> <property name="name" value="marks_lee"/> <!--引用bean注入--> <property name="address" ref="address"/> <!--数组注入--> <property name="books"> <array> <value>Chinese</value> <value>English</value> </array> </property> <!--set注入--> <property name="games"> <set> <value>lol</value> <value>bob</value> </set> </property> <!--list注入--> <property name="hobbys"> <list> <value>basketball</value> <value>baseball</value> </list> </property> <!--空字符串注入--> <property name="wife"> <null/> </property> <!--map注入--> <property name="card"> <map> <entry key="身份证" value="23456789"/> <entry key="性别" value=""/> </map> </property> <!--properties注入--> <property name="properties"> <props> <prop key="driver">com.mysql.jdbc.Driver</prop> <prop key="url">jdbc:mysql:localhost://3306</prop> </props> </property> </bean> </beans>
    Processed: 0.010, SQL: 8