package com.hanqi; import java.util.*; import com.hanqi.User; public class HelloWorld { public HelloWorld() { } public HelloWorld(String name) { this.name = name; } private User user; private String name; public String getName() { return name; } public void setName(String name) { System.out.println("设置name = " + name); this.name = name; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public void sayHello() { System.out.println("Hello " + name + " " + user); } private List<User> userlist; public List<User> getUserlist() { return userlist; } public void setUserlist(List<User> userlist) { this.userlist = userlist; } }
HelloWorld
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- autowire="byName" bean的id和要装配的属性名一致,根据属性名进行自动装配 --> <!-- <bean id="helloWorld" autowire="byName" class="com.hanqi.HelloWorld"> --> <!-- autowire="byType" 根据bean的类型进行自动匹配的,要求同一类型的bean只能被定义一个 --> <!-- <bean id="helloWorld" autowire="byType" class="com.hanqi.HelloWorld"> --> <bean id="helloWorld" scope="prototype" class="com.hanqi.HelloWorld" p:user-ref="user2"> <property name="name" value="测试1"></property> <property name="userlist"> <list > <ref bean="user1" /> </list> </property> </bean> <bean id="user1" class="com.hanqi.User"> <property name="name" value="测试3"></property> <property name="age" value="20"></property> <property name="sex" value="男"></property> </bean> <bean id="user2" class="com.hanqi.User" p:name="测试4" p:age="30" p:sex="男"> </bean> </beans>
applicationContext.xml
package com.hanqi; public class User { public User() { } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } public User(String name, int age, String sex) { super(); this.name = name; this.age = age; this.sex = sex; } public User(String name, String sex, int age) { super(); this.name = name; this.age = age; this.sex = sex; } private String name; private int age; private String sex; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
User
package com.hanqi; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSp { public static void main(String[] args) { //构建容器 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld hw = (HelloWorld)ac.getBean("helloWorld"); hw.sayHello(); HelloWorld hw1 = (HelloWorld)ac.getBean("helloWorld"); System.out.println(hw == hw1); } }
TestSp
时间: 2024-11-07 03:01:55