spring工程搭建

搭建配置Spring:
使用IDEA创建Maven工程 1.打开开发工具IDEA,点击创建新项目
2.检查项目使用的JDK是否安装好JDK 3.打开Maven工程,点击下一步
4.输入项目名spring-demo可以看到存储位置有自动追加spring-demo
5.将存储位置改为任意盘下 6.groupID一般是公司域名,我们可以使用com.自己的姓名全拼作为练习 7.版本默认即可 点击完成
IDEA配置Maven:
1.File-Setting打开设置页 2.搜索maven 3.修改maven home directory为maven安装路径
4.勾选“Override”将setting文件位置及仓库位置改为自配置 5.修改setting文件位置为 maven安装路径\conf\setting.xml 6.修改仓库位置为 maven安装路径\repository
搭建配置Spring:
引入依赖 maven仓库查询网址:MavenRepository spring基础包: spring-core:Core模块主要包含Spring框架基本的核心工具类,Spring的其他组件要都要使用到这个包里的类,Core模块是其他组件的基本核心 spring-beans:包含访问配置文件、创建和管理bean以及进行IOC/DI操作相关的所有类 spring-context:Spring的上下文即IOC容器,通过上下文可以获得容器中的Bean spring-expression:EL表达式语言用于在运行时查询和操纵对象



org.springframework
spring-core
5.2.13.RELEASE



org.springframework
spring-beans
5.2.13.RELEASE



org.springframework
spring-context
5.2.13.RELEASE



org.springframework
spring-expression
5.2.13.RELEASE

刷新maven等待自动下载 libraries中有了所有导入的包表示依赖引入完成
核心配置文件 框架是一个半成品,已经封装好了很多功能提供我们使用,而我们如何让他们工作呢? 这里需要创建一个配置文件和Spirng框架通信,文件路径为\src\main\resources文件名为applicationContext.xml
官方给出的配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!-- bean definitions here -->

把他复制到我们的配置文件后左上角会提示“Application context not configured for this file”,点击“Configure application context”,点击OK
编写代码测试 接口类 新建接口类
编写接口类

package services;

public interface UserService {
public void saveUser();
}

实现类 新建实现类
实现接口并使用快捷键添加接口方法实现 编写实现方法
package services.impl;

import services.UserService;

public class UserServiceImpl implements UserService {
public void saveUser() {
System.out.println(“service的save方法执行了”);
}
}

补充配置文件 将我们自定义的实现类交给Spring的容器管理。

测试类 新建测试类
编写测试类main方法
public class DemoTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
UserService service = (UserService) context.getBean(“userService”);
service.saveUser();
}
}

Alt+Enter键导包 测试结果
可以看到控制台打印输出 证明确实从容器中获取到了userService实例 执行过程分析 BeanFactory BeanFactory是基础类型的IOC容器,是管理bean容器的根接口,并提供了完整的IOC服务支持 简单来说BeanFactory就是一个管理Bean的工厂,它主要负责初始化各种Bean、调用生命周期等方法 ApplicationContext ApplicationContext被称为应用上下文,是BeanFactory接口的子接口,在其基础上提供了其他的附加功能,扩展了BeanFactory接口 ClassPathXmlApplicationContext ClassPathXmlApplicationContext是ApplicationContext的实现类,也在其基础上加了许多附加功能 该类从类路径ClassPath中寻找指定的XML配置文件,找到并完成对象实例化工作
其构造器源码如下:
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {configLocation}, true, null);
}

public ClassPathXmlApplicationContext(
String[] configLocations,
boolean refresh,
@Nullable ApplicationContext parent)
throws BeansException {
super(parent);
// 加载项目中的Spring配置文件
setConfigLocations(configLocations);
if (refresh) {
// 刷新容器
refresh();
}
}

构造器的作用: 调用setConfigLocations方法加载项目中的Spring配置文件 调用refresh方法刷新容器(bean的实例化就在这个方法中) refresh方法源码如下:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 准备容器刷新
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 准备bean工厂对象
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 加载配置文件中的所有bean标签
postProcessBeanFactory(beanFactory);


// Instantiate all remaining (non-lazy-init) singletons.
// 完成此上下文的bean工厂初始化,初始化所有剩余的单例bean
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
// 完成容器刷新
finishRefresh();
} catch (BeansException ex) {

} finally {

}
}
}

refresh方法的作用: 1.准备容器刷新 2.准备bean工厂对象 3,。加载配置文件中的所有bean标签 4.完成bean工厂实例化 5.完成容器刷新 context.getBean() context.getBean()方法是通过配置文件中声明的bean标签id属性获取容器内的实例。
编写代码测试:
编写接口类:java文件夹下新建接口类
public interface UserService {
public void saveUser();
}

新建实现类:输入接口方法实现
public class UserServiceImpl implements UserService {
public void saveUser() {
System.out.println(“service的save方法执行了”);
}
}

新建测试类:编写测试类main方法
public class DemoTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
UserService service = (UserService) context.getBean(“userService”);
service.saveUser();
}
}

执行过程分析:
BeanFactory BeanFactory是基础类型的IOC容器,是管理bean容器的根接口,并提供了完整的IOC服务支持
简单来说BeanFactory就是一个管理Bean的工厂,它主要负责初始化各种Bean、调用生命周期等方法
ApplicationContext ApplicationContext被称为应用上下文,是BeanFactory接口的子接口,在其基础上提供了其他的附加功能,扩展了BeanFactory接口 ClassPathXmlApplicationContext ClassPathXmlApplicationContext是ApplicationContext的实现类,也在其基础上加了许多附加功能 后端常用代码结构
接下来主要讲这三层的功能划分 Controller层(流程控制层):主要负责具体的业务模块流程的控制 Service层(业务逻辑层):主要负责业务模块的逻辑应用设计 DAO层(数据操作层):主要负责与数据库进行联络的一些任务 例如 Controller像是服务员,顾客点什么菜,菜上给几号桌,都是他的职责 Service是厨师,菜单上的菜全是他做的 Dao是厨房的小工,和原材料打交道的事情全是他管 为了方便后端与前端、后端与数据库的数据传输引入了一些定义类,如entity、vo、dto、po、jo等,常用的就是entity和vo
vo为视图类,传递和接收前端的数据,与前端所需字段一一对应 entity为实体类,与数据库表一一对应

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值