工厂模式,@Autowire可以自动注入bean句柄到Map中(附源码)
问题背景
最近做了一个项目需要使用工厂模式,但不同类型的句柄比较多,一个个添加比较麻烦,使用@Autowire自动注入可以减少代码量,和防止遗忘少添加句柄
注意事项:
- 可以直接使用文中代码,也可以下载源码进行参考
项目搭建
1 引入pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yg</groupId>
<artifactId>autowireBean</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>autowireBean</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2 创建工厂
package com.yg.autowirebean.service;
/**
* @Author suolong
* @Date 2022/4/1 11:43
* @Version 1.5
*/
public interface AnimalFactory {
String eat();
}
3 添加不同工厂类型句柄,默认@Service的句柄名字为开头字母小写,也可以自己指定
package com.yg.autowirebean.service.impl;
import com.yg.autowirebean.service.AnimalFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @Author suolong
* @Date 2022/4/1 11:46
* @Version 1.5
*/
@Slf4j
@Service
public class Dog implements AnimalFactory {
@Override
public String eat() {
log.info("Dog eat meat");
return "meat";
}
}
package com.yg.autowirebean.service.impl;
import com.yg.autowirebean.service.AnimalFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @Author suolong
* @Date 2022/4/1 11:47
* @Version 1.5
*/
@Slf4j
@Service
public class Cat implements AnimalFactory {
@Override
public String eat() {
log.info("Cat eat fish");
return "fish";
}
}
4 自动把bean句柄导入到Map中,然后使用
package com.yg.autowirebean.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Set;
/**
* @Author suolong
* @Date 2022/4/1 11:49
* @Version 1.5
*/
@Service
@Slf4j
public class Eat {
@Autowired
Map<String, AnimalFactory> animalHandler;
@PostConstruct
public void test() {
log.info("Start up");
Set<Map.Entry<String, AnimalFactory>> entries = animalHandler.entrySet();
for (Map.Entry<String, AnimalFactory> entry : entries) {
log.info("animal: {}, food: {}", entry.getKey(), entry.getValue().eat());
}
}
}
测试结果
1 运行项目,可以看到key为句柄明,默认首字母小写,所以为cat,dog,value则是自动导入的bean句柄
总结
这种方式可以不用自己一个个导入,比较快捷
作为程序员第 92 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …