Spring教程

Spring教程

注解和XML优缺点比较

就目前Java web 开发应用中都能见到用xml作为配置的身影。在常用的框架中如:struts、spring mvc、hibernate、mybites等这些框架中(早期版本表现更为突出)都有xml配置。

一、XML优点

1:xml是集中式的元数据,不需要和代码绑定的;

在我们开发中,xml配置文件和代码类是区分开的。不需要绑定到代码中

2:使用xml配置可以让软件更具有扩展性;

比如,我们在spring中,我们不想使用接口而是想用接口的实现类,这个时候只需要修改xml配置中bean的class值就可以了。

3:对象之间的关系一目了然;

比如,我们在基于xml配置读取配置信息,如下图:


从xml结构中,我们就可以看出,在popertyPlaceholderConfigure类里面有个locations的属性,而且是list集合。

再比如,使用xml配置数据源的是:

 

DataSource对象的属性一目了然。

4:xml定义:可扩展标记语言,标准通用标记语言的子集,简称XML。

从这个定义中我们可以发现,xml最大的优势就在于,开发者(程序员)能够为软件量身定做使用的标记,使得xml更通俗易懂;

5:成熟的校验机制,来保证正确。可以使用Schema或者是DTD来对xml的正确性进行校验。

6:基于xml配置的时候,只需要修改xml即可,不需要对现有的程序进行修改。

7:容易与其他系统进行数据交互。数据共享方便

二、XML缺点

虽然上面列出了很多优点,但是xml也有缺点

1:应用程序中如果使用了xml配置,需要解析xml的工具或者是是第三方类库的支持;

2:解析xml的时候必然会占用资源,势必会影响到应用程序的性能;

以java为例,无论是将xml一次性装置到内存中,还是一行一行读取解析的,都会占用资源的。

3:xml配置文件过多,会导致维护变得困难

4:在程序编译期间无法对其配置项的正确性进行验证,只能在运行期发现。

5:出错后,排错变得困难。往往在配置的时候,一个手误就会出现莫名其妙的错误(虽然事出必有妖,但是排查真难);

比如,xml配置bean信息的时候,如果class的值带有空格,这种不好检查的,是比较麻烦的。排查起来很费事。

6:开发的时候,既要维护代码又要维护配置文件,使得开发的效率降低;

7:代码与配置项之间有时候会存在很多“潜规则”.改变了任意一方,都有可能影响到另一方的使用。这是个大坑

比如:自定义的标记,如果其他开发不清楚这些的话,修改了无论是代码还是xml的配置,都会导致程序不能正常运行。

8:开发工具对xml的验证支持的不是很好。

比如idea,对xml正确性,如果是自定义的,验证就不是很好。

说完xml的优缺点,我们在来看看注解的优缺点

三、注解优点

1:注解的解析可以不依赖于第三方库,可以之间使用Java自带的反射

2:注解和代码在一起的,之间在类上,降低了维护两个地方的成本

3:注解如果有问题,在编译期间,就可以验证正确性,如果出错更容易找

4:使用注解开发能够提高开发效率。不用多个地方维护,不用考虑是否存在“潜规则”

四、注解缺点

1:修改的话比较麻烦。如果需要对注解进行修改的话,就需要对整个项目重新编译

2:处理业务类之间的复杂关系,不然xml那样容易修改,也不及xml那样明了

3:在程序中注解太多的话,会影响代码质量,代码简洁会有影响

4:如果后来的人对注解不了解,会给维护带来成本

5:注解功能没有xml配置齐全

五、简单总结下两者优缺点比较

(1)注解优点:

  • 简化配置
  • 使用起来直观且容易,提升开发的效率
  • 类型安全,容易检测出问题

(2)注解缺点:

  • 修改起来比xml麻烦
  • 如果不项目不了解,可能给开发和维护带来麻烦

(3)Xml优点:

  • 把类与类之间松解偶;修改方便;容易扩展
  • 容易和其他系统进行数据交互
  • 对象之间的关系一目了然

(4)Xml缺点:

  • 配置冗长,需要额外维护;影响开发效率
  • 类型不安全,校验不出来,出错不好排查
  • 注解简单概括:写起来比较简单、方便,看起来也简洁,但是修改麻烦
  • Xml配置概括:写起来比较灵活、修改方便,但是写和维护麻烦

SpingBoot教程

Springboot启动问题

加载数据库源

深入Spring Boot:怎样排查 Cannot determine embedded database driver class for database type NONE

Maven打包

一、原生Maven打包

  

<!-- <build > 主要用于编译设置 -->
<build>
    <!-- 定义打包成jar的名字 -->
    <!-- 这里如果不定义 , 打包成的jar名字格式为 : <artifactId> + <version> -->
    <finalName>thymeleaf</finalName>
    <plugins>
        <!--SpringBoot maven插件-->
        <!-- 可以将应用打成一个可执行的jar包 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 设置启动入口 -->
            <!-- manClass即使不配置 , SprinBoot也在打包的时候也清楚入口是哪个 , 其实不用配置 -->
            <configuration>
                <mainClass>com.bella.ThymeleafApplication</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

 

、everything.xml打包

 1、配置XML文件

(1)在resources添加assembly文件夹

(2)assembly文件夹添加XML文件

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3
          http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>tar</id>
    <formats>
        <format>tar</format>
    </formats>
    <files>
        <file>
<!--            application.properties文件所在位置-->
            <source>src/main/resources/config/application.properties</source>
<!--            打包到指定文件夹-->
            <outputDirectory>conf</outputDirectory>
            <filtered>true</filtered>
        </file>
    </files>
    <fileSets>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
            <excludes>
                <exclude>config/application.properties</exclude>
            </excludes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
        </dependencySet>
        <dependencySet>
            <outputFileNameMapping>${artifact.name}.${artifact.extension}</outputFileNameMapping>
            <includes>
                <include>${groupId}:${artifactId}</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

 

2、POM.XML添加配置

 

<build>
        <finalName>springboot-test</finalName>
        <plugins>
            <!-- 资源配置文件和代码分开打包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <!--注意这玩意从编译结果目录开始算目录结构 -->
                    <excludes>
                        <exclude>*/*.properties</exclude>
                        <exclude>*/*.yml</exclude>
                        <exclude>*/*.txt</exclude>
                        <exclude>*/*.json</exclude>
                        <exclude>*/*.sh</exclude>
                        <exclude>*/*.xml</exclude>
                        <exclude>*/*.sql</exclude>
                        <exclude>*/*.jar</exclude>
                        <exclude>static/</exclude>
                        <exclude>templates/</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <mainClass>${main.class}</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/resources/assembly/everything.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

 

 

 

posted @ 2020-08-10 10:29  北京漂流瓶  阅读(708)  评论(0编辑  收藏  举报