当前位置: 首页 > news >正文

后端:Spring、Spring Boot-实例化Bean依赖注入(DI)

文章目录

    • 1. 实例化Bean
    • 2. 使用FactoryBean
    • 3. 依赖注入(DI)
      • 3.1 @AutoWired 属性注入(查找顺序:先类型,后名字)
      • 3.2 @AutoWired 在构造函数&参数上的使用
      • 3.3 @Inject和@Resource 进行依赖注入
      • 3.4 @Value 进行注入

1. 实例化Bean

默认使用无参构造函数,如果在这个Bean下定义了一个有参的构造方法(没有写无参构造方法),实例化时使用的是这个有参构造方法;如果有多个有参的构造方法(没有写无参构造方法),此时实例化时会报错,因为不知道使用哪个构造方法。

package com.lize.demo.dao;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import org.springframework.stereotype.Component;@Component("UserDao")
public class UserDao {private TestBean tb;private TestBean2 tb2;
//    public UserDao(){
//        System.out.println("构造函数");
//    }public UserDao(TestBean tb){System.out.println("有参的构造函数"+tb);this.tb = tb;}public UserDao(TestBean tb,TestBean2 tb2){System.out.println("有参的构造函数"+tb+tb2);this.tb = tb;this.tb2 = tb2;}public void printUserDao(){System.out.println("UserDao");}}
package com.lize.demo;import com.lize.demo.dao.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class DemoApplicationTests {@Autowiredprivate UserDao ud;@Testvoid contextLoads() {ud.printUserDao();}}

报错信息如下:
在这里插入图片描述
此时如果要实例化有参的Bean,可以使用注解@Bean的方式来进行,如下:

package com.lize.demo.dao;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;public class UserDao {private TestBean tb;private TestBean2 tb2;public UserDao(TestBean tb){System.out.println("有参的构造函数"+tb);this.tb = tb;}public UserDao(TestBean tb,TestBean2 tb2){System.out.println("有参的构造函数"+tb+tb2);this.tb = tb;this.tb2 = tb2;}public void printUserDao(){System.out.println("UserDao");}}
package com.lize.demo.config;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import com.lize.demo.dao.UserDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;@Configuration
public class SpringConnfig {@Beanpublic UserDao getUserDao(TestBean tb,TestBean2 tb2){return new UserDao(tb,tb2);}
}

运行结果如下:
在这里插入图片描述

2. 使用FactoryBean

定义一个类,让其实现FactoryBean这个接口,并重写其下方法,如下:

package com.lize.demo.service;import com.lize.demo.TestBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Service;@Service("UserService")
public class UserService implements FactoryBean {@Overridepublic Object getObject() throws Exception {return new TestBean();}@Overridepublic Class<?> getObjectType() {return null;}
}
package com.lize.demo;import com.lize.demo.dao.UserDao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);System.out.println(context.getBean("UserService"));}}

此时的打印结果如下:
在这里插入图片描述
上述打印结果为TestBean,而不是UserService这个Bean。如果要获取UserService这个Bean,可以通过类型获取,如下:
在这里插入图片描述
还有一种做法就是在第一种的基础上,通过字符串获取Bean,字符串前面加上“&”符号,如下:
在这里插入图片描述
如果想通过类型获取TestBean这个Bean,可以在getObjectType方法下添加对应的类型信息,如下:
在这里插入图片描述
在这里插入图片描述
运行结果:
在这里插入图片描述
总结一下:
使用FactoryBean来实例化Bean。

  • FactoryBean是一个接口;
  • 需要有一个Bean,一旦这个Bean实现FactoryBean就成为了特殊的Bean;
  • 需要实现两个方法
    • getObject,当通过Bean实际名获取到的Bean就是getObject返回的对象(伪装);
    • getObjectType,想通过获取对应的类型去获取这个伪装的Bean,就需要返回getObject返回的对象的类型;
  • 可以自由控制Bean的构造方法来实例化Bean

3. 依赖注入(DI)

3.1 @AutoWired 属性注入(查找顺序:先类型,后名字)

使用这个注解,首先会通过类型去容器中查找是否有这个Bean,如果没有,再通过名字去查找是否有这个Bean。

直接在类上添加注解@Component定义Bean,名字为testBean3

package com.lize.demo;import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;@Component
//@Primary
public class TestBean3 {private String name;public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "TestBean3{" +"name='" + name + '\'' +'}';}
}

使用配置类定义Bean,名字为:TestBean31

package com.lize.demo.config;import com.lize.demo.TestBean3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SpringConnfig {@Beanpublic TestBean3 TestBean31(){return new TestBean3();}
}

上述定义了两个类型相同的Bean。

package com.lize.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class TestBean4 {@Autowiredprivate TestBean3 testBean3;@Overridepublic String toString() {return "TestBean4{" +"testBean3=" + testBean3 +'}';}
}

在TestBean4 中引入这个Bean,然后在单元测试中输出结果如下:

package com.lize.demo;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class DemoApplicationTests {@Autowiredprivate TestBean4 tb4;@Testvoid contextLoads() {System.out.println(tb4);}
}

运行结果如下:
在这里插入图片描述
可以看到,此时因为有两个Bean类型相同,因此采用名字去查找Bean,在TestBean4中使用的Bean名字为testBean3,因此输出的结果中的Bean为直接在类上添加注解@Component的那个Bean(name的值默认为空)。如果把TestBean4中的那个Bean的名字修改为TestBean31,那么此时的输出结果就是通过配置类定义的那个Bean了。

package com.lize.demo.config;import com.lize.demo.TestBean3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SpringConnfig {@Beanpublic TestBean3 TestBean31(){TestBean3 testBean3 = new TestBean3();testBean3.setName("TestBean31");return testBean3;}}
package com.lize.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class TestBean4 {@Autowiredprivate TestBean3 TestBean31;@Overridepublic String toString() {return "TestBean4{" +"testBean3=" + TestBean31 +'}';}
}

在这里插入图片描述

如果通过名字还是查找不到,比如把TestBean4中的引入那个Bean的名字修改为tb3,那么此时就会报错了。
在这里插入图片描述

此时可以在定义Bean的那个类上添加注解 @Primary,表示主要的。
在这里插入图片描述

在这里插入图片描述
另外一种解决方法就是在这个TestBean4引入的那个Bean下指明到底是哪个Bean(使用注解 @Qualifier),如下:
在这里插入图片描述

3.2 @AutoWired 在构造函数&参数上的使用

如果一个Bean定义了多个有参的构造函数,但是没有定义默认的构造函数(无参构造函数),此时在另外一个类中引入这个Bean,然后在单元测试中输出这个Bean,会报错,如下:

package com.lize.demo.dao;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import org.springframework.stereotype.Component;@Component("UserDao")
public class UserDao {private TestBean tb;private TestBean2 tb2;
//    public UserDao(){
//        System.out.println("构造函数");
//    }public UserDao(TestBean tb){System.out.println("有参的构造函数"+tb);this.tb = tb;}public UserDao(TestBean tb,TestBean2 tb2){System.out.println("有参的构造函数"+tb+tb2);this.tb = tb;this.tb2 = tb2;}public void printUserDao(){System.out.println("UserDao");}}

在这里插入图片描述
如果此时想要正常输出,可以在对应的构造函数上面添加注解@AutoWired ,如下,此时正常输出。
在这里插入图片描述
TestBean、TestBean2如下形式:
在这里插入图片描述
如果想要为构造函数中的参数设置为不必须的,需要在参数上面设置 @Autowired(required = false),直接在构造函数上设置是不生效的,因此会报错(下面没有给出),如下:
在这里插入图片描述

在这里插入图片描述
此时打印结果为null。
另外,还可以写在单元测试的方法上面,如下:
在这里插入图片描述
Spring会自动调用@Autowired的方法进行自动注入,在没有调用set的方法的前提下,此时调用get的结果不为null,如下:

package com.lize.demo.dao;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component("UserDao")
public class UserDao {private TestBean tb;private TestBean2 tb2;
//    public UserDao(){
//        System.out.println("构造函数");
//    }@Autowiredpublic UserDao(@Autowired(required = false) TestBean tb){System.out.println("有参的构造函数"+tb);this.tb = tb;}@Autowiredpublic void setTb2(TestBean2 tb2){this.tb2 = tb2;}public TestBean2 getTb2(){return tb2;}public UserDao(TestBean tb,TestBean2 tb2){System.out.println("有参的构造函数"+tb+tb2);this.tb = tb;this.tb2 = tb2;}public void printUserDao(){System.out.println("UserDao");}}

在这里插入图片描述

3.3 @Inject和@Resource 进行依赖注入

@Resource优先根据名字进行查找,找不到再根据类型查找。
@inject不能设置required=false属性,另外还需要添加额外的依赖。
推荐使用构造函数进行注入,或者@Resource进行注入

3.4 @Value 进行注入

基本数据类型的注入

package com.lize.demo.entity;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class User {@Value("lize")private String name;@Value("19")private Integer age;@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +'}';}
}
package com.lize.demo;import com.lize.demo.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class DemoApplicationTests {@Testvoid contextLoads(@Autowired User user) {System.out.println(user);}}

在这里插入图片描述
如果想通过从文件中的数据进行注入,如下,新建a.properties
在这里插入图片描述
在这里插入图片描述
在Spring Boot项目中,如果想获取配置文件application.properties中的数据,不需要使用@PropertySource指定路径文件,如下:
在这里插入图片描述
在这里插入图片描述
如果在数据文件获取不到对应数据,在Spring Boot项目中会报错(解决方法为在变量名后面加入“:”填写默认值),但是在Spring中会指定把值直接注入到对应变量。
在这里插入图片描述
复杂数据类型的注入,使用spel表达式的方式进行注入,如下:

package com.lize.demo.entity;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;@Component
public class User {@Value("#{{'语文':'98'}}")private Map<String,String> score;@Value("#{'王者,原神'}")private List<String> like_games;@Overridepublic String toString() {return "User{" +"score=" + score +", like_games=" + like_games +'}';}
}

在这里插入图片描述

http://www.lryc.cn/news/474793.html

相关文章:

  • C++ 数据结构 静态顺序表、动态顺序表。
  • QML旋转选择器组件Tumbler
  • 在工作中常用到的 Linux 命令总结
  • 反射、枚举和lambda表达式
  • Stable Diffusion Web UI 1.9.4常用插件扩展-WD14-tagger
  • java 第19天 Lambda、jdk8.0新方法、Optional
  • 江协科技STM32学习- P31 I2C通信协议
  • 6、liunx目录的功能
  • 挑战Java面试题复习第5天,无人扶我青云志
  • 大规模语言模型:从理论到实践(1)
  • C#如何锁定和解除鼠标及键盘BlockInput
  • Stable Diffusion 3.5发布:图像生成新纪元,多模态AI的突破!
  • MySQL超大分页怎么优化处理?limit 1000000,10 和 limit 10区别?覆盖索引、面试题
  • RabbitMQ 实现消息队列负载均衡
  • 嵌入式linux中HTTP协议原理基本分析
  • thinkphp和vue基于Workerman搭建Websocket服务实现用户实时聊天,完整前后端源码demo及数据表sql
  • 浅谈射频应用
  • SAP(PP生产制造)拆解工单业务处理
  • 《Python游戏编程入门》注-第4章2
  • deque
  • YOLOv11改进策略【卷积层】| CVPR-2020 Strip Pooling 空间池化模块 处理不规则形状的对象 含二次创新
  • yt-dlp下载视频
  • oracle insert忽略主键冲突,忽略重复记录
  • 小新学习k8s第四天之发布管理
  • 01_IAR新建CC2530工程
  • 原生鸿蒙的竞争力到底如何?
  • 数字化生态平台:关键功能全解析
  • c 到 c++ 过渡
  • [linux驱动开发--环境搭建] qemu-9.1+linux-kernel-6.11
  • 019集——获取CAD图中多个实体的包围盒(CAD—C#二次开发入门)