Springboot大行其道的当下,很少有人单独自己整合Spring进行开发了,但是发现Spring提供的容器配合注解开发是真的非常方便。
有些非web项目使用Springboot开发难免有些不伦不类,为了能够既享受Spring带来的方便,又不让非web项目开发起来不这么不伦不类,我做了一系列的尝试,将Spring生态中的组件逐一自己做了整合。
网上很多整合案例,但是不少是需要xml的,这个整合系列尽可能的抛弃了xml方式的整合,而采用注解方式整合。
今天是这个系列的第一篇,将Spring引入一个普普通通的maven项目。
导入Spring所有依赖文件
8 8 5.3.6
org.springframework spring-context ${spring.version} org.springframework spring-core ${spring.version} org.springframework spring-aop ${spring.version} org.springframework spring-beans ${spring.version} org.springframework spring-expression ${spring.version}
打包插件
org.apache.maven.plugins maven-assembly-plugin 3.0.0 it.aspirin.HelloApplication jar-with-dependencies make-assembly package single
项目结构如下
主要创建了启动类HelloApplication.java,几个测试依赖注入的类HelloController.java, ImageService.java,ClickhouseImageServiceImpl.java,MysqlImageServiceImpl.java,ImageContext.java。下面会讲到。
├── pom.xml
└── src├── main│ ├── java│ │ └── it│ │ └── aspirin│ │ ├── HelloApplication.java│ │ ├── controller│ │ │ └── HelloController.java│ │ ├── mapper│ │ │ └── ImageMapper.java│ │ └── service│ │ ├── ImageService.java│ │ └── impl│ │ ├── ClickhouseImageServiceImpl.java│ │ ├── ImageContext.java│ │ └── MysqlImageServiceImpl.java│ └── resources│ ├── application.properties└── test└── java
创建类添加注解
@Controller
public class HelloController {@Value("${jdbc.driver}")private String driver;public String sayHello(String name) {return driver + " hello " + name;}
}
@Service
public class ClickhouseImageServiceImpl implements ImageService {@Overridepublic String databaseType() {return "ck";}@Overridepublic String insert() {return "ck insert";}
}
@Service
public class MysqlImageServiceImpl implements ImageService {@ResourceImageService imageService;@Overridepublic String databaseType() {return "mysql";}@Overridepublic String insert() {return "mysql insert";}
}
@Service
public class ImageContext implements ApplicationContextAware {private final Map map = new ConcurrentHashMap<>();public String insert(String databaseType) {final ImageService imageService = Optional.ofNullable(map.get(databaseType)).orElseThrow(() -> new IllegalArgumentException("invalid database type:" + databaseType));return imageService.insert();}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {final Map tempMap = applicationContext.getBeansOfType(ImageService.class);tempMap.values().forEach(service -> map.put(service.databaseType(), service));}
}
启动类跟Springboot一样,主要添加包扫描注解ComponentScan,用法一样。
然后通过AnnotationConfigApplicationContext类获取创建的容器。比如下面获取了HelloController类的实例,这个类并没有通过new的方式实例化。
@ComponentScan("it.aspirin")
public class HelloApplication {public static void main(String[] args) {final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(HelloApplication.class);final HelloController bean = context.getBean(HelloController.class);final String info = bean.sayHello("aspirin");LOGGER.info("info = {}", info);final ImageContext imageContext = context.getBean(ImageContext.class);LOGGER.info("imageContext.insert(\"mysql\") = " + imageContext.insert("mysql"));}
}
单纯集成Spring相对比较简单。只需要导入依赖,添加ComponentScan扫描类即可。
然后通过AnnotationConfigApplicationContext对象获取到需要的对象。
这样的好处是创建单例对象变得非常容易,对象实例不再需要手动管理,而是交给Spring统一进行管理。
日常工作操作数据库必不可少,因此下期文章讲解无xml配置纯注解方式spring集成mybatis。
上一篇:押宝AI与出海 值得买科技发布2024战略 押宝AI与出海 值得买科技发布2024战略
下一篇:华为官宣余承东职务调整;“我不服”!椰树强势回应“擦边”被罚;胖东来回应帮扶永辉丨大公司动态 华为余承东道歉 余承东否认被下令禁提遥遥领先