<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>