package com.shuwen.user;
public class User {
int age;
String name;
public User() {
System.out.println("无参构造");
}
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import com.shuwen.user.User;
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");
User user = context.getBean("user", User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user.hashCode());
System.out.println(user2.hashCode());
System.out.println(user==user2);
}
}
<?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="user" class="com.shuwen.user.User" scope="singleton">
<property name="name" value="shuwen"/>
<property name="age" value="21"/>
</bean>
<bean id="user2" class="com.shuwen.user.User" scope="singleton">
<property name="name" value="shuwen"/>
<property name="age" value="21"/>
</bean>
</beans>
输出信息:
无参构造
无参构造
1852584274
1354011814
false