spring环境搭建

在做ssh环境搭建时,先搭建spring和hibernate的环境搭建,在搭建struts环境.

好处:在搭建struts环境之前出的错和web容器无关。

一.引入配置文件

  1:从已有的配置文件复制过来

  2:通过xml模板新建配置文件:

    a:首先进行applicationContext.xml文件提示配置(下次再配置可省略此步骤):

    Window----preferences----输入xml----选择xml Catalog----add配置

 

    

    b:右键new---XML(Basic Templates)---改名applicationContext---Create XML file from an XML schema file---

     Select XML Category entry 选中已配好的配置文件---Root element 选中beans,prefix去掉前缀p---finish

       配置文件如下:

      

二.添加相应的spring jar包

  1.直接把需要的jar包复制到WEB-INF下的lib文件夹下,jar包自动同步到Libraries下的Web App Libraries包下

      

    此种方式jar包会同步到项目中,部署时也会同步到tomcat中.

    

 

 

  2.从自己的用户库的中选中相应的jar包

    右键build path

    然后在用户库中选中自己事先配置好的jar包,如下:

    

     此种方式jar包不会同步到项目中,但部署时会同步到tomcat中

    

 

    注意:添加用户库时如果在此处打钩代表系统库,则jar包不会同步到服务器,这是开发模式才用到的.

 

 

三.创建一个date对象检测环境

  1,在applicationContext.xml中创建Date类的代理

<?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-3.2.xsd ">
    
    <bean id="date" class="java.util.Date"/>
    
</beans>

  2.建一个测试类

package cn.it.ssh;
import java.util.Date;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 
 * @author Administrator
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SSHTest {
    @Resource
    private Date date;
    @Test
    public void date(){
        System.out.println(date);
    }
    
}

 

 运行出现则代表环境OK.

 

  关于出现的注解解释:

   @RunWith(SpringJUnit4ClassRunner.class):

     让测试运行于Spring测试环境, Spring框架在org.springframework.test.annotation 包中提供了常用的Spring特定的注解集,如果你在Java5或以上版本开发,可以在测试中使用它。

   @ContextConfiguration(location="classpath:applicationContext.xml"):

    用来指明Spring配置文件的位置,含义:用来读取src和web-inf下的applicationContext.xml文件.

    如果有多个Spring配置文件 location="classpath:applicationContext-*.xml"

   

 

posted @ 2017-01-28 21:24  yf066083  阅读(150)  评论(0编辑  收藏  举报