spring中IOC常用注解以及注解依赖配置
spring中使用注解时,首先需要配置xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="项目路径"/>
</beans>
常用注解
存入spring容器中的注解:
@Component 把当前类存入spring容器中
@Controller 与@Component效果相同,用于表现层
@Service与@Component效果相同,用于业务层
@Repository与@Component效果相同,用于持久层
用于注入数据的注解:
@Autowired自动按照类型注入数据,出现位置可以变量上,也可以是方法上
用于改变作用范围的注解:
@Scope用于指定Bean的作用范围,属性:value用于指定范围的取值常用取值singleton ,prototype,未加注解默认为单例既Singleton ,prototype为多例。
和生命周期相关的注解:
@PreDestroy用于指定销毁方法
@PastConstruct用于指定初始化方法
Spring IOC注解详解:配置与常用技术应用
本文详细介绍了Spring框架中常用的组件扫描注解@Component、@Controller、@Service、@Repository,以及数据注入注解@Autowired、作用范围注解@Scope和生命周期管理注解@PreDestroy和@PostConstruct。同时,指导如何在XML配置文件中设置扫描包路径。
474

被折叠的 条评论
为什么被折叠?



