spring跟着走


<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值