Spring安装配置教程

Spring安装配置教程

 

1.Spring简介

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
2002 Rod Johnon <Expoer One-on-one j2eedevelopment and Design>
Spring 2003 ,IOC Aop
Spring data,spring boot,spring cloud,spring framework ,spring social

IOC :控制反转 (DI:依赖注入)


2.搭建Spring环境

下载jar
http://maven.springframework.org/release/org/springframework/spring/
spring-framework-4.3.9.RELEASE-dist.zip
开发spring至少需要使用的jar(5个+1个):
spring-aop.jar 开发AOP特性时需要的JAR
spring-beans.jar 处理Bean的jar 
spring-context.jar 处理spring上下文的jar 
spring-core.jar spring核心jar
spring-expression.jar spring表达式
三方提供的日志jar
commons-logging.jar 日志

2.编写配置文件
方式一:
为了编写时有一些提示、自动生成一些配置信息:
方式一:增加sts插件
可以给eclipse增加 支持spring的插件:spring tool suite(https://spring.io/tools/sts/all)
下载springsource-tool-suite-3.9.4.RELEASE-e4.7.3a-updatesite.zip,然后在Eclipse中安装:Help-Install new SoftWare.. - Add
因为我的eclipse版本不高因为你下载时的sts版本需要对应你的eclipse插件才有效,如果想要装插件的可以参考下面文章:
https://jingyan.baidu.com/article/2d5afd69208f8a85a2e28eb6.html

方式二:
直接下载sts工具(相当于一个集合了Spring tool suite的Eclipse): https://spring.io/tools/sts/
下载后会得到一个压缩包,解压后即可使用,

界面如eclipse一样,并且装上了sts插件

首先,我们新建一个Java project
然后我们来配置xml文件


新建:bean configuration .. - applicationContext.xml

3.开发Spring程序(IOC)

ApplicationContext conext = new ClassPathXmlApplicationContext(“applicationContext.xml”) ;
建立一个学生实体类以及测试类
Student.java

 
  1. package entity;

  2.  
  3. public class Student {

  4. private int stuNo;

  5. private String stuName;

  6. private int stuAge;

  7. public int getStuNo() {

  8. return stuNo;

  9. }

  10. public void setStuNo(int stuNo) {

  11. this.stuNo = stuNo;

  12. }

  13. public String getStuName() {

  14. return stuName;

  15. }

  16. public void setStuName(String stuName) {

  17. this.stuName = stuName;

  18. }

  19. public int getStuAge() {

  20. return stuAge;

  21. }

  22. public void setStuAge(int stuAge) {

  23. this.stuAge = stuAge;

  24. }

  25.  
  26. @Override

  27. public String toString() {

  28. // TODO Auto-generated method stub

  29. return this.stuNo+","+this.stuName+","+this.stuAge;

  30. }

  31. }

Test.java

 
  1. package test;

  2.  
  3. import entity.Student;

  4.  
  5. public class Test {

  6. public static void main(String[] args) {

  7. Student student = new Student();

  8. student.setStuName("zs");

  9. student.setStuAge(23);

  10. student.setStuNo(001);

  11. System.out.println(student);

  12. }

  13. }

现在我们使用Spring IOC方式来做
先到刚刚建立的xml来配置
xml

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

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

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  5.  
  6. <!-- 该文件中产生的所有对象,被spring放入到了一个springIOC的容器中 -->

  7. <!-- id:唯一标识符 class:指定类型 -->

  8. <bean id = "student" class = "entity.Student">

  9. <!-- property:该class所代表的类的属性 -->

  10. <property name="stuNo" value = "002"></property>

  11. <property name="stuName" value = "ls"></property>

  12. <property name="stuAge" value = "20"></property>

  13. </bean>

  14.  
  15. </beans>

  16.  

在对Test测试类进行修改

下面是springIOC容器的工作原理:

//执行从springIOC容器中获取一个 id为student的对象
Student student = (Student)conext.getBean(“student”) ;
可以发现,springioc容器 帮我们new了对象,并且给对象赋了值

### Spring Cloud 安装配置教程 #### 1. 搭建环境准备 为了顺利安装配置 Spring Cloud,需先准备好开发环境。确保已安装 JDK 和 Maven,并设置好环境变量。 #### 2. 创建 Spring Cloud Config Server 创建一个新的 Maven 工程作为配置服务器的基础工程[^1]。在 `pom.xml` 文件中加入必要的依赖项来支持 Spring Cloud 功能: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> ``` 接着,在启动类上添加 `@EnableConfigServer` 注解启用配置服务功能。 #### 3. 配置 Git 存储库路径 通过修改 application.properties 或者 yml 文件中的属性 spring.cloud.config.server.git.uri 来指定远程Git仓库地址用于存储微服务应用的外部化配置文件[^2]。例如: ```properties spring.cloud.config.server.git.uri=https://github.com/your-repo/config-repo spring.cloud.config.server.git.search-paths=SpringcloudConfig ``` 这会使得 Config Server 只会在名为 "SpringcloudConfig" 的目录下寻找配置文件。 #### 4. 启动并测试 Config Server 完成上述步骤之后就可以运行应用程序了。访问 http://localhost:8888/{application}/{profile} 即可获取对应的配置信息。 #### 5. 使用 Spring Cloud Bus 实现热部署刷新配置 为了让客户端能够动态感知到最新的配置变化而无需重启服务实例,可以引入 Spring Cloud Bus 组件实现消息总线机制。只需简单地向 pom 中增加如下依赖即可开启此特性: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> ``` 随后利用 POST 请求发送 `/actuator/bus-refresh` 命令触发全网范围内的配置重载操作。 #### 6. 版本兼容性说明 当前文档所描述的内容适用于 Spring Cloud Greenwich 版本及其以上版本,该版本基于 Spring Boot 2.1.7 构建而成[^3]。对于不同版本间的差异,请参照官方迁移指南进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值