如何开启spring框架以注解形式的配置

步骤

  • 导包(新版本需要导入spring-aop-4.3.17.RELEASE.jar)
  • 为配置文件applicationContext.xml引入新的命名空间(约束)
  • 开启使用注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd ">

    <!-- 指定扫描com.fei.bean包下的所有类中的注解  -->
    <context:component-scan base-package="com.fei.bean"></context:component-scan>
    
</beans>

注意:会扫描指定包下的所有子孙包

  • 在类中使用注解完成配置
具体注解类型(将对象注册到容器),写在类前面的
@Component("user")
// 上面的注解配置相当于xml文件中的<bean name="user" class="com.fei.bean.User"></bean>
    @Service("user")    // service层
    @Controller("user") // web层
    @Repository("user") // dao层
上面三种方式的效果都是一样的,都是将对象注册到容器,只是用不同的名字用于区分对象属于不同的层

转载于:https://www.cnblogs.com/zxfei/p/11178685.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值