第4.38课 上课 bean的生命周期, Spring的零配置

4_38

bean的生命周期

bean的生命周期:

singleton bean必须一直处于容器的管理之下.

 

因此,对于singleton bean容器可以去定义bean的生命周期行为:

- bean初始化之后:

a.通过init-method属性指定初始化方法.

b.让该bean实现InitializingBean,实现该接口之后,该类实现的的接口中的afterPropertiesSet()将会作为生命周期方法.

如果两种初始化方法同时存在时,接口中指定的方法会优先执行.

 

 

- bean销毁之前:

a.通过destroy-method属性指定销毁之前的方法.

b.让该bean实现DisposableBean,实现该接口之后,该类实现的接口中的destroy()将会作为生命周期方法.

 

对于singleton bean而言,必须等到容器死的时候singleton bean才会死.

 实现InitializingBean接口

 init-method指定初始化方法

 实现DisposableBean接口

 destroy-method指定销毁方法

 销毁

(实现DisposableBean接口destroy-method指定销毁方法)

 初始化

(实现InitializingBean接口init-method指定初始化方法)

Spring的零配置

以前XML很流行,几乎所有Java框架都要用XML作为配置文件.后来随着注解的流行,几乎所有Java框架又都改为支持注解.

所谓零配置,就是用Annotation代替原来的XML,其本质完全一样.

使用零配置的2个步骤:

- 在Spring的配置文件中定义搜索包.

- 使用注解修饰Spring容器的Bean即可.

 

@Component、@Controller、@Service、@Repository: 所有Spring的Bean都可使用该注解修饰,

如果不指定id,默认使用类名的首字母小写作为id.

 <context:component-scan .../>开启组件扫描

<!-- 开启组件扫描,它会到基础包下扫描用注解@Component、@Service、

@Repository、@Controller注解的bean,并交由Spring容器管理 -->

<context:component-scan base-package="com.yayadou.spring"/>

 完整的命名空间

<?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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

 

 

</beans>

 @Component普通组件

普通组件

@Component(value="emailSender")

public class EmailSender{

 

}

 @Controller控制器组件(SpringMVC才会用到)

 @Service业务组件

业务层组件

@Service(value="userService")

public class UserService {

 

}

 @Repository仓储(数据访问)

数据访问层组件

@Repository("userDao")

public class UserDao {

 

}

  @Scope指定Bean的作用域

@Scope: 指定Bean的作用域,与bean元素scope属性相同.

 

@Scope(value="singleton") // singleton、prototype、request、session

public class EmailSender{

 

}

 @Lazy延迟初始化

@Lazy:指定延迟初始化.相当于lazy-init属性.

 

@Lazy(true)

public class EmailSender{

 

}

 @PostConstruct与@PreDestroy

@PostConstruct和@PreDestroy

相当于init-method属性和destroy-method属性.

 

 

 

@PostConstruct()

public void init(){

System.out.println("====初始化====");

}

 

@PreDestroy()

public void destroy(){

System.out.println("======销毁释放资源======");

}

 @Resource配置依赖注入

@Resource: 配置依赖注入,

它直接支持field注入(无需setter方法,Spring直接对Field赋值).

不配置name属性就是按byType,配置name属性就是按byName

@Resource(name="userDao")

private UserDao userDao;

 @Autowired与@Qualifier

@Autowired与@Qualifier:自动装配.

@Autowired默认是byType的自动装配.

@Qualifier可指定byName的自动装配.

 

 

// @Autowired默认是byType的自动装配.

@Autowired(required=true)

// @Qualifier可指定byName的自动装配.

@Qualifier("emailSender")

private EmailSender emailSender;

posted on 2018-01-31 22:31  東風★破  阅读(138)  评论(0)    收藏  举报

导航