SpringIoC注解式开发

9.1 声明Bean的注解

负责声明Bean的注解,常见的包括四个:
● @Component

package com.powernode.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Component {
    String value();
}

● @Controller

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}


● @Service

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

● @Repository

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

阅读完源码 我们发现 其实 @Controller @Service @Repository 这三个注解都是@Component注解的别名

也就是说无论用这四个中哪个注解 都可以让spring的IoC容器管理Bean

那一个注解就可以搞定为什么延伸出后面三个呢??这是为了方便我们阅读 就比如看到Service我们就知道是业务层 看到Repository就知道是持久层

只是为了增强程序的可读性,建议:

  • 控制器类上使用:Controller
  • service类上使用:Service
  • dao类上使用:Repository

这些注解都有一个value属性 value属性就是对应的Beanid

@Service(“userBean”) 相当于在xml中

9.2 注解的使用

如何使用以上的注解呢?

  • 第一步:加入aop的依赖
  • 第二步:在配置文件中添加context命名空间
  • 第三步:在配置文件中指定扫描的包
  • 第四步:在Bean类上使用注解

tips(因为在关联Spring-Context依赖的时候 aop会自动导入 所以这一步不用做)

添加context命名空间

<?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对象 如果有对应的注解 就放入map-->
<context:component-scan base-package="com.myStudy.Bean"></context:component-scan>

在Bean上加注解

@Service("people")
public class People {
}
@Service
public class User {
}

测试!

在这里插入图片描述

还有一点 如果注解中 value值没有指定 那就默认类名且首字母小写

9.3 注入的注解

@Component @Controller @Service @Repository 这四个注解是用来声明Bean的,声明后这些Bean将被实例化。接下来我们看一下,如何给Bean的属性赋值。给Bean属性赋值需要用到这些注解:

  • @Value
  • @Autowired
  • @Qualifier
  • @Resource

我就着重讲解下@Value和@Resource叭

9.3.1@Value 注入简单类型

直接上代码

public class MyDataSource  {
//    value注解可以用在构造方法上 也可以直接用在属性上 或者用在构造器上 底层是反射
    @Value("com.mysql.cj.jdbd.Driver")
    private  String driver;

    @Value("jdbc:mysql://localhost:3306/spring6")
    private  String url;

    @Value("root")
    private String username;
    @Value("123456")
    private  String passwd;

    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", passwd='" + passwd + '\'' +
                '}';
    }

这就给一个jdbc的配置信息

然后我们声明完属性可以直接用value注解 然后底层嘛就是反射 跟set没关系 因为这里我们没有set方法依然可以赋值 来看测试程序

在这里插入图片描述

9.3.2@Resource注入非简单类型

@Resource注解默认根据名称装配byName,未指定name时,使用属性名作为name。通过name找不到的话会自动启动通过类型byType装配。

在使用@Resource这个注解前 要引入依赖

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>3.0.0-M1</version>
</dependency>

然后上代码测一下子

public class School {
//
//@Resource(name = "teacher")
    @Resource//如果不指定name属性 就会默认把name赋值为属性名teacher
//    如果属性名找不到才会按照byType
    private Teacher teacher1;

    public void A(){
        System.out.println(teacher1);
    }
}
public class Teacher {
}

在这里插入图片描述

这里发现也是没有任何问题的 其实一句话总结Resource就是

默认byName注入,没有指定name时把属性名当做name,
根据name找不到时,才会byType注入。byType注入时,某种类型的Bean只能有一个

9.4 全注解开发

上面演示其实相当于半注解 开发 一半xml一半注解 那么接下来就直接 全注解开发

其实很简单 首先要写一个类代替xml文件

@Configuration
@ComponentScan({"com.myStudy.noXml"})
public class xmlClass {

}

这里看到加了两个注解

  1. @Configuration 声明是一个配置
  2. ComponentScan 就是组件扫描(包名下的子类)扫描哪些类有着Bean管理的注解

那就给一些Bean测试一下吧

@Component("man")
public class Man {
    @Resource(name = "woman")
    private Woman woman;
    @Override
    public String toString() {
        return "Man{" +
                "woman=" + woman +
                '}';
    }
}
@Component("woman")
public class Woman {

}

测试类

这里注意以前有配置文件我们多态new的是ClassPathXmlApplicationContext(注解文件名)

而全注解我们要new AnnotationConfigApplicationContext(配置类)

AnnotationConfigApplicationContext Context = new AnnotationConfigApplicationContext(xmlClass.class);
Man man = Context.getBean("man", Man.class);
System.out.println(man);

运行结果也是没毛病

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值