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

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

1、什么是注解
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化 xml 配置
2、Spring 针对 Bean 管理中创建对象提供注解
(1)@Component
(2)@Service
(3)@Controller
(4)@Repository

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

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

第一步 引入依赖
在这里插入图片描述
第二步 开启组件扫描,引入 context 名称空间

<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:util="http://www.springframework.org/schema/util" 
 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/util http://www.springframework.org/schema/util/spring-util.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启组件扫描
 1 如果扫描多个包,多个包使用逗号隔开
 2 扫描包上层目录
-->
<context:component-scan base-package="com.atguigu"></context:component-scan>

第三步 创建类,在类上面添加创建对象注解

//在注解里面 value 属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class=".."/>
public class UserService {
    public void add() {
      System.out.println("service add.......");
    }
}

4、开启组件扫描细节配置

<!--示例 1 use-default-filters="false" 表示现在不使用默认 filter,自己配置 filter context:include-filter ,设置扫描哪些内容-->
<context:component-scan base-package="com.atguigu" use-defaultfilters="false">
 <context:include-filter type="annotation" 
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--示例 2 下面配置扫描包所有内容 context:exclude-filter: 设置哪些内容不进行扫描-->
<context:component-scan base-package="com.atguigu">
 <context:exclude-filter type="annotation" 
 expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

基于注解方式实现属性注入

(1)@Autowired:根据属性类型进行自动装配(如果一个接口有多个实现类,则不能用根据属性类型进行装配,因为他分不清是哪个实现类,是能用根据属性名称来进行属性注入)

第一步 把 service 和 dao 对象创建,在 service 和 dao 类添加创建对象注解

第二步 在 service 注入 dao 对象,在 service 类添加 dao 类型属性,在属性上面使用注解

@Service
public class UserService {
    //定义 dao 类型属性
    //不需要添加 set 方法
    //添加注入属性注解
    @Autowired 
    private UserDao userDao;
    public void add() {
      System.out.println("service add.......");
      userDao.add();
    }
}

(2)@Qualifier:根据名称进行注入
这个@Qualifier 注解的使用,和上面@Autowired 一起使用

   //定义 dao 类型属性
   //不需要添加 set 方法
   //添加注入属性注解
   @Autowired //根据类型进行注入
   @Qualifier(value = "userDaoImpl1") //根据名称进行注入
   private UserDao userDao;

(3)@Resource:可以根据类型注入,可以根据名称注入 (特殊点:这是属于javax中的注解,不是spring中的)

   //@Resource //根据类型进行注入
   @Resource(name = "userDaoImpl1") //根据名称进行注入
   private UserDao userDao;

(4)@Value:注入普通类型属性

   @Value(value = "abc")
   private String name;

完全注解开发

了解:只是作为了解,平时在开发中直接用SpringBoot进行完全注解开发
(1)创建配置类,替代 xml 配置文件

   @Configuration //作为配置类,替代 xml 配置文件
   @ComponentScan(basePackages = {"com.atguigu"})
   public class SpringConfig {
   }

(2)编写测试类

   @Test
   public void testService2() {
    //加载配置类
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); //这个位置变了不是ClassPathXmlApplicationContext,因为我们不需要XML配置文件,我们需要的是一个配置类!
    UserService userService = context.getBean("userService",  UserService.class);
    System.out.println(userService);
    userService.add();
   }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真真最可爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值