spring原理案例-基本項目搭建 03 創建工程運行測試 spring ioc原理實例示例


下面開始項目的搭建

使用 Java EE - Eclipse 新建一 Dynamic Web Project

7d8decd6-cbab-4618-8fd6-148550868db6

b1e45657-a918-49f9-8591-ef4c55fec14e

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

c3c3c5ac-223b-40a6-850c-07947d50fe5c

點擊Next

Configure web module settings 對話框勾選 Generate web.xml deployment descriptor 選項:

7890f292-047e-451e-8970-fb3e7eedf28d

然后點擊finish完成

 

079e6ac5-9993-4fbc-9fde-ee4f7aef6b6f

這幾個包或許是需要最少的包

-------------------------------------------------------

3.下面開始部署

把所需要的jar包ctrl c    ctrl v粘貼到lib目錄

然后  添加進來

0d26f02e-d14a-4062-a154-da391b62bff4

添加完的效果

0aabf9f2-557e-4de0-bf7b-34bbb4023c55

然后新建兩個類

    一個實體類-----------------HelloWorldSpringBean

    一個測試類(包含main函數)----------HelloWorldSpring

新建配置文件 -----------helloWorldSpring.xml

ead0ecdc-c4b4-4c9e-b9fc-4cf4569f04d3

具體如下:

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">修改為這個重新運行,可以打開

2c77a4c7-7071-41db-b38f-a7eb908b1bb1

最終的代碼為:

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

spring原理案例-基本項目搭建 01 spring framework 下載 官網下載spring jar包

spring原理案例-基本項目搭建 02 spring jar包詳解 spring jar包的用途

spring原理案例-基本項目搭建 03 創建工程運行測試 spring ioc原理實例示例

springmvc整合mybatis完整項目示例

springmvc 項目完整示例01 需求與數據庫表設計 簡單的springmvc應用實例 web項目

springmvc 項目完整示例02 項目創建-eclipse創建動態web項目 配置文件 junit單元測試

springmvc 項目完整示例03 小結

springmvc 項目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql語句 mybatis應用

springmvc 項目完整示例05  日志 --log4j整合 配置 log4j屬性設置 log4j 配置文件 log4j應用

springmvc 項目完整示例06 日志–log4j 參數詳細解析 log4j如何配置

springmvc 項目完整示例07 設置配置整合springmvc springmvc所需jar包springmvc web.xml文件配置

springmvc 項目完整示例08 前台頁面以及知識點總結

maven項目整合springmvc整合mybatis

eclipse 創建maven 項目 動態web工程完整示例

eclipse 創建maven 項目 動態web工程完整示例 maven 整合springmvc整合

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM