项目结构
单例代码
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描entity包 -->
<context:component-scan base-package="entity" />
</beans>
People
import org.springframework.stereotype.Component;
// 相当于配置一个bean,bean默认id为首字母小写,即people
@Component
public class People {
private String name;
// 注意为Integer
private Integer age;
public People() {
System.out.println("People无参构造函数");
}
public People(String name, Integer age) {
this.name = name;
this.age = age;
System.out.println("People二参构造函数:name=" + name + " age=" + age);
}
get,set,toString函数
}
Test
import entity.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.test1();
}
public void test1() {
System.out.println("在加载xml文件时已经单例bean的对象new出来了");
String xmlPath = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
System.out.println("-------------------------------------");
System.out.println("---------people:---------");
People people = (People) applicationContext.getBean("people");
System.out.println(people);
System.out.println("---------people2:---------");
People people2 = (People) applicationContext.getBean("people");
System.out.println(people2);
System.out.println("是否是同一个对象");
System.out.println(people.equals(people2));
}
}
结果输出
在加载xml文件时已经单例bean的对象new出来了
People无参构造函数
-------------------------------------
---------people:---------
People{name='null', age=null}
---------people2:---------
People{name='null', age=null}
是否是同一个对象
true
设置bean的id
People
import org.springframework.stereotype.Component;
// 设置bean的id为peopleId
@Component(value = "peopleId")
public class People {
private String name;
// 注意为Integer
private Integer age;
public People() {
System.out.println("People无参构造函数");
}
public People(String name, Integer age) {
this.name = name;
this.age = age;
System.out.println("People二参构造函数:name=" + name + " age=" + age);
}
get,set,toString函数
}
Test
import entity.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.test1();
}
public void test1() {
System.out.println("在加载xml文件时已经单例bean的对象new出来了");
String xmlPath = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
System.out.println("-------------------------------------");
System.out.println("---------people:---------");
People people = (People) applicationContext.getBean("peopleId");// id改成了peopleId
System.out.println(people);
System.out.println("---------people2:---------");
People people2 = (People) applicationContext.getBean("peopleId");// id改成了peopleId
System.out.println(people2);
System.out.println("是否是同一个对象");
System.out.println(people.equals(people2));
}
}
结果输出
在加载xml文件时已经单例bean的对象new出来了
People无参构造函数
-------------------------------------
---------people:---------
People{name='null', age=null}
---------people2:---------
People{name='null', age=null}
是否是同一个对象
true
多例代码
添加@Scope(“prototype”)
People
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
// 相当于配置一个bean,bean默认id为首字母小写,即people
@Component
@Scope("prototype")
public class People {
private String name;
// 注意为Integer
private Integer age;
public People() {
System.out.println("People无参构造函数");
}
public People(String name, Integer age) {
this.name = name;
this.age = age;
System.out.println("People二参构造函数:name=" + name + " age=" + age);
}
get,set,toString函数
}
测试结果
在加载xml文件时已经单例bean的对象new出来了
-------------------------------------
---------people:---------
People无参构造函数
People{name='null', age=null}
---------people2:---------
People无参构造函数
People{name='null', age=null}
是否是同一个对象
false
总结
@Component作用为实现bean的注入(需要在xml设置扫描)
- @Component相当于在xml中配置:(在不设置id的情况下默认id为类名首字母小写)
<bean id=“people” class=“entity.People” > - @Component(value = “peopleId”)相当于xml配置:(将id设为value值,即peopleId)
<bean id=“peopleId” class=“entity.People” >
@Scope(“prototype”):多例