[黑马程序员SSM框架教程] Spring-22 注解开发依赖注入

1.自动装配是基于暴力反射对私有属性进行装配的,所以不需要setter方法。打破了IOC提供对象,我提供入口的思想。现在不用提供入口也能实现。
2. @Qualifier必须依赖注解@Autowired,当自动装配的接口类型有多个实现类时使用,
3. @Autowired就相当于bean里面的绑定,将服务层中添加的数据层接口与数据层中注册对应的实现类进行绑定的过程。注解中没有对应的id名是因为默认按接口类型装配,所以不需要取名字就能自动绑定。很神奇。
具体实现代码:(自动装配,按id自动装配,简单类型装配,文件载入装配)

  • 导入框架依赖(省略)
  • 项目结构
    在这里插入图片描述
  • 配置类
package com.ljh.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.ljh")
public class SpringConfig {
    //@Configuration注解声明配置类,相当于配置文件
    //@ComponentScan("com.ljh") 相当于扫描bean的位置,收集所有bean对象
}
  • dao层接口省略
  • dao层实现类1
package com.ljh.dao.impl;

import com.ljh.dao.BookDao;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//@Repository("bookDao1")。数据层注解,可以不加名字
//一些生命周期配置
@Repository("bookDao1")
public class BookDaoImpl implements BookDao {
    public void save() {
        System.out.println("Dao层数据集");
    }
    @PostConstruct
    public void init(){
        System.out.println("init");

    }
    @PreDestroy
    public void des(){
        System.out.println("des");
    }
}
  • 同一接口dao层实现类,仅以输出做区分,体现后面的@Qualifier的作用
  • 加入了@Value(“ljh”)标签进行简单类型的依赖注入。
package com.ljh.dao.impl;

import com.ljh.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//@Repository("bookDao1")。数据层注解,可以不加名字
//一些生命周期配置
@Repository("bookDao2")
public class BookDaoImpl2 implements BookDao {
    @Value("ljh")
    private String name;
    public void save() {
        System.out.println("Dao2层数据集 "+name);
    }
    @PostConstruct
    public void init(){
        System.out.println("init2");

    }
    @PreDestroy
    public void des(){
        System.out.println("des2");
    }
}

  • 服务层实现类
package com.ljh.service.impl;

import com.ljh.dao.BookDao;
import com.ljh.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
//@Service服务层实现类注解
@Service
public class BookServiceImpl implements BookService {
    //@Autowired DI依赖,用于在ioc容器中找到对应bean,进行注入
    //@Qualifier("bookDao1")搭配@Autowired使用,用于装配指定实现类id
    @Autowired
    @Qualifier("bookDao1")
    private BookDao bookDao1;
    @Autowired
    @Qualifier("bookDao2")
    private BookDao bookDao2;
    public void save() {
        System.out.println("启动服务层");
        bookDao1.save();
        bookDao2.save();
    }
}
  • 启动类
package com.ljh;

import com.ljh.config.SpringConfig;
import com.ljh.service.BookService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class app {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext atx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookService bean = atx.getBean(BookService.class);
        bean.save();
        atx.close();
    }
}
  • 运行结果:可以看到是先开启所有bean生命周期再根据名字进行依赖注入。最后统一关闭。解释了@Qualifier可以将相同接口的同一类型不同注解的实现类进行分别指定的装配。
    (https://img-blog.csdnimg.cn/6e3b9e68e1944677b5caeaf6793e4006.png)

  • 载入外部文件
    在资源文件夹命名jdbc.properties文件

name = ljh
  • 配置类中载入
    @PropertySource(“jdbc.properties”)
package com.ljh.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan("com.ljh")
@PropertySource("jdbc.properties")
public class SpringConfig {
    //@Configuration注解声明配置类,相当于配置文件
    //@ComponentScan("com.ljh") 相当于扫描bean的位置,收集所有bean对象
}
  • 将实现类中 @Value(“ljh“)改成 @Value(”${name}")
package com.ljh.dao.impl;

import com.ljh.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//@Repository("bookDao1")。数据层注解,可以不加名字
//一些生命周期配置
@Repository("bookDao2")
public class BookDaoImpl2 implements BookDao {
    @Value("${name}")
    private String name;
    public void save() {
        System.out.println("Dao2层数据集 "+name);
    }
    @PostConstruct
    public void init(){
        System.out.println("init2");

    }
    @PreDestroy
    public void des(){
        System.out.println("des2");
    }
}
  • 运行结果:可以发现ljh依然能从外部文件注入(载入多个配置文件和扫描包一样{,,},但是,*.properties不可用)
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

伊可同学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值