pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springHello</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
MyTest.java
package com.Cai.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.getString());
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = (User) context.getBean("user");
User user1 = context.getBean("user2", User.class);
System.out.println(user);
System.out.println(user1);
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="beans.xml"/>
<import resource="userbeans.xml"/>
</beans>
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.xsd">
<bean id="hello" class="com.Cai.pojo.Hello">
<property name="string" value="Spring"/>
</bean>
<bean id="address" class="com.Cai.pojo.Address">
<property name="address" value="厦门"/>
</bean>
<bean id="student" class="com.Cai.pojo.Student">
<property name="name" value="cwj"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>asdf</value>
<value>qwer</value>
<value>ferjoifjsda</value>
</array>
</property>
<property name="hobbys">
<list>
<value>asdf</value>
<value>gjirjg</value>
<value>gutjgkd</value>
</list>
</property>
<property name="cards">
<map>
<entry key="a" value="f"/>
<entry key="s" value="g"/>
</map>
</property>
<property name="games">
<set>
<value>aaa</value>
<value>ititih</value>
<value>gjjgj</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="学号">1901030077</prop>
</props>
</property>
</bean>
</beans>
userbeans.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">
<bean id="user" class="com.Cai.pojo.User" p:name="ccc" p:age="12"/>
<bean id="user2" class="com.Cai.pojo.User" c:name="c22" c:age="22" scope="prototype"/>
</beans>
Address.java
package com.Cai.pojo;
import lombok.Data;
@Data
public class Address {
private Address address;
}
Hello.java
package com.Cai.pojo;
import lombok.Data;
@Data
public class Hello {
private String string;
}
Student.java
package com.Cai.pojo;
import lombok.Data;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Data
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 wife;
private Properties info;
}
User.java
package com.Cai.pojo;
import lombok.Data;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Data
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 wife;
private Properties info;
}