编写基于Intellij2016.3与Java SDK1.8
下载Spring最新jar包:
http://repo.spring.io/release/org/springframework/spring
和Spring依赖的日志组件包
http://commons.apache.org/proper/commons-logging/
1、新建一个Module
上图如果选择不完整的话,可以如下添加依赖添加确实的jar包。
2、添加依赖
经测试,一个简单的HelloWorld程序也需要依赖如下5个包:
3、编写测试文件
HelloWorld.java
package com.xiya; /** * Created by N3verL4nd on 2017/3/4. */ public class HelloWorld { private String message; public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } }
MainApp.java
package com.xiya; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by N3verL4nd on 2017/3/4. */ public class MainApp { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); HelloWorld test = (HelloWorld) applicationContext.getBean("HelloWorld"); System.out.println(test.getMessage()); } }4、编写beans配置文件(src目录下)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="HelloWorld" class="com.xiya.HelloWorld"> <property name="message" value="Spring Hello World!" /> </bean> </beans>
一下对程序运行并没有影响,它的作用估计是方便查找xml文件之间的关系吧
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:161) at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:225) at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:88) at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:58) at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:61) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.xiya.MainApp.main(MainApp.java:11) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 13 more Process finished with exit code 1原因是少加入commons-logging-1.2包
添加方法同上。
测试: