spring跟着走

4 篇文章 0 订阅

<script type="text/javascript"> google_ad_client = "pub-5033576919944123"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2007-10-24: csdn.blog google_ad_channel = "8548491739"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

第一步:加包,依据自己工程需要加入相应包.

第二步:在web.xml里面加入
 
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:*.spring.xml</param-value>
 </context-param> 

第三步:*.spring.xml   如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

 <bean id="message" class="wtc.spring.Message"/>
 
 <bean id="ts" class="wtc.spring.TrySpring">
  <property name="msg" ref="message"/>
 </bean>
 
</beans>

第四步:写wtc.spring.Message和wtc.spring.TrySpring两个类

package wtc.spring;

public class Message {
 public void showMessage(){
  System.out.println("tgystc");
 }
}

package wtc.spring;

public class TrySpring {
 Message msg = null;

 public Message getMsg() {
  return msg;
 }

 public void setMsg(Message msg) {
  this.msg = msg;
 }
 
 public void showMessage(){
  msg.showMessage();
 }
}

第五步:测试

package wtc.spring;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

 @Before
 public void setUp() throws Exception {
 }

 @After
 public void tearDown() throws Exception {
 }
 
 @Test
 public void wtc() throws Exception {
  ApplicationContext context = new ClassPathXmlApplicationContext("*.spring.xml");
  TrySpring ts = (TrySpring) context.getBean("ts");
  ts.showMessage();
 }

}

web下采用:

 private WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletConfig().getServletContext());
 TrySpring ts = (TrySpring) wac .getBean("ts");
  ts.showMessage();

 


<script type="text/javascript"> google_ad_client = "pub-5033576919944123"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2007-10-24: csdn.blog google_ad_channel = "8548491739"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringSpring Cloud是两个相关的框架。Spring是一个开源的Java框架,用于构建Java应用程序。它提供了许多模块和工具,可以帮助开发者更轻松地构建企业级应用程序。Spring Boot是Spring框架的一部分,它是一个快速配置脚手架,可以帮助开发者快速搭建Spring应用程序。Spring Boot可以独立使用,也可以与Spring Cloud一起使用。 Spring Cloud是一个云端分布式架构解决方案,它是基于Spring Boot构建的。它提供了一系列的组件和工具,用于简化开发分布式系统的任务,比如服务发现、服务注册、负载均衡、配置管理等。Spring Cloud依赖于Spring Boot,因此无法独立使用,它扩展了Spring Boot的功能,使开发者可以更方便地构建和管理微服务架构。 总结起来,Spring Boot是一个快速配置脚手架,用于开发单个微服务,而Spring Cloud是基于Spring Boot的云应用开发工具,用于构建和管理分布式系统。Spring Cloud依赖于Spring Boot,因此需要在Spring Boot的基础上进行扩展。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【微服务系列】Spring SpringMVC SpringBoot SpringCloud概念、关系及区别](https://blog.csdn.net/weixin_33700350/article/details/93831564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值