spring原理案例-基本專案搭建 03 建立工程執行測試 spring ioc原理例項示例
下面開始專案的搭建
使用 Java EE - Eclipse 新建一 Dynamic Web Project
Target Runtime 選 Apache Tomcat 7.0(不要選 Apache Tomcat 6.0,7 以後才支援 Servlet 3.0)。
點選 Next > 按鈕。
預設的 Source folders 配置如下:
ps:可以根據需求自己編輯比如
刪除預設的,增加以下四個並修改預設的輸出目錄為 WebContent\WEB-INF\classes:
src/main/java
src/main/resources
src/test/java
src/test/resources
點選Next
Configure web module settings 對話方塊勾選 Generate web.xml deployment descriptor 選項:
然後點選finish完成
這幾個包或許是需要最少的包
-------------------------------------------------------
3.下面開始部署
把所需要的jar包ctrl c ctrl v貼上到lib目錄
然後 新增進來
新增完的效果
然後新建兩個類
一個實體類-----------------HelloWorldSpringBean
一個測試類(包含main函式)----------HelloWorldSpring
新建配置檔案 -----------helloWorldSpring.xml
具體如下:
HelloWorldSpringBean
package chapter2.HelloWorldSpring; public class HelloWorldSpringBean { private String hello; public String getHello(){ return hello; } public void setHello(String hello){this.hello=hello; } public void show(){ System.out.println("--message--"+getHello()); } }
一個屬性,
以及對應的get set方法
還有執行方法
HelloWorldSpring
package chapter2.HelloWorldSpring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class HelloWorldSpring { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new FileSystemXmlApplicationContext("src/helloWorldSpring.xml"); HelloWorldSpringBean helloWorldSpringBean = (HelloWorldSpringBean)ctx.getBean("myHelloWorld"); helloWorldSpringBean.show(); } }
helloWorldSpring.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="myHelloWorld" class="chapter2.HelloWorldSpring"> <property name="hello"> <value>hello World spring!</value> </property> </bean> </beans>
HelloWorldSpring直接run as application 執行,報錯
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 2 in XML document from file [D:\ProgramData\Workspaces\eclipse\chapter2\src\helloWorldSpring.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; 不允許有匹配 "[xX][mM][lL]" 的處理指令目標。
配置檔案開頭不能有其他內容空格或者空行等,如果有的話就會報錯
XML沒有以<?xml version="1.0" encoding="UTF-8"?> 開頭,也就是說第一個字元必須是<?xml......
解決方法:
規範的XML格式、
<?xml version="1.0" encoding="UTF-8"?> 必須是XML檔案的第一個元素且前面不能空格。
修改後繼續報錯,錯誤內容為
十一月 10, 2015 5:50:10 下午 org.springframework.context.support.FileSystemXmlApplicationContext refresh 警告: Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [chapter2.HelloWorldSpring] for bean with name 'myHelloWorld' defined in file [D:\ProgramData\Workspaces\eclipse\chapter2\src\helloWorldSpring.xml]; nested exception is java.lang.ClassNotFoundException: chapter2.HelloWorldSpring at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1351) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:628) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597) at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1444) at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:974) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:752) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140) at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84) at chapter2.HelloWorldSpring.HelloWorldSpring.main(HelloWorldSpring.java:12) Caused by: java.lang.ClassNotFoundException: chapter2.HelloWorldSpring
類找不到,發現是配置檔案中的class中寫錯了,沒有寫好類名
class="chapter2.HelloWorldSpring.HelloWorldSpringBean">修改為這個重新執行,可以開啟
最終的程式碼為:
package chapter2.HelloWorldSpring; public class HelloWorldSpringBean { private String hello; public String getHello(){ return hello; } public void setHello(String hello){ this.hello=hello; } public void show(){ System.out.println("--message--"+getHello()); } } --------------------------------------------------------- package chapter2.HelloWorldSpring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import chapter2.HelloWorldSpring.HelloWorldSpringBean; public class HelloWorldSpring { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new FileSystemXmlApplicationContext("src/helloWorldSpring.xml"); HelloWorldSpringBean helloWorldSpringBean = (HelloWorldSpringBean)ctx.getBean("myHelloWorld"); helloWorldSpringBean.show(); } } ----------------------------------------------------------------------------------- <?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="myHelloWorld" class="chapter2.HelloWorldSpring.HelloWorldSpringBean"> <property name="hello"> <value>hello World spring!</value> </property> </bean> </beans>
spring原理 實踐解析-簡單的helloworld
springmvc整合mybatis完整專案示例
maven專案整合springmvc整合mybatis