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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值