说到组件式开发,比较抽象。打个比方,就好像是积木,散着的时候,各个是个独立的东西,但是通过一系列的组合,他就会成为一个新的整体。java web追求的是解耦,这样的一种组件式开发,其实也完成了这个功能。程序员是独立的,每个程序员有自己的特长与喜好,假如组件分为dao组件,service组件,以及web前端组件,把所有喜欢和数据库打交道的程序员分到dao组件下,所有喜欢业务的程序员分到service组件下等等。只要接口不变,每个程序员都不用关心别的组件到底是怎么开发的,更不用关心别的组件是用的什么框架。
下图是一个图片说明:
理想是美好的,那能不能实现呢?一般来说,项目都是MVC结构,而且大部分项目都会使用SSH或者SSM架构,通过spring把整个MVC紧密的结合在一起。代码上依赖注入实现解耦,但有个前提是你接口的实现类必须要由spring管理的。那么想要实现组件式,就不能采取这样的一种方式。因为组件是相互独立的,简单说就是两个完全不同的项目。
我们使用maven来构建项目。环境是jdk64位,tomcat7.0.61 64位,Eclipse luna64位。首先创建一个Maven Project作为父项目,起名叫module-parent。
为了方便下载,在pom文件里加上私服以及junit
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>scc-module</groupId>
<artifactId>module-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>module-parent</name>
<description>父项目</description>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<versionRange>[1.2,)</versionRange>
<goals>
<goal>resources</goal>
<goal>testResources</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://192.168.1.31:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://192.168.1.31:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
然后关键的来了,建立组件。先建一个maven module,取名叫module-dao
紧接着同上建立service的module,这里记得不能直接点finish,要点next,在packing那里选择war,以下是建好的目录结构
篇幅不够了,下篇说吧