IOC操作Bean管理(基于注解方式)

17 篇文章 0 订阅

1.什么是注解
  1. 注解是代码的特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值。。。)
  2. 使用注解,注解作用在类上面,方法声明,属性上面
  3. 使用注解的目的:简化xml配置,使用更优雅、更简洁的方式实现功能
2.Spring针对Bean管理中创建对象提供的注解
  1. @Component (建议普通的组件)
  2. @Service(建议用在Service层)
  3. @Controller(建议用在Web层)
  4. @Repository(建议用在持久层)
    以上4个注解功能都是一样的,都可以用来创建bean实例
3.基于注解方式实现对象创建
  1. 引入依赖

    • jar包:
      在这里插入图片描述

    • maven

       <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-aop</artifactId>
              <version>5.0.7.RELEASE</version>
          </dependency>
      
  2. 开启组件扫描

    • 在配置文件中引入centext文件空间
      在这里插入图片描述

      <?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:p="http://www.springframework.org/schema/p"
             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">
      
      </beans>
      
    • 编写xml标签
      在这里插入图片描述

       <!--
          开启组件扫描
          <context:component-scan></context:component-scan>
          base-package写法:
              1.扫描多个包,多个包用逗号隔开
              <context:component-scan base-package="com.yang.pojo,com.yang.dao"></context:component-scan>
              2.扫描包的上层目录
              <context:component-scan base-package="com.yang"></context:component-scan>
          -->
          <context:component-scan base-package="com.yang"></context:component-scan>
      

3.创建类,在类上添加创建对象注解

package com.yang.service;

import org.springframework.stereotype.Component;

/**
 * @program: TestSpring
 * @description: UserService层
 * @author: 陈阳
 * @create: 2021-01-24 21:12
 **/

//    在注解里value属性值可以不写
//    默认值是类的名称,首字母小写
//    UserService --- userService
@Component(value = "userService")//<bean id="userService" class="......." />
public class UserService {

    public void add(){
        System.out.println("service层的添加方法");
    }

}
  • 测试是否可用
    1.测试代码:
    package com.yang.run;
    
    import com.yang.service.UserService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @program: TestSpring
     * @description: 测试注解方式是否可用
     * @author: 陈阳
     * @create: 2021-01-24 21:19
     **/
    public class TestAnn {
        @Test
        public void test1(){
            ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
            UserService userService = context.getBean("userService", UserService.class);
            userService.add();
        }
    }
    
    
  1. 测试结果:
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值