Spring的学习_______4.各种参数类型的依耐注入(DI)测试以及P命名空间的注入和C命名空间的注入
Posted xbfchder
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring的学习_______4.各种参数类型的依耐注入(DI)测试以及P命名空间的注入和C命名空间的注入相关的知识,希望对你有一定的参考价值。
(本案例为:Idea下的maven项目)
1.实体类的编写:
Student.java
public class Student private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> cards; private Set<String> games; private String girlF; private Properties info; public Student() public Student(String name, Address address, String[] books, List<String> hobbys, Map<String, String> cards, Set<String> games, String girlF, Properties info) this.name = name; this.address = address; this.books = books; this.hobbys = hobbys; this.cards = cards; this.games = games; this.girlF = girlF; this.info = 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> getHobbys() return hobbys; public void setHobbys(List<String> hobbys) this.hobbys = hobbys; public Map<String, String> getCards() return cards; public void setCards(Map<String, String> cards) this.cards = cards; public Set<String> getGames() return games; public void setGames(Set<String> games) this.games = games; public String getGirlF() return girlF; public void setGirlF(String girlF) this.girlF = girlF; public Properties getInfo() return info; public void setInfo(Properties info) this.info = info; @Override public String toString() return "Student" + "name=‘" + name + ‘\‘‘ + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", cards=" + cards + ", games=" + games + ", girlF=‘" + girlF + ‘\‘‘ + ", info=" + info + ‘‘;
User.java
public class User private String name; private String sex; public User() public User(String name, String sex) this.name = name; this.sex = sex; public void setName(String name) this.name = name; public void setSex(String sex) this.sex = sex; @Override public String toString() return "User" + "name=‘" + name + ‘\‘‘ + ", sex=‘" + sex + ‘\‘‘ + ‘‘;
2.src / main / resources下的applicationContext.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" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--Address--> <!-- 作用域scope: Web相关 request session singleton:单例模式 prototype: 原型 bean中不填写作用域默认, scope="singleton",单例 单例:在内存中只有一个备份 --> <bean id="addr" class="com.kuang.pojo.Address"> <property name="address" value="西安"/> </bean> <!--Student--> <bean id="student" class="com.kuang.pojo.Student"> <!--普通字段--> <property name="name" value="小明"/> <!--引用其他bean使用ref--> <property name="address" ref="addr"/> <!--数组的注入--> <property name="books"> <array> <value>西游记</value> <value>水浒传</value> <value>红楼梦</value> <value>三国演义</value> </array> </property> <!--List注入--> <property name="hobbys"> <list> <value>女孩</value> <value>代码</value> <value>电影</value> <value>音乐</value> </list> </property> <!--Map的注入--> <property name="card"> <map> <entry key="IdCard" value="666666888888884444"/> <entry key="银行卡" value="111122223333444"/> </map> </property> <!--Set注入--> <property name="games"> <set> <value>王者荣耀</value> <value>贪玩蓝月</value> <value>绝地求生</value> <value>LOL</value> </set> </property> <!--Null空值注入--> <property name="girlF"> <null/> </property> <!--Properties注入--> <property name="info"> <props> <prop key="学号">201932301</prop> <prop key="性别">男</prop> <prop key="姓名">小明</prop> </props> </property> </bean> <!--p:property属性,命名空间注入--> <bean id="user1" class="com.kuang.pojo.User" p:name="狂神" p:age="18"/> <!--c:constructor构造器:命名空间注入--> <bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="19" /> </beans>
3.测试类的编写:
public class Student @Test public void test() ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml"); com.xbf.pojo.Student student =(com.xbf.pojo.Student) context.getBean("student"); System.out.println(student.toString());
以上是关于Spring的学习_______4.各种参数类型的依耐注入(DI)测试以及P命名空间的注入和C命名空间的注入的主要内容,如果未能解决你的问题,请参考以下文章
为啥为 __eq__ 定义参数类型会引发 MyPy 类型错误?