(一) Spring 通用项目结构

(一) Spring 通用项目结构

1. spring完整包4.3.6下载

spring-framework-4.3.6.RELEASE-dist.zip

2. 创建项目
因为eclipse已经抛弃了,eclipse的操作方式与idea只有略微的差别,所以这里使用idea作为项目开发工具。
效果其实差不多

在这里插入图片描述

3. 新建libs文件夹,放入核心jar包
我们使用 Core Container下的核心包

spring架构

这里使用的包有:
commons-logging-1.2.jar  // 日志包,这个是单独的
spring-beans-4.3.6.RELEASE.jar // bean
spring-context-4.3.6.RELEASE.jar // context
spring-core-4.3.6.RELEASE.jar // core 
spring-expression-4.3.6.RELEASE.jar // spEL 
仔细观察,名称一一对应,如下图所示:
在这里插入图片描述
4. src下创建applicationContext.xml文件,用来配置spring容器

在这里插入图片描述

1) applicationContext.xml基础内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

仔细观察得知:

其中xmlsnxml的命名空间,可以让我们在后面使用标签如:

 <bean id="user" class="com.dacangshu.pojo.User"/>

其中bean就是默认命名空间下的导入对应上面的基础内容中的beans

xmlns="http://www.springframework.org/schema/beans"

spring-beans.xsd文件中包括了对于xml的架构定义:

http://www.springframework.org/schema/beans/spring-beans.xsd
2) 好了,我们写一个bean试一试

com.dacangshu.pojo下创建User类试试?它是空的,不需要写任何内容。

在这里插入图片描述

我们来看看,完整的xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.dacangshu.pojo.User"/>
</beans>

注意:

<bean id="user" class="com.dacangshu.pojo.User"/>
5. 创建启动类

spring架构

package com.dacangshu;

import com.dacangshu.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author dacangshu
 * @date 2021/5/11 13:34
 */
public class BootStrap {
    public static void main(String[] args) {
        // 创建上下文context对象,使用 ClassPathXmlApplicationContext
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 从容器中获取id为user的对象
        User user = (User) context.getBean("user");

        // 输出
        System.out.println(user);
    }
}

在这里插入图片描述

看! 它开始工作了!

com.dacangshu.pojo.User@561b6512

说明:
  1. ClassPathXmlApplicationContext代表加载代码src目录下的文件,"applicationContext.xml"代表src下的"applicationContext.xml"文件

  2. User user = (User) context.getBean("user"); 可以用它代替

    // 从容器中获取id为user的对象
    User user = context.getBean("user", User.class);
    
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
哦,我很乐意为您提供帮助。下面是一个可能的Spring Cloud论坛项目结构命名: ``` forum-system/ ├── forum-common/ # 通用代码 │ ├── src/ │ ├── pom.xml ├── forum-config/ # 配置中心 │ ├── src/ │ ├── pom.xml ├── forum-auth/ # 权限管理服务 │ ├── src/ │ ├── pom.xml ├── forum-post/ # 帖子相关服务 │ ├── src/ │ ├── pom.xml ├── forum-comment/ # 评论相关服务 │ ├── src/ │ ├── pom.xml ├── forum-search/ # 搜索相关服务 │ ├── src/ │ ├── pom.xml ├── forum-gateway/ # Zuul网关服务 │ ├── src/ │ ├── pom.xml ├── forum-eureka/ # Eureka注册中心 │ ├── src/ │ ├── pom.xml ├── forum-monitoring/ # 监控服务 │ ├── src/ │ ├── pom.xml ├── forum-cicd/ # CI/CD相关配置 │ ├── Jenkinsfile │ ├── pom.xml ├── pom.xml # 整体pom文件 ``` 在上述结构中,每个服务都有一个独立的子目录。这些服务往往可以互相独立运作。例如,`forum-post`服务负责处理论坛帖子的创建、更新、查询、删除功能,而`forum-comment`服务则负责论坛评论的管理和相关功能。这种服务架构可以让团队更高效地进行协作和开发,因为每个服务都能够独立进行部署、测试、监控和维护。 除了服务子目录之外,项目还包含了通用代码和配置目录。`forum-common`目录可以包含多个通用的类库,比如一些工具类、异常处理、统一的返回值结构定义等。`forum-config`则负责存放配置文件,比如`application.yml`、`bootstrap.yml`等等。 最后,`forum-cicd`目录则包含了CI/CD相关配置,比如Jenkinsfile等,方便自动化构建和部署。整个项目的pom文件则定义了项目的依赖关系、打包方式、插件等等信息。 希望我的回答能够为您提供帮助,如果还有其他问题,欢迎继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值