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用于指定初始化方法