spring对bean的管理(基于XML)
bean的实例化
方式一 使用默认构造函数
<bean id="userService" class="com.itheima.serive.impl.UserServiceImpl"></bean>
方式二 使用工厂中的方法
public class InstanceFactory {
public IUserService getUserService(){
return new UserServiceImpl();
}
}
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="userService" factory-bean="instanceFactory" factory-method="getUserService"></bean>
方式三 使用工厂中的静态方法
public class StaticFactory {
public static IUserService getUserService(){
return new UserServiceImpl();
}
}
<bean id="userService" class="com.itheima.factory.StaticFactory" factory-method="getUserService"></bean>
bean的作用域
bean标签的scope属性
singleton:单例的(默认值)
prototype:多例的
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
bean的生命周期
单例对象
出生:当容器创建时对象出生
活着:只要容器还在,对象一直活着
死亡:容器销毁,对象消亡
总结:单例对象的生命周期和容器相同
多例对象
出生:当我们使用对象时spring框架为我们创建
活着:对象只要是在使用过程中就一直活着。
死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
bean的依赖注入
构造函数注入
public class User {
private Integer id;
private String username;
private Date birthday;
public User(Integer id, String username, Date birthday) {
this.id = id;
this.username = username;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public Date getBirthday() {
return birthday;
}
}
<bean id="user" class="com.itheima.pojo.User">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="username" value="jack"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
set方法注入
public class User {
private Integer id;
private String username;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
<bean id="user" class="com.itheima.pojo.User">
<property name="id" value="1"></property>
<property name="username" value="jack"></property>
<property name="birthday" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>
注入集合数据
package com.itheima.utils;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionInjection {
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties props;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
}
<bean id="collectionInjection" class="com.itheima.utils.CollectionInjection">
<!--给List注入数据(基本类型)-->
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<!--给List注入数据(引用类型)-->
<property name="list">
<list>
<ref bean="user1"></ref>
<ref bean="user2"></ref>
<ref bean="user3"></ref>
</list>
</property>
<!--给Set注入数据(基本类型)-->
<property name="set">
<set>
<value>a</value>
<value>b</value>
<value>c</value>
</set>
</property>
<!--给Set注入数据(引用类型)-->
<property name="set">
<set>
<ref bean="user1"></ref>
<ref bean="user2"></ref>
<ref bean="user3"></ref>
</set>
</property>
<!--给Map注入数据(基本类型)-->
<property name="map">
<map>
<entry key="red" value="红"></entry>
<entry key="green" value="绿"></entry>
<entry key="blue"><value>蓝</value></entry>
</map>
</property>
<!--给Map注入数据(引用类型)-->
<property name="map">
<map>
<entry key="u1" value-ref="user1"></entry>
<entry key="u2" value-ref="user2"></entry>
<entry key="u3" value-ref="user3"></entry>
</map>
</property>
<!--给Properties注入数据-->
<property name="props">
<props>
<prop key="small">小</prop>
<prop key="middle">中</prop>
<prop key="big">大</prop>
</props>
</property>
</bean>
spring对bean的管理(基于注解)
扫描指定包下的所有类
<context:component-scan base-package="类所属的包名"></context:component-scan>
bean的实例化
持久层
@Repository("userDao")
public class UserDaoImpl implements IUserDao {
......
}
业务层
@Service("userService")
public class UserServiceImpl implements IUserService {
......
}
控制层
@Controller
public class UserController {
......
}
其他
@Component("txManager")
public class TransactionManager {
......
}
@Repository
@Service
@Controller
的本质都是@Component
注解- 这些注解的作用:用于把当前类对象存入spring容器中。
- 属性:value用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母改小写。