Spring-装配bean基于注解

4 篇文章 0 订阅
3 篇文章 0 订阅
装配bean基于注解
一、用注解取代 xml 配置文件
  1. @Component
    @Component(“id”)取代< bean id=" " class = " ">
  2. web开发中,提供3个@Component注解衍生注解(功能一样)取代< bean class=" ">
  • @Repository,dao层
  • @Service,service层
  • @Controller,web层
  1. 依赖注入,给私有字段设置,也可以给setter方法设置
  • 方式一:按照【类型】注入;
    @Autowired
  • 方式二:按照【名称】注入1
    @Autowired
    @Qualifier(“名称”)
  • 方式三:按照【名称】注入2
    @Resource(“名称”)
  1. 生命周期
    初始化:@PostConstruct
    销毁:@PreDestroy
  2. 作用域
    @Scope(“prototype”) 多例
二、xml配置命名空间

注释使用前提,添加命名空间,让spring扫描含有注解类!!!

其中xmlns是XML namespace的缩写,命名空间详细内容可参考这里

schema命名空间分两部分

  1. 命名空间声明
    默认:xmlns="" <标签名> --> < bean >
xmlns="http://www.springframework.org/schema/beans"
<xxx></xxx>

显示:xmlns:别名="" <别名:标签名> --> < context:…>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
<xsi:xxx id="">
<context:xxx id="">
  1. 确定schema xsd文件位置
    xsi:schemaLocation=“名称 位置 名称2 位置2”
    内容都是成对的【名称 位置】
xsi:schemaLocation="http://www.springframework.org/schema/beans
       			    http://www.springframework.org/schema/beans/spring-beans.xsd">	

配置好的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">
		
		<!-- 
		<bean id="userServiceId" class="com_itheima.c_ioc.UserSerivceImp" ></bean>
		 -->
		 <!-- 组件扫描 -->
		 <context:component-scan base-package="com_itheima.g_annotation_b_web"></context:component-scan>
</beans>
三、演示过程

这里

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值