八、IOC 操作 Bean 管理(基于注解方式)创建对象

1. 什么是注解

  1. 注解是代码总特殊的标记,格式:@注解名称(属性名称=属性值)
  2. 使用注解,注解作用在类上面,方法上面,属性上面
  3. 使用注解目的:简化xml配置

2. Spring 针对 Bean 管理中创建对象提供注解

  1. @Component
  2. @Service
  3. @Controller
  4. @Repository

* 上面的四个注解功能是一样的,都可以用来创建Bean实例

3. 基于注解方式实现对象的创建

  1. 引入依赖,引入aop的依赖
  2. 开启组件的扫描
<?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
">


    <!-- 开启组件扫描 -->
    <!--  1. 如果扫描多个包,使用逗号隔开-->
    <!--  2. 扫描包的上层目录-->
    <context:component-scan base-package="com.study.modules.annotate" ></context:component-scan>
</beans>
package com.study.modules.annotate.service;


import org.springframework.stereotype.Component;


/**
* 在注解里面 value 属性值可以省略不写
* 默认值是类名称,首字母小写.
*/
@Component(value = "userService")
public class UserService {


    public void add() {
        System.out.println("service add ……");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值