发表于: 2017-08-15 21:52:03

1 1088


今天完成的事情:

学习maven聚合项目

聚合项目给我的感觉就是一个项目拆分成多个模块然后再让他们联系在一起


在项目里建立module,分为core(核心),service(服务),web(客户端)

其中core放的是module层和serivce接口
他们被多次复用到,所以直接放在核心层,然后在其他model依赖中添加core的依赖即可使用.
结构是这样的


接下来是service的module,里面包含了原本项目中的大部分代码,例如dao层,实现类.配置文件有mapper,jdbc,log4j,mybatis
还有一个test类,运行前需要先启动service里的test,开启服务


mybatis要配置好RMI

pom.xml文件里写上大部分功能的依赖,然后再依赖上core

最后是web的module
里面放控制器和spring mvc和web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0" >


  <display-name>Archetype Created Web Application</display-name>

<!--<context-param>-->

<!--<param-name>contextConfigLocation</param-name>-->
<!--<param-value>classpath:spring-mybatis.xml</param-value>-->
<!--</context-param>-->


        <!--spring监听器-->
        <!--class定义listener类名(实现)-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

        <!--防止spring内存溢出监听器,比如quartz-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

        <!--spring mvc servlet-->
        <!--对客户端请求的静态资源如图片、js文件等的请求交由默认的servlet进行处理-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<!--存疑,这里不应该是对应的class文件,包名+类名么-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--指定当web应用启动时,装载servlet的次序.当值为整正数或0时,servlet容器先加载数值较小的servlet,再依次加载其他数值大的servlet.当值为负或未定义,servlet容器将在web客户首次访问这个servlet时加载它-->
<load-on-startup>1</load-on-startup>
<!--<async-supported>true</async-supported>-->
</servlet>

<servlet-mapping>
<!--与servlet中的name要一致-->
<servlet-name>SpringMVC</servlet-name>
<!--此处也可以配置成*.do形式-->
<!--浏览器访问网址-->

<url-pattern>/</url-pattern>
</servlet-mapping>

        <!--欢迎文件页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


问了下师兄,假如原有项目有util,打算放在这里面应该放在哪
放在web里面,因为一般使用都在web里修改东西,而core和service放的是增删改查和数据库,一般不会改动
配置上去tomcat,在clean install打包的时候把报错了:
[ERROR] Failed to execute goal on project Hello-service: Could not resolve dependencies for project model:Hello-service:jar:1.0-SNAPSHOT: Failed to collect dependencies at model:hello-core:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for model:hello-core:jar:1.0-SNAPSHOT: Could not find artifact model:model:pom:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]

[ERROR] For more information about the errors and possible solutions, please read the following articles:


大概意思是找不到这个jar包.解决方法是先把主项目clean install一遍,再依次打包core-service-web.
成功

然后直接运行tomcat,报错异常为:


[WARN][RMI TCP Connection(3)-127.0.0.1][2017-08-15 14:46:07][org.springframework.beans.factory.support.AbstractBeanFactory] - Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StudentService' defined in class path resource [spring-mvc.xml]: Invocation of init method failed; nested exception is org.springframework.remoting.RemoteLookupFailureException: Lookup of RMI stub failed; nested exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:


大致意思为找不到springmvc里的StudentService,RMI请求失败

解决方法是:先开启service的服务,然后再运行tomcat就跑成功了,原因是tomcat运行时是默认自动跑那个配置的,好像有一串代码能选择是否在tomcat运行时直接跑配置,这个东西我不知道,师兄也忘记了.暂时不去管它


发生这种情况的原因
1:没有开启服务端
2:服务端端口不同



第二条成功
然后写两个service,起先是我写了一个新的类,在类名后+1,其他完全不变,然后在mybatis中添加第二个服务端.
结果报错了,然后询问了师兄,师兄说可以只有一个类,然后暴露出两个方法



成功

最终代码详见svn上,一步一步的加代码和功能都有



明天计划的事情:

做完最后任务8的第四条,完成任务8

然后整理项目,不懂的地方打上注释,搞一个plus型项目



遇到的问题:

如上



收获:

如上,感谢志勇师兄指点!


返回列表 返回列表
评论

    分享到