spring搭建一

来源:互联网 发布:windows内存占用高 编辑:程序博客网 时间:2024/05/24 04:17

自己学习搭建spring的过程:

首先写接口类:

public interface Knight {
    
    public void embarkOnQuest();
}

然后写实现类:


public class BraveKnight implements Knight{
    
    private Quest quest;
    
    public BraveKnight(Quest quest){
        this.quest=quest;
    }
    
    @Override
    public void embarkOnQuest() {
        
        System.out.println("begin.......");
        quest.embark();
        
        System.out.println("end............");
    }

}



Quest接口:

public interface Quest {
    
    void embark();
    
}

Quest实现类:


public class RescueDamselQuest implements Quest{

    @Override
    public void embark() {
        
        System.out.println("This is call RescueDamselQuest");
    }

}


接下来配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringTestProgect</display-name>
 
  <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      
   <!-- 可以自定义servlet.xml配置文件的位置和名称,调整路径-->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/resources/spring-servlet.xml</param-value> <!--这个路径是跟Class文件在同一目录下,spring-servlet.xml中写的是url及跳转到相应的类中-->
      </init-param>
      
      <load-on-startup>1</load-on-startup>
  </servlet>
 
   <!-- Spring配置 监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
 
  <!-- 指定spring bean的配置文件所在目录, -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/resources/applicationContext.xml</param-value>
  </context-param>
</web-app>



那么接下来写applicationContext.xml的内容是:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
    <import resource="/WEB-INF/resources/knights.xml"/>            
</beans>


knight.xml的内容是:

<?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.0.xsd">
     <bean id="knight" class="com.springaction.knights.impl.BraveKnight">
         <constructor-arg ref="quest"/>
     </bean>
    
     <bean id="quest" class="com.springaction.Quest.impl.RescueDamselQuest"></bean>
</beans>



测试类:

public class BraveKnightTest {
    
    @Test
    public void knightShouldEmbarkOnQuest(){
        
        Quest mockQuest=mock(Quest.class);
        
        BraveKnight knight=new BraveKnight(mockQuest);
        
        knight.embarkOnQuest();
    }
}


运行结果:

begin.......
end............



0 0
原创粉丝点击