Spring学习心得(1)-- 简单开发环境搭建

去官网下载开发包。

一,我的项目结构

二,搭建准备

导入jar包:spring.jar,commons-logging.jar,log4j-1.2.15.jar,junit-4.4.jar

日志文件放入src目录下:log4j.properties

spring配置文件放入src目录下:applicationContext.xml

三,创建HelloWorld类

将要纳入spring管理的类对象

  1. package com.lanhuigu.spring.action;  
  2.   
  3. public class HelloWorld {  
  4.     private String msg;  
  5.     //通过set方法注入属性值  
  6.     public void setMsg(String msg) {  
  7.         this.msg = msg;  
  8.     }  
  9.       
  10.     public String getMsg() {  
  11.         return msg;  
  12.     }  
  13.   
  14. }  
四,创建spring配置文件applicationContext.xml

将HelloWorld对象纳入spring管理,创建一个惟一id为HelloWorld的bean对象

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!--  
  4.   - Application context definition for JPetStore's business layer.  
  5.   - Contains bean references to the transaction manager and to the DAOs in  
  6.   - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").  
  7.   -->  
  8. <beans xmlns="http://www.springframework.org/schema/beans"  
  9.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  10.         xmlns:aop="http://www.springframework.org/schema/aop"  
  11.         xmlns:tx="http://www.springframework.org/schema/tx"  
  12.         xsi:schemaLocation="  
  13.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  14.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  15.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  16.     <!-- 定义一个id为HelloWorld的bean -->  
  17.     <bean id="HelloWorld" class="com.lanhuigu.spring.action.HelloWorld">  
  18.         <!-- 将变量msg值依赖注入 -->  
  19.         <property name="msg">  
  20.             <value>HelloWorld</value>  
  21.         </property>  
  22.     </bean>  
  23. </beans>  
五,测试程序

读取spring配置文件,根据bean对象id为HelloWorld获取HelloWorld对象,调用对象的属性方法

  1. package com.lanhuigu.spring.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import com.lanhuigu.spring.impl.ISayHello;  
  8.   
  9. public class TestHelloWorld {  
  10.     @Test  
  11.     public void testMyHelloWorld(){  
  12.         //1.读取spring初始化的配置文件  
  13.         ApplicationContext acxt =   
  14.                 new ClassPathXmlApplicationContext("/applicationContext.xml");  
  15.         //2.根据bean获取ISayHello实现类对象  
  16.         ISayHello sayHello = (ISayHello) acxt.getBean("sayHello");  
  17.         //3.调用接口方法  
  18.         System.out.println(sayHello.doSay());  
  19.     }  
  20. }  

六,输出结果

HelloWorld

七,总结

spring使用步骤:

(1)创建项目导入必须jar包

(2)加入日志文件log4j.properties,spring配置文件applicationContext.xml

(3)创建业务接口或类,纳入spring管理

(4)读取spring配置文件,根据bean对象id拿到我们的接口或类对象,使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建Spring Cloud开发环境,你需要进行以下步骤: 1. 首先,你需要确保你的Java开发环境已经安装和配置好了。你可以去Oracle官网下载和安装Java Development Kit (JDK)。 2. 接下来,你需要使用构建工具(比如Maven或Gradle)来构建你的项目。在你的项目的pom.xml(如果使用Maven)或build.gradle(如果使用Gradle)文件中,你需要添加Spring Cloud的相关依赖。你可以在Maven Central Repository搜索并添加Spring Cloud的依赖,其中包括中提到的`org.springframework.cloud:spring-cloud-config-server`和中提到的`org.springframework.cloud:spring-cloud-config-client`。 3. 在你的项目中,你需要创建一个Spring Cloud Config Server。这是一个服务,用于管理和提供配置信息给其他应用程序。你可以使用Spring Boot创建一个新的Spring Cloud Config Server应用程序,并在其配置文件中指定一些基本设置,比如配置文件存储库的位置和访问控制规则。 4. 接下来,你需要创建一个Spring Cloud Config Client应用程序。这是真正的应用程序,它将从Config Server获取配置信息并使用它们。你可以使用Spring Boot创建一个新的Spring Cloud Config Client应用程序,并在其配置文件中指定Config Server的URL以及应用程序需要的其他配置属性。 5. 最后,你可以根据需要创建其他的Spring Cloud组件,比如服务注册与发现(如Spring Cloud Netflix Eureka),负载均衡(如Spring Cloud Netflix Ribbon)等等。根据你的具体需求,你可以在你的项目中添加这些组件。 综上所述,搭建Spring Cloud开发环境的步骤包括安装Java开发环境、配置构建工具、添加Spring Cloud依赖、创建Config Server和Config Client应用程序,以及根据需要添加其他的Spring Cloud组件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值