SpringBoot开发二
需求介绍-Spring入门
主要是理解IOC
,理解容器和Bean
代码
在Test
里面利用getBean
方法帮助我们看一下容器的创建:
那我首先要写一个Bean
对象,假设是写一个访问数据库类。
AlphaDao
(interface
)类型:
package com.nowcoder.community.dao; public interface AlphaDao {
String select();
}
然后写两个类实现这个接口体验利用容器好处:
AlphaDaoHibernatelmpl
:
package com.nowcoder.community.dao;
import org.springframework.stereotype.Repository;
@Repository("AlphaHibernate")
public class AlphaDaoHibernateImpl implements AlphaDao {
@Override
public String select() {
return "Hibernate";
}
}
AlphaDaoMybatisImpl
:
package com.nowcoder.community.dao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
@Repository
@Primary // 具有更高优先级
public class AlphaDaoMyBatisImpl implements AlphaDao{
@Override
public String select() {
return "MyBatis";
}
}
这个时候就有两个Bean
对象,可以通过容器管理了。
其次呢,Spring
容器还可以管理bean
的声明周期,实现一些业务逻辑,那我们重新再写一个Bean
AlphaService
:
package com.nowcoder.community.service; import com.nowcoder.community.dao.AlphaDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; @Service
@Scope //("prototype"):表示Bean实例有多个,每次get就实例化一个。默认单例。
public class AlphaService { @Autowired
private AlphaDao alphaDao;
//被Spring容器管理的bean只被实例化一次,因为它是单例的
public AlphaService() {
System.out.println("实例化AlphaService");
}
//初始化在构造器之后
@PostConstruct
public void init() {
System.out.println("初始化AlphaService");
}
//销毁之前调用,释放某些资源
@PreDestroy
public void destroy() {
System.out.println("销毁AlphaService");
} public String find() {
return alphaDao.select();
} }
上面我们都是自己写的Bean
,但是有的时候我们希望能在容器中加载一个第三方的Bean
,
那我们就需要自己写一个配置类,在配置类中通过Bean
注解进行申明,那么就开始写一个配置类。
所有第三方的都写在config
这个包里面:
AlphaConfig
:
package com.nowcoder.community.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.text.SimpleDateFormat; //配置类,加载第三方的bean
@Configuration
public class AlphaConfig {
@Bean
public SimpleDateFormat simpleDateFormat() {//方法名就是bean的名字
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); }
}
但是这种都是自己去主动去获取,我们其实可以通过依赖注入来实现。
在使用之前进行申明就可以了,使用这个@Autowired
注解。
上面都是bean
声明,下面就是一个具体的测试的方法了。
CommunityApplicationTests
:
package com.nowcoder.community; import com.nowcoder.community.dao.AlphaDao;
import com.nowcoder.community.service.AlphaService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import java.text.SimpleDateFormat;
import java.util.Date; @RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
class CommunityApplicationTests implements ApplicationContextAware {
private ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Test
public void testApplicationContext() {
System.out.println(applicationContext);
AlphaDao alphaDao = applicationContext.getBean(AlphaDao.class);
System.out.println(alphaDao.select()); alphaDao = applicationContext.getBean("AlphaHibernate", AlphaDao.class);
System.out.println(alphaDao.select());
} @Test
public void testBeanManagement() {
AlphaService alphaService = applicationContext.getBean(AlphaService.class);
System.out.println(alphaService);
} @Test
public void testBeanConfig() {
SimpleDateFormat simpleDateFormat = applicationContext.getBean(SimpleDateFormat.class);
System.out.println(simpleDateFormat.format(new Date()));
} @Autowired
@Qualifier("AlphaHibernate")// 加载特指的Bean
private AlphaDao alphaDao; @Autowired
private AlphaService alphaService; @Autowired
private SimpleDateFormat simpleDateFormat; // 测试依赖注入
@Test
public void testDI() {
System.out.println(alphaDao);
System.out.println(alphaService);
System.out.println(simpleDateFormat);
}
}
SpringBoot开发二的更多相关文章
- SpringBoot开发二十-Redis入门以及Spring整合Redis
安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis 的常用命 ...
- SpringBoot开发二十四-Redis入门以及Spring整合Redis
需求介绍 安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis ...
- SpringBoot开发二十二-统一处理异常
需求介绍 首先服务端分为三层:表现层,业务层,数据层. 请求过来先到表现层,表现层调用业务层,然后业务层调用数据层. 那么数据层出现异常它会抛出异常,那异常肯定是抛给调用者也就是业务层,那么业务层会再 ...
- SpringBoot开发二十一-发送私信
发送私信功能开发: 功能开发 数据访问层 message-mapper.xml 增加 <insert id="insertMessage" parameterType=&qu ...
- SpringBoot开发二十-私信列表
私信列表功能开发. 发送私信功能开发 首先创建一个实体类:Message package com.nowcoder.community.entity; import java.util.Date; p ...
- SpringBoot开发二十三-统一记录日志
统一记录日志 AlphaAspect package com.nowcoder.community.aspect; import org.aspectj.lang.ProceedingJoinPoin ...
- (二)SpringBoot之springboot开发工具的使用以及springboot插件的功能
一.springboot开发工具的使用 1.1 在项目中添加springoot开发工具 1.2 功能 修改代码后点击保存自动重启 二.springboot插件的功能 2.1 maven配置 <p ...
- 基于SpringBoot开发一个Restful服务,实现增删改查功能
前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...
- SpringBoot开发案例之多任务并行+线程池处理
前言 前几篇文章着重介绍了后端服务数据库和多线程并行处理优化,并示例了改造前后的伪代码逻辑.当然了,优化是无止境的,前人栽树后人乘凉.作为我们开发者来说,既然站在了巨人的肩膀上,就要写出更加优化的程序 ...
随机推荐
- 如何快速实现一个虚拟 DOM 系统
虚拟 DOM 是目前主流前端框架的技术核心之一,本文阐述如何实现一个简单的虚拟 DOM 系统. 为什么需要虚拟 DOM? 虚拟 DOM 就是一棵由虚拟节点组成的树,这棵树展现了真实 DOM 的结构.这 ...
- Redis 高级面试题
Redis 持久化机制 Redis 是一个支持持久化的内存数据库,通过持久化机制把内存中的数据同步到硬盘文件来 保证数据持久化.当 Redis 重启后通过把硬盘文件重新加载到内存,就能达到恢复数据的目 ...
- Whitzard OJ Introduce to packing
1.概述 这个就是个smc,为什么会归于加壳,我个人理解是和UPX的运行方式有点像把,不对应该是说和压缩壳的运行方式 很相似,都是先运行一段解密代码,之前的符号表也替换了下 2.解题 有两种方式一种是 ...
- Linux中ftp服务器搭建
一.FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp ...
- Sharding+SpringBoot+Mybatis 读写分离
基于Sharding JDBC的读写分离 1.引入pom.xml <dependencies> <!-- mybatis --> <dependency> < ...
- Intellij idea 设置关闭自动更新
目录结构: File -> Settings- -> Appearance & Behavior -> System Settings -> Updates 把Auto ...
- 【redis前传】redis整数集为什么不能降级
前言 整数集合相信有的同学没有听说过,因为redis对外提供的只有封装的五大对象!而我们本系列主旨是学习redis内部结构.内部结构是redis五大结构重要支撑! 前面我们分别从redis内部结构分析 ...
- 结对开发_石家庄地铁查询web系统_psp表
结对开发_石家庄地铁查询_博客地址:https://www.cnblogs.com/flw0322/p/10680172.html PSP0: PSP0 Personal Software Proce ...
- Python+js进行逆向编程加密MD5格式
一.安装nodejs 二.安装:pip install PyExecJs 三.js源文件Md5格式存放本地,如下 var n = {}function l(t, e) {var n = (65535 ...
- 锁屏面试题百日百刷-java大厂八股文(day3)
为了有针对性的准备面试,锁屏面试题百日百刷开始每日从各处收集的面经中选择几道经典面试题分享并给出答案供参考,答案中会做与题目相关的扩展,并且可能会抛出一定问题供思考.这些题目我会标注具体的公司.招聘类 ...