文章目录
一、注解介绍
本质:类
用法:@注解名称
作用:使用注解取代xml配置文件
xml配置:
<context:component-scan base-packag=" "></<context:component-scan>
二、常见注解
1、@Component
@Component 取代<bean class=" ">
@Component(“id”)取代<bean id=" " class=" ">
2、@Component衍生注解(web开发)
@Repository:dao层
@Service:service层
@Controller:web层
作用 :取代<bean class=" ">
(同@Component` )
3、依赖注入
3.1 普通值:
@Value(" ")
3.2 引用值:
1.按【类型】注入
@Autowired
2.按【名称】注入
(1) 方式一:
@Autowired
@Qualifier(“名称”)
(2) 方式二:
@Resource(“名称”)
4、生命周期
初始化:@PostConstruct
销毁:@PreDestroy
5、作用域
默认单例模式;
多例:@Scpoe(“prototype”)
6、注解和xml文件混合使用
1.将所有的bean配置到xml文件中
2.xml中配置:
<context:annotation-config></context:annotation-config>