Bean 作用域
Scope Description
singleton (Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
Bean 的自动装配
自动装配是 Spring 满足 bean 依赖的一种方式
Spring 会在上下文中自动寻找,并自动给 bean 装配属性
Spring 中有三种装配方式
再 xml 中显式地配置
再 Java 中显式地配置
隐式的自动装配 bean
xml 中显式的配置
public class People {
private String name;
private Animal cat;
private Animal Dog;
...
public class Cat implements Animal {
@Override
public void bark() {
System.out.println("miao~");
}
}
public class Dog implements Animal {
@Override
public void bark() {
System.out.println("wang~");
}
}
<bean id="cat" class="com.youzi.pojo.Cat"/>
<bean id="dog" class="com.youzi.pojo.Dog"/>