Spring Bean
Bean(Spring对象的一种称呼,用它来表示java对象)
Bean都是用接口和实现类实现的
Spring官方声明为Spring Bean的注解有以下几种:
- org.springframework.stereotype.Service
- org.springframework.stereotype.Component
- org.springframework.stereotype.Controller
- org.springframework.stereotype.Repository
只要在类上添加这些注解都可以被IOC容器加载
@Componen
t注解通用的是Bean注解,其余三个注解都是扩展自@Component
@Service
代表的是Service Bean
@Controller
作用于web Bean
@Repository
作用于持久化相关Bean
依赖注入
依赖注入让得到其他Bean(对象)的实例相当简单,只需要在类上面添加注解,如下
@Autowired
private SongService songService;
Autowired完整的类路径是
org.springframework.beans.factory.annotation.Autowired
注:前提条件就是当前的类必须是Spring Bean
Spring Bean的生命周期
在方法上面添加@PostConstruct
注解,就代表该方法在Spring Bean启动后会自动执行
@PostConstruct的完整包路径为
javax.annotation.PostConstruct