avatar

Catalog
No title

1. Spring注解开发

1.1 Spring原始注解

注解 说明
@Component 使用在类上用于实例化Bean
@Controller 使用在web层类上用于实例化Bean
@Service 使用在service层类上用于实例化Bean
@Repository 使用在dao层类上用于实例化Bean
@Autowired 使用在字段上用于根据类型依赖注入
@Qualifier 结合@Autowired使用用于根据名称进行依赖注入
@Resource 相当于@Autowired+@Qualifier,按照名称进行注入
@Value 注入普通属性
@Scope 标注Bean的作用范围
@PostConstruct 使用在方法上标注该方法是Bean的初始化方法
@PreDestroy 使用在方法上标注该方法是Bean的销毁方法

1.2 Spring的新注解

使用原始注解,无法全部替代XML文件

非自定义的Bean的配置:
加载properties文件的配置:< context:property-placeholder>
组件扫描的配置:< context:component-scan>
引入其他文件:

注解 说明
@Configuration 用于指定当前类是个 Spring配置类,当创建容器时会从该类上加载注解
@ComponentScan 用于指定Spring在初始化容器时要扫描的包。作用和在Spring的xml配置文件中<context:component-scan base package=”comitheima’/>-样
@Bean 使用在方法上,标注将该方法的返回值存储到Spring容器中
@PropertySource 用于加载properties文件中的配置
@Import 用于导入其他配置类

通过注解可以实现0配置文件开发

Author: kim yhow
Link: http://yoursite.com/2020/08/19/Spring-注解开发/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信
  • 支付寶
    支付寶