Spring的IOC注解方式

配置

log4j.properties
applicationContext.xml
引入约束地址:
spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configur
ation.html
加一句:<context:component-scan />

  • 引入约束:(引入 context 的约束):
<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.xsdhttp://www.springframew
ork.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

Spring的Bean管理中的常用注解

  • @Component:组件(作用在类上)
    Spring中提供@Component的三个衍生注解:(功能目前来说是一致的)

    1. @Controller :WEB层
    2. @Service :业务层
    3. @Repository :持久层

    这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强

  • 属性注入的注解:(使用注解注入的方式,不用提供set方法)
    @value :用于注入普通类型
    @Autowired :自动装配
    默认按类型进行装配

    按名称注入:@Qualifier:强制使用名称注入
    @Resource相当于@Autowired和@Qualifier一起使用

Bean的作用范围的注解

@Scope:
singleton:单例
prototype:多例

Bean的生命周期的配置

@PostConstruct:相当于init-method
@PreDestroy :相当于destroy-method

XML和注解

XML的结构清晰,注解方式开发方便
实际开发中有一种XML和注解整合开发
Bean有XML配置,但是使用的属性使用注解注入

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值