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开发案例之多任务并行+线程池处理
前言 前几篇文章着重介绍了后端服务数据库和多线程并行处理优化,并示例了改造前后的伪代码逻辑.当然了,优化是无止境的,前人栽树后人乘凉.作为我们开发者来说,既然站在了巨人的肩膀上,就要写出更加优化的程序 ...
随机推荐
- css边框样式(动画)
html: <div class="wrap"> <a href="#">shui</a> </div> css ...
- Java:代码高效优化
本文转自阿里技术站,感谢阿里前辈提供的技术知识,微信关注 "阿里技术" 公众号即可实时学习. 1.常量&变量 1.1.直接赋值常量值,禁止声明新对象 直接赋值常量值,只是创 ...
- 在Intellij IDEA中新建Web项目
1.点击左上角 文件(F) ,--> new --> 项目 2.勾选下面的复选框,下一步就是给项目起名字和存放项目的位置 2.在Web文件下新建 clsses 和 lib文件夹:http ...
- 14个Java技术网站,程序员必备!
先看再点赞,给自己一点思考的时间,如果对自己有帮助,微信搜索[程序职场]关注这个执着的职场程序员.我有什么:职场规划指导,技能提升方法,讲不完的职场故事,个人成长经验. 程序员都是无师自通?这就有点胡 ...
- 判断字符串是否为ip地址----python
def isIp(ip_str): flag = True if '.' not in ip_str: return False if ip_str.count('.')!=3 : return Fa ...
- Mysql常用语句整理
把工作常用的mysql命令整理一下,省的用的时候在到处找 1.常用命令 1.1 登录 mysql -u root -p 1.2 生成随机数 若在 i<=R<=j 范围内生成随机数 FLOO ...
- C语言:统计字符个数及种类
#include <stdio.h> int main(){ char c; //用户输入的字符 int shu=0;//字符总数 int letters=0, // 字母数目 space ...
- 《鸟哥Linux私房菜》 全套视频和PDF资料—— 老段带你学鸟哥Linux视频课程
<鸟哥的Linux私房菜-基础篇-服务器篇(第三版)>学习Linux极为经典的入门资料,但是还是很多同学难以坚持系统的看完整本书,最终以放弃而告终. 为了帮助大家更容易入门Linux,老段 ...
- PHP获取当日或本月时间戳范围
在mysql数据库中使用int类型保存时间戳时,一般在程序中可能会用到统计当日,当月的一些数据.那么可以用如下的方式限定时间范围: //当日销售 $today_start = strtotime( ...
- 给KVM添加新的磁盘
给KVM添加新的磁盘 两种方案 1 添加虚拟磁盘文件 2 添加物理磁盘 硬件配置: 物理主机(宿主机):foundation 物理主机磁盘情况: 我们有三块物理磁盘,sda.sdb和sdc(这里都是S ...