之前写过了~~~~
1.复杂类型
2.真实测试对象

四个文件
Student实体类的创建:
主要是依据官方文档来建立。那个Address也是为了测试不同的类型,而创建的引入类。主体是这个Student!!!

- package com.Li.pojo;
-
- import java.util.*;
-
- //搭建多种注入方式,所以需要多种参数类型
- public class Student {
- private String name;
- private Address address;
- private String[] books;
- private List
hobbys; - private Map
card; - private Set
games; - private Properties info;
- private String wife;
-
- 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
getHobbys() { - return hobbys;
- }
-
- public void setHobbys(List
hobbys) { - this.hobbys = hobbys;
- }
-
- public Map
getCard() { - return card;
- }
-
- public void setCard(Map
card) { - this.card = card;
- }
-
- public Set
getGames() { - return games;
- }
-
- public void setGames(Set
games) { - this.games = games;
- }
-
- public Properties getInfo() {
- return info;
- }
-
- public void setInfo(Properties info) {
- this.info = info;
- }
-
- public String getWife() {
- return wife;
- }
-
- public void setWife(String wife) {
- this.wife = wife;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", address=" + address +
- ", books=" + Arrays.toString(books) +
- ", hobbys=" + hobbys +
- ", card=" + card +
- ", games=" + games +
- ", info=" + info +
- ", wife='" + wife + '\'' +
- '}';
- }
- }
Address实体类的创建:
- package com.Li.pojo;
-
- public class Address {
- private String address;
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
- }
beans.xml(最基础的)
- "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="student" class="com.Li.pojo.Student">
- <property name="name" value="李"/>
- bean>
-
-
- beans>
MyTest
- import com.Li.pojo.Student;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
- Student student = (Student) context.getBean("student");
- System.out.println(student.getName());
-
- }
- }
这些配置都没什么好说的,之前都配置过,学过。
测试
输出配置注入的名字

- "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="address" class="com.Li.pojo.Address"/>
-
-
- <bean id="student" class="com.Li.pojo.Student">
-
-
- <property name="name" value="李"/>
-
-
- <property name="address" ref="address"/>
-
-
- <property name="books">
- <array>
- <value>红楼梦value>
- <value>水浒传value>
- <value>三国演义value>
- <value>西游记value>
- array>
- property>
-
-
- <property name="hobbys">
- <list>
- <value>听歌value>
- <value>敲代码value>
- <value>打球value>
- list>
- property>
-
-
- <property name="card">
- <map>
- <entry key="身份证" value="0000000"/>
- <entry key="银行卡" value="1111111"/>
- map>
- property>
-
-
- <property name="games">
- <set>
- <value>LOLvalue>
- <value>COCvalue>
- <value>BOBvalue>
- set>
- property>
-
-
- <property name="wife">
- <null>null>
- property>
-
-
- <property name="info">
- <props>
- <prop key="driver">20220000prop>
- <prop key="url">xxxprop>
- <prop key="password">1111prop>
- <prop key="username">rootprop>
- props>
- property>
- bean>
-
-
-
- beans>
测试一下。

发现address有问题,所以给Address类里面增加一个toString方法

测试修改之后:

成功!(因为再bean里面没有设置adress的值,所以为null)

- package com.Li.pojo;
-
- public class User {
- public String name;
- public int age;
-
- public User() {
- }
-
- public User(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
- }
导入xml约束:

- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:c="http://www.springframework.org/schema/c"
测试p与c (p对应无参构造,c对应有参构造)

测试:
- @Test
- public void test2(){
- ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
-
- User user = context.getBean("user",User.class);//和强转本质一样,只不过换了一种形式
- System.out.println(user);
-
- User user2 = context.getBean("user2",User.class);
- System.out.println(user2);
- }

六种作用域:

默认单例:singleton(每一次创建的都是一个相同的bean对象)

prototype:(原型模式,每一次都是不同的bean对象)



测试之后不相等了。