Spring 从何学起 要学,但是左看右看看了些资料.一个直观带说明的例子或者最好.请不吝啬赐教. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 从hello world开始学吧:),从网上找的代码,自己配成功了。HelloWorldClient.javapackage com.openv.spring;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;public class HelloWorldClient { protected static final Log log = LogFactory.getLog(HelloWorldClient.class); public HelloWorldClient() { Resource resource = new ClassPathResource("appcontext.xml"); BeanFactory factory = new XmlBeanFactory(resource); HelloWorld hw = (HelloWorld) factory.getBean("fileHelloWorld"); log.info(hw.getContent()); } public static void main(String[] args) { new HelloWorldClient(); }}HelloWorld.javapackage com.openv.spring;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class HelloWorld { protected static final Log log = LogFactory.getLog(HelloWorld.class); private HelloStr hStr; public HelloWorld(HelloStr hStr) { this.hStr = hStr; } public String getContent() { return hStr.getContent(); }}HelloStr.javapackage com.openv.spring;public interface HelloStr { public String getContent();}FileHelloStr.javapackage com.openv.spring;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class FileHelloStr implements HelloStr { protected static final Log log = LogFactory.getLog(FileHelloStr.class); private String propfilename; public FileHelloStr(String propfilename) { this.propfilename = propfilename; } public String getContent() { String helloworld = ""; try { Properties properties = new Properties(); InputStream is = getClass().getClassLoader().getResourceAsStream( propfilename); properties.load(is); is.close(); helloworld = properties.getProperty("helloworld"); } catch (FileNotFoundException ex) { log.error(ex); } catch (IOException ex) { log.error(ex); } return helloworld; }}appcontext.xml(放在src根目录下)<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean name="fileHelloWorld" class="com.openv.spring.HelloWorld"> <constructor-arg> <ref bean="fileHello"/> </constructor-arg> </bean> <bean name="fileHello" class="com.openv.spring.FileHelloStr"> <constructor-arg> <value>helloworld.properties</value> </constructor-arg> </bean> </beans>helloworld.properties(放在src根目录下)helloworld = "Hello World!!"build.xml(放在项目根目录下)<?xml version="1.0"?><project name="example4" default="run" basedir="."> <path id="lib"> <fileset dir="./lib"> <include name="spring.jar"/> <include name="commons-logging.jar"/> </fileset> </path> <target name="run" depends="compile" description="Run HelloWorldClient"> <java classname="com.openv.spring.HelloWorldClient" fork="yes"> <classpath refid="lib"/> <classpath path="classes"/> </java> </target> <target name="compile"> <mkdir dir="classes"/> <javac destdir="classes" source="1.5" target="1.5" deprecation="false" optimize="false" failonerror="true"> <src path="src"/> <classpath refid="lib"/> </javac> <copy todir="classes"> <fileset dir="src"> <include name="helloworld.properties"/> <include name="appcontext.xml"/> </fileset> </copy> </target></project> spring2.0 的包哪里下?需要哪些包?我正是想做个HelloWorld 这篇文章不错,讲的 Spring IOC 部分http://hi.baidu.com/iiitom/blog/item/86c09fa15804cc8b471064b2.html 关于servlet的一个问题 网站初建,望多提宝贵意见 JSTL和JavaBean的问题 紧急~~JSP问题~~ 控件的名字不能以数字开头,是么? 一个javabean调用另一个javabean中的方法? 在线等待,如何根据session id 取出 session出来。 ejb事务问题? 如何在web页面上实现统计图表?? jsp页面在提交时如何隐藏传递的参数? 问一下在server.xml里设置虚拟目录与直接在webapps目录下建立目录的区别是什么? 那位老大用过tmax公司的jeus服务器啊?看看这个错误是什么原因引起的?
HelloWorldClient.java
package com.openv.spring;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;public class HelloWorldClient {
protected static final Log log = LogFactory.getLog(HelloWorldClient.class); public HelloWorldClient() {
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloWorld hw = (HelloWorld) factory.getBean("fileHelloWorld");
log.info(hw.getContent());
} public static void main(String[] args) {
new HelloWorldClient();
}
}HelloWorld.java
package com.openv.spring;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;public class HelloWorld {
protected static final Log log = LogFactory.getLog(HelloWorld.class); private HelloStr hStr; public HelloWorld(HelloStr hStr) {
this.hStr = hStr;
} public String getContent() {
return hStr.getContent();
}
}HelloStr.java
package com.openv.spring;public interface HelloStr {
public String getContent();
}FileHelloStr.java
package com.openv.spring;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;import java.util.Properties;public class FileHelloStr implements HelloStr {
protected static final Log log = LogFactory.getLog(FileHelloStr.class); private String propfilename; public FileHelloStr(String propfilename) {
this.propfilename = propfilename;
} public String getContent() {
String helloworld = ""; try {
Properties properties = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream(
propfilename);
properties.load(is);
is.close();
helloworld = properties.getProperty("helloworld");
} catch (FileNotFoundException ex) {
log.error(ex);
} catch (IOException ex) {
log.error(ex);
} return helloworld;
}
}appcontext.xml(放在src根目录下)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <bean name="fileHelloWorld"
class="com.openv.spring.HelloWorld">
<constructor-arg>
<ref bean="fileHello"/>
</constructor-arg>
</bean> <bean name="fileHello"
class="com.openv.spring.FileHelloStr">
<constructor-arg>
<value>helloworld.properties</value>
</constructor-arg>
</bean>
</beans>helloworld.properties(放在src根目录下)
helloworld = "Hello World!!"build.xml(放在项目根目录下)
<?xml version="1.0"?>
<project name="example4" default="run" basedir=".">
<path id="lib"> <fileset dir="./lib">
<include name="spring.jar"/>
<include name="commons-logging.jar"/>
</fileset>
</path> <target name="run" depends="compile" description="Run HelloWorldClient"> <java classname="com.openv.spring.HelloWorldClient" fork="yes">
<classpath refid="lib"/>
<classpath path="classes"/>
</java> </target> <target name="compile"> <mkdir dir="classes"/> <javac destdir="classes" source="1.5" target="1.5"
deprecation="false" optimize="false" failonerror="true">
<src path="src"/>
<classpath refid="lib"/>
</javac>
<copy todir="classes">
<fileset dir="src">
<include name="helloworld.properties"/>
<include name="appcontext.xml"/>
</fileset>
</copy>
</target>
</project>
这篇文章不错,讲的 Spring IOC 部分http://hi.baidu.com/iiitom/blog/item/86c09fa15804cc8b471064b2.html