作者:吴伟琬63526 | 来源:互联网 | 2023-10-16 12:19
我猜想开发中大多都用注解,因为简单吗,哈哈 正题: 注解:就是一个类,使用@注解名称。 开发中:使用注解 取代 xml配置文件。(谁说不是呢,专门为懒人准备的)。
1. @Component 取代class="" >@Component ("id" ) 取代 "" class ="" >2. web开发,提供3 个@Component 注解衍生注解(功能一样)取代class="" >@Repository :dao层@Service :service层@Controller :web层3. 依赖注入 ,给私有字段设置,也可以给setter方法设置普通值:@Value ("" )引用值:方式1 :按照【类型】注入@Autowired 方式2 :按照【名称】注入1 @Autowired @Qualifier ("名称" )方式3 :按照【名称】注入2 @Resource ("名称" )4. 生命周期初始化:@PostConstruct 销毁:@PreDestroy 5. 作用域@Scope ("prototype" ) 多例
注解使用前提,添加命名空间,让spring扫描含有注解类
<beans xmlns &#61;"http://www.springframework.org/schema/beans" xmlns:xsi &#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns:context &#61;"http://www.springframework.org/schema/context" xsi:schemaLocation &#61;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <context:component-scan base-package &#61;"com.fly.pig" > context:component-scan >beans >
下面是记录我使用注解的时候遇到的问题&#xff1a; 1.
2.