搜索词>>logback 耗时0.0050
  • springmvc集成logback不打印spring mvc启动日志解决

    springmvc集成logback不打印spring mvc启动日志解决<strong>问题:</strong><br />     springmvc集成logback不打印spring mvc启动日志<br /> <br /> <strong>Logback简介</strong><br />     Logback是在log4j基础上重写了内部的实现,在某些特定的场景上面,甚至可以比之前的速度快上10倍。在保证logback的组件更加快速的同时,同时所需的内存更加少。所以logback是一个不错的选择,当然你也可以选择其他的日志组件。<br /> <br /> <strong>1.为了使logback能打印Spring的日志这里需要添加几个依赖,默认logback是不会打印Spring的日志</strong> <pre> <code class="language-xml"> <!-- https://mvnrepository.com/artifact/org.logback-extensions/logback-ext-spring --> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.25</version> </dependency></code></pre> <strong>2.再次启动即可</strong><br /> <img alt="spring mvc启动日志" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-07/6174d1be296f43c3b87d5094858d4641.png" />
  • Spring Boot logback tomcat部署不生效问题解决

    Spring Boot logback tomcat部署不生效问题解决Spring Boot logback tomcat部署不生效问题解决
  • spring boot项目中使用logback日志

    spring boot项目中使用logback日志,USING LOGBACK WITH SPRING BOOT ,Logback让一个优秀的企业应用程序的日志框架——它是快速,简单但强大的配置选项,有一个小的内存占用。我在介绍性的文章,介绍logback.Logback简介:一个企业日志框架。在Logback一系列的文章中,我讨论了如何使用XML配置Logback和Groovy。可选用的Logback配置:使用XML和Logback配置:使用Groovy.<h2>  引言</h2> <p>    Logback让一个优秀的企业应用程序的日志框架——它是快速,简单但强大的配置选项,有一个小的内存占用。我在介绍性的文章,介绍logback.Logback简介:一个企业日志框架。在Logback一系列的文章中,我讨论了如何使用XML配置Logback和Groovy。可选用的Logback配置:使用XML和Logback配置:使用Groovy.</p> <p>    在这篇文章中,我将讨论如何使用Logback spring boot 。虽然有许多Java日志记录选项,默认的 spring boot 选择使用Logback记录器。像许多东西在春天的引导,Logback默认配置合理的默认值。开箱即用的,spring boot使得Logback易于使用。</p> <h2>详细介绍</h2> <h4> </h4> <h3>创建日志记录器</h3> <p>    在以前的文章,我写过一篇关于如何用spring boot创建一个web应用。这里将会为这个web应用配置logback日志。这个应用包含  IndexController我们将添加日志记录代码。IndexController的代码是这样的。</p> <pre> <code class="language-java">package guru.springframework.controllers;     import guru.springframework.helpers.SpringLoggingHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;   @Controller public class IndexController {     private final Logger logger = LoggerFactory.getLogger(this.getClass());     @RequestMapping("/")     String index(){         logger.debug("This is a debug message");         logger.info("This is an info message");         logger.warn("This is a warn message");         logger.error("This is an error message");         new SpringLoggingHelper().helpMethod();         return "index";     } }</code></pre> <p>让我们添加一个 SpringLoggingHelper类与应用程序日志代码。虽然这类不做任何事除了释放日志语句,它将帮助我们理解配置日志记录在不同的包中。这是SpringLoggingHelper 的代码:</p> <pre> <code class="language-java">package guru.springframework.helpers;     import org.slf4j.Logger; import org.slf4j.LoggerFactory;   public class SpringLoggingHelper {     private final Logger logger = LoggerFactory.getLogger(this.getClass());     public void helpMethod(){         logger.debug("This is a debug message");         logger.info("This is an info message");         logger.warn("This is a warn message");         logger.error("This is an error message");       } }</code></pre> <p>在上面的类中,我们写日志代码对SLF4J API。SLF4J是常用的外墙日志框架,如Java Util日志,Log4J 2和Logback。通过编写SLF4J,我们的代码仍然和Logback解耦,从而提供插件不同的日志框架的灵活性,以后如果需要。</p> <p>如果你想知道关于SLF4J和Logback依赖性,你不需要指定任何。spring boot包含它们。假设您正在使用Maven或Gradle管理你春天启动项目,必要的依赖关系是 spring boot 下的依赖关系的一部分。</p> <p>运行 SpringBootWebApplication主类。当应用程序启动时,从你的浏览器的URL访问它,<strong>http://localhost:8080</strong></p> <p>IntelliJ控制台的日志输出。</p> <p><img alt="在弹簧启动时使用默认配置记录输出" class="img-thumbnail" src="https://i2.wp.com/springframework.guru/wp-content/uploads/2016/04/logging_output_with_springboot-1024x266.png?resize=863%2C224&ssl=1" /></p> <p>我们还没有写任何Logback配置。输出的 IndexController和 SpringLoggingHelper从logback根记录器类。注意,调试消息没有得到记录。Logback默认情况下将日志消息调试水平。然而, spring boot 团队给我们提供了一个默认的配置在theSpring Logback启动默认Logback配置文件, 基地.xml。此外,Spring提供了提供两个预配置的输出源通过引导 控制台- - - - - -appender.xml和 文件- - - - - -appender.xml文件。的 基地.xml文件referencesboth。</p> <p>这是base.xml的代码,文件从spring-boot github获取。</p> <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?>   <!-- Base logback configuration provided for compatibility with Spring Boot 1.1 -->   <included> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml" /> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root> </included></code></pre> <p>在这里您可以看到,通过将根记录器设置为INFO,Spring Boot已经覆盖了Logback的默认日志记录级别 ,这是上面示例中没有看到调试消息的原因。我们将在下一节中看到,在Spring Boot中更改日志级别非常简单。</p> <br /> 查看我的免费简介春季课程 <h4>通过Spring Boot的application.properties文件进行配置</h4> <p>在Spring Boot应用程序中,您可以外部化配置以在不同的环境中使用相同的应用程序代码。application.properties。属性文件可能是最受欢迎的几种不同的方法来外部化Spring Boot配置属性。在Spring Boot Web应用程序的默认结构中,可以找到application.properties。资源文件夹下的 属性文件。在application.properties中。属性文件,您可以定义Spring Boot的日志级别,应用程序记录器,Hibernate,Thymeleaf等。您还可以定义一个日志文件以将日志消息写入到控制台之外。</p> <p>下面是一个示例 应用。属性文件与日志配置。</p> <pre> <code>logging.level.org.springframework.web=INFO logging.level.guru.springframework.controllers=DEBUG logging.level.org.hibernate=ERROR logging.file=logs/spring-boot-logging.log</code></pre> <p><br /> 当您现在运行主类并访问应用程序时,将来自IndexController和 SpringLoggingHelper的日志消息 记录到控制台和logs / spring - boot - logging 。日志文件。<strong>注意</strong>:还有一个 日志记录。path属性来指定日志记录文件的路径。如果你使用它,Spring Boot会创建一个spring。日志文件在指定的路径。但是,您不能同时指定 日志记录。文件和 日志记录。路径属性在一起。如果完成,Spring Boot将忽略两者。</p> <p><img alt="使用application.properties启动Spring启动日志输出" class="img-thumbnail" src="https://i2.wp.com/springframework.guru/wp-content/uploads/2016/04/logging_output_with_application_properties-1024x486.png?resize=863%2C410&ssl=1" /></p> <p>在输出中,请注意IndexController的调试和更高级别的消息 已记录到控制台和文件中。这是因为在application.properties中。属性文件中,我们指定 DEBUG作为日志级 大师。springframework的。控制器包, IndexController是其中的一部分。由于我们没有明确配置 SpringLoggingHelper类,所以默认配置为 base 。使用了xml文件。因此,只有 记录了SpringLoggingHelper的INFO和更高级别的消息 。</p> <p>当您需要获取特定类或包的更详细的日志消息时,可以看到这是多么简单。</p> <h4>通过外部文件进行回溯配置</h4> <p>通过application.properties回溯配置。属性文件将足够用于许多Spring Boot应用程序。然而,大型企业应用程序可能会有更复杂的日志记录要求。如前所述,Logback通过<a href="https://springframework.guru/logback-configuration-using-xml/" rel="external nofollow" target="_blank">XML</a>和Groovy配置文件支持高级日志记录配置。</p> <p>在Spring Boot应用程序中,您可以将Logback XML配置文件指定为 logback 。xml或 logback - spring 。xml在项目类路径中。然而,Spring Boot团队建议使用<strong>-spring</strong>变体进行日志记录配置,   logback - spring 。XML优于  的logback 。xml。如果使用标准 的logback 。xml配置,Spring Boot可能无法完全控制日志的初始化。</p> <p>这是logback的代码 - spring 。xml文件。</p> <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <configuration>     <include resource="org/springframework/boot/logging/logback/base.xml"/>     <logger name="guru.springframework.controllers" level="WARN" additivity="false">         <appender-ref ref="CONSOLE"/>         <appender-ref ref="FILE"/>     </logger>     <logger name="guru.springframework.helpers" level="WARN" additivity="false">         <appender-ref ref="CONSOLE"/>         <appender-ref ref="FILE"/>     </logger> </configuration></code></pre> <p><br /> 通过更新的Spring Boot Logback配置,我们的日志输出现在如下所示:在上面的配置代码中,我们包括了 基础。第3行中的xml文件,请注意,我们没有配置任何appender。相反,我们依靠由Spring Boot提供的 CONSOLE和 FILE appender。</p> <p><img alt="记录Spring引导XML配置的输出" class="img-thumbnail" src="https://i0.wp.com/springframework.guru/wp-content/uploads/2016/04/logging_output_xml_configuration-1024x473.png?resize=863%2C399&ssl=1" /></p> <p><strong>注</strong>:Spring Boot要求 的logback - Spring。xml配置文件要在类路径上。但是,您可以将其存储在不同的位置,并使用日志记录指向它 。config属性在application.properties中。属性。</p> <h4>spring boot配置文件在日志记录中</h4> <p>在本地机器上开发时,通常将日志级别设置为 DEBUG。这将为您提供详细的日志消息供您开发使用。在生产时,它的典型设置将日志级别设置为 WARN或以上。这是为了避免在生产过程中运行时使用过多的调试信息和日志记录来填充日志。虽然日志记录非常有效,但仍然有成本。</p> <p>Spring Boot通过使用< springProfile >元素扩展用于回溯配置的Spring配置文件来解决这些要求 。使用你这个元素 的logback - spring。xml文件,您可以根据活动的spring boot配置文件选择性地包括或排除日志记录配置部分。</p> <p><strong>注意</strong>:在回弹配置中支持 < springProfile >可从SpringBoot 1.3.0.M2里程碑开始。</p> <p>以下是使用活动的Spring boot配置文件配置Logback的XML示例。</p> <p><br />  </p> <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <configuration>     <include resource="org/springframework/boot/logging/logback/base.xml" />     <springProfile name="dev,staging">         <logger name="guru.springframework.controllers" level="DEBUG" additivity="false">             <appender-ref ref="CONSOLE" />         </logger>>     </springProfile>     <springProfile name="production">         <logger name="guru.springframework.controllers" level="WARN" additivity="false">             <appender-ref ref="FILE" />         </logger>     </springProfile> </configuration></code></pre> <p>要将配置文件传递给应用程序,请使用- Dspring运行 应用程序。档案。active = JVM参数。在上面的配置代码中,对于 dev和 staging配置文件,我们配置了上 师。springframework的。控制器记录器将DEBUG和更高级别的消息记录到 控制台。对于 生产配置文件,我们配置了相同的记录器来将WARN和更高级别的消息记录到 文件中。</p> <p>对于本地开发,在IntelliJ中,选择<strong>Run-> Edit Configurations</strong>,并在<strong>Run / Debug Configurations</strong>对话框中设置JVM参数,如下所示。</p> <p><img alt="设置用于登录IntelliJ的活动配置文件" class="img-thumbnail" src="https://i0.wp.com/springframework.guru/wp-content/uploads/2016/04/RunDebug_Configurations_Dialog.png?resize=755%2C261&ssl=1" /></p> <p>现在,当我们使用dev配置文件运行应用程序时 ,我们将看到以下日志输出。</p> <p><img alt="使用弹簧活动配置文件记录输出" class="img-thumbnail" src="https://i1.wp.com/springframework.guru/wp-content/uploads/2016/04/logging_output_springl_profile.png?resize=863%2C171&ssl=1" /></p> <p>在上面的输出中,观察IndexController的日志记录输出 。 根据dev配置文件的配置,DEBUG和更高的日志消息记录到控制台 。您可以使用生产配置文件重新启动应用程序, 以确保将 WARN和较高日志消息记录到该文件中。</p> <h4>配置文件的条件处理</h4> <p>Logback支持在Janino库的帮助下对配置文件进行条件处理。您可以使用配置文件中的 < if >, < then >和 <else >元素来定位多个环境。要执行条件处理,请将Janino依赖项添加到您的Maven POM中,像这样。</p> <pre> <code class="language-xml"><dependency>    <groupId>org.codehaus.janino</groupId>    <artifactId>janino</artifactId>    <version>2.7.8</version> </dependency></code></pre>   <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <configuration>     <include resource="org/springframework/boot/logging/logback/base.xml" />     <springProfile name="dev,staging">         <logger name="guru.springframework.controllers" level="DEBUG" additivity="false">             <appender-ref ref="CONSOLE" />         </logger>>     </springProfile>     <springProfile name="production">     <logger name="guru.springframework.controllers" level="WARN" additivity="false">         <appender-ref ref="FILE" />     </logger>     </springProfile>     <if condition='property("spring.profiles.active").contains("dev")'>         <then>             <logger name="guru.springframework.helpers" level="DEBUG" additivity="false">                 <appender-ref ref="CONSOLE" />             </logger>         </then>         <else>             <logger name="guru.springframework.helpers" level="WARN" additivity="false">                 <appender-ref ref="FILE" />             </logger>         </else>     </if> </configuration></code></pre>   <p>完整的logback - spring。这是具有条件处理逻辑的xml文件。</p> <p> </p> <p>在上面的代码中,我们在< if >元素中指定了一个条件, 以检查当前活动的配置文件是否包含 dev。如果条件求 值为true,则< then >元素中的配置代码将 执行。在 < then >元素中,我们配置了 大师。springframework的。帮助人员将 DEBUG和更高级的消息记录到控制台。我们使用 < else >元素来配置记录器来将WARN和更高的消息记录到 日志文件中。所述 < 别的>元素执行用于比其它任何配置文件 dev的。</p> <p>当您使用生产配置文件运行应用程序 并进行访问时,两个记录器都会将 WARN和更高的消息记录到日志文件中,与此类似。<br /> <img alt="使用生产配置文件记录输出" class="img-thumbnail" src="https://i0.wp.com/springframework.guru/wp-content/uploads/2016/04/logging_output_production_profile.png?resize=642%2C319&ssl=1" /></p> <p>对于 dev配置文件,两个记录器都会将 DEBUG和更高级的消息记录到控制台,类似于此。<br /> <img alt="记录dev配置文件的输出" class="img-thumbnail" src="https://i2.wp.com/springframework.guru/wp-content/uploads/2016/04/logging_output_dev_profile.png?resize=863%2C222&ssl=1" /></p> <h4>logback自动扫描问题与spring boot</h4> <p>在的logback - spring。xml文件,您可以通过设置scan = “true”属性来启用配置的自动扫描 。启用自动扫描后,Logback扫描配置文件中的更改。对于任何更改,Logback自动重新配置它们。您可以通过将时间段传递给scanPeriod属性来指定扫描周期 ,其值以毫秒,秒,分钟或小时为单位指定。<br /> 例如,此代码告诉Logback扫描 logback - spring 。xml每10秒钟。</p> <pre> <code class="language-xml"><configuration debug="true" scan="true" scanPeriod="10 seconds" >   ... </configuration></code></pre>   <pre> <code>//Error on using auto-scan with springProfile   -ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:39 - no applicable action for [springProfile],   current ElementPath  is [[configuration][springProfile]]   //Error on using auto-scan with springProperty   -ERROR in ch.qos.logback.core.joran.spi.Interpreter@12:125 - no applicable action for [springProperty],     current ElementPath  is [[configuration][springProperty]]</code></pre>   <p>Spring Boot Logback的一个限制是使用 springProfile和 springProperty,设置自动扫描结果为错误。</p> <p>由于不兼容性问题而发生错误。Spring Boot使用 JoranConfigurator子类支持 springProfile和 springProperty。不幸的是,Logback的 ReconfigureOnChangeTask不提供挂接插件。</p> <h4>结论</h4> <p>Logback的流行趋势在于开源社区。许多受欢迎的开源项目使用Logback来记录日志需求。 <a href="https://camel.apache.org/" rel="external nofollow" target="_blank" title="阿帕奇骆驼">Apache Camel</a>,<a href="http://gradle.org/" rel="external nofollow" target="_blank" title="毕业">Gradle</a>和<a href="http://www.sonarqube.org/" rel="external nofollow" target="_blank">SonarQube</a>只是几个例子。</p> <p>回溯显然具有处理复杂企业应用程序登录需求的能力。所以,难怪Spring Boot团队为默认的日志记录实现选择了Logback。正如你在这篇文章中看到的,Spring Boot团队提供了与Logback的良好集成。开箱即用,Logback可以随Spring Boot一起使用。在这篇文章中,您已经看到,随着日志需求的发展,在spring boot中配置Logback是多么容易。<br /> <br /> 完整例子1:<br />  </p> <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <!-- scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。 scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒当scan为true时,此属性生效。默认的时间间隔为1分钟。 debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 --> <configuration scan="false" scanPeriod="60 seconds" debug="false"> <property name="filePath" value="/aos/logs/axschoolweb.log" /> <property name="fileNamePattern" value="/aos/logs/axschoolweb" /> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${filePath}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${fileNamePattern}.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder> <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- level等级顺序:error>warn>info>debug。 --> <logger name="org.springframework" level="WARN"/> <logger name="org.springframework.web" level="DEBUG"/> <logger name="org.springframework.remoting" level="WARN"/> <logger name="org.springframework.scheduling.quartz" level="WARN"/> <logger name="org.springframework.data.jpa" level="WARN"/> <logger name="org.cometd" level="WARN"/> <logger name="ch.qos.logback" level="WARN"/> <logger name="jdbc.sqltiming" level="WARN"/> <logger name="com.leftso" level="DEBUG"><!--根据具体项目配置--> <appender-ref ref="rollingFile"/> </logger> <!-- root与logger是父子关系,没有特别定义则默认为root,任何一个类只会和一个logger对应, 要么是定义的logger,要么是root,判断的关键在于找到这个logger,然后判断这个logger的appender和level。 --> <root level="DEBUG"> <appender-ref ref="console"/> <appender-ref ref="rollingFile"/> </root> </configuration></code></pre> <br /> <br /> 20171124更新=============================> <h3>logback配置打印mybaties sql日志</h3> <pre> <code class="language-xml"><logger name="net.xqlee.project.demo.dao" ><!--这里配置自己的dao(mybaties mapper接口)的包路径--> <level value="DEBUG"/> <appender-ref>console</appender-ref><!--指定一种或多种logback日志记录器--> </logger></code></pre> 如上代码片段所示,logback打开mybaties的sql打印日志需要创建一个logger并配置mybaties的dao所在包。然后将日志的等级调制DEBUG即可。日志的记录方式和其他logger一样可以配置多个。
  • Spring Boot Logback 输出日志慢8小时解决

    找到encoder的pattern节点找到encoder的pattern节点。        <encoder>            <charset>UTF-8</charset>            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%-4relative] %-5level %logger{30} - %msg%n</pattern>        </encoder>在日期%d{yyyy-MM-dd HH:mm:ss} 里面添加时区,%d{yyyy-MM-dd HH:mm:ss,CTT} 其中(CTT代表上海)修改后如下:        <encoder>            <charset>UTF-8</charset>            <pattern>%d{yyyy-MM-dd HH:mm:ss,CTT} [%-4relative] %-5level %logger{30} - %msg%n</pattern>        </encoder> ​​​​​​​
  • spring boot 使用SLF4J Logging-Java编程

    Java编程之Spring Boot 使用SLF4J Logging,spring boot,SLF4J<h2>一、项目结构图</h2> <img alt="项目结构图" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-07/b33c0281356d4650ac605ee4715f5600.png" /><br /> 默认情况下,SLF4j日志记录包含在Spring boot包中。<br /> <strong>application.properties</strong> <pre> <code>spring-boot-web-project$ mvn dependency:tree +... +- org.springframework.boot:spring-boot-starter-logging:jar:1.4.2.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.7:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.1.7:compile [INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile [INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.21:compile [INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.21:compile +...</code></pre> <p><strong>请注意</strong><br /> <em>查看这个Spring Boot Logback XML模板,了解默认的日志记录模式和配置。</em></p> <h2>二、配置文件</h2> <h3>2.1application.properties<br /> 要启用日志记录,创建一个应用程序。在资源文件夹的根目录下的属性文件。<br /> 日志记录。级别-定义日志级别,日志记录将输出到控制台。<br /> <strong>application.properties</strong></h3> <pre> <code>logging.level.org.springframework.web=ERROR logging.level.com.mkyong=DEBUG</code></pre> logging.file -定义日志文件,日志将输出到一个文件和控制台。<br /> <strong>application.properties</strong> <pre> <code>logging.level.org.springframework.web=ERROR logging.level.com.mkyong=DEBUG #output to a temp_folder/file logging.file=${java.io.tmpdir}/application.log #output to a file #logging.file=/Users/mkyong/application.log</code></pre> <code>logging.pattern</code> -定义一个定制的日志记录模式。<br /> <strong>application.properties</strong> <pre> <code>logging.level.org.springframework.web=ERROR logging.level.com.mkyong=DEBUG # Logging pattern for the console logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n" # Logging pattern for file logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" logging.file=/Users/mkyong/application.log</code></pre> <h2><strong>application.yml</strong></h2> 这在YAML格式中是一样的<br /> <strong>application.yml</strong> <pre> <code>logging: level: org.springframework.web: ERROR com.mkyong: DEBUG pattern: console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n" file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n" file: /Users/mkyong/application.log</code></pre> <h2><strong>Classic Logback.xml</strong></h2> 如果您不喜欢Spring引导日志模板,只需创建一个标准的logback。xml在资源文件夹的根目录或类路径的根目录下。这将覆盖Spring引导日志模板。<br /> <strong>logback.xml</strong> <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="DEV_HOME" value="c:/logs" /> <appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${DEV_HOME}/debug.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} - %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <logger name="com.mkyong" level="debug" additivity="false"> <appender-ref ref="FILE-AUDIT" /> </logger> <root level="error"> <appender-ref ref="FILE-AUDIT" /> </root> </configuration></code></pre> <h2>Spring Boot logging by Profile</h2> <p>请注意<br /> 阅读本文-概要文件的配置<br />  </p> <p>创建一个logback-spring。在类路径的根目录中使用xml,以利用Spring引导提供的模板特性。</p> <p>在以下的例子:</p> <p>如果配置文件为dev,则将日志记录到控制台和一个滚动文件。</p> <p>如果概要文件被戳了,则只记录到一个滚动文件。<br /> <strong>logback-spring.xml</strong><br />  </p> <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/> <springProfile name="dev"> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> </appender> <root level="ERROR"> <appender-ref ref="CONSOLE"/> <appender-ref ref="ROLLING-FILE"/> </root> </springProfile> <springProfile name="prod"> <appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <root level="ERROR"> <appender-ref ref="ROLLING-FILE"/> </root> </springProfile> </configuration></code></pre> <br /> <strong>application.yml</strong> <pre> <code>spring: profiles: active: prod logging: level: ROOT: ERROR org.springframework: ERROR org.springframework.data: ERROR com.mkyong: INFO org.mongodb: ERROR file: /Users/mkyong/application.log</code></pre> 对于非web启动应用程序,您可以像这样覆盖日志文件输出: <pre> <code>$ java -Dlogging.file=/home/mkyong/app/logs/app.log -jar boot-app.jar</code></pre> <h2>Set Root Level</h2> <strong>application.properties</strong> <pre> <code># root logging level, warning : too much output logging.level.=DEBUG</code></pre> <strong>application.yml</strong> <pre> <code>logging: level: ROOT: DEBUG</code></pre> <br /> <a href="http://www.mkyong.com/wp-content/uploads/2017/01/spring-boot-web-slf4j-logging.zip" rel="external nofollow" target="_blank">下载演示demo</a>
  • Spring Boot 2.0 入门 logoback配置实战教程

    Spring Boot 2.0 入门 logoback配置实战教程,俗话说好马配好鞍。Spring Boot 框架从各方面给我们带来了开发效率。日志自然也不会落下。本文将讲解与Spring Boot 框架最优配置的日志框架logbackSpring Boot 2.0 入门 logoback配置实战教程
  • spring boot mybatis 整合_spring boot与mybaties的使用

    spring boot mybatis 整合使用讲解介绍,spring boot与mybaties的使用讲解介绍。spring boot mybatis xml mapper方式的入门和通过一个简单的例子带新手入门spring boot整合mybaties基本使用。本文主要讲解在Java编程中,spring boot mybatis xml mapper方式的入门和通过一个简单的例子带新手入门mybaties基本使用。<br /> 项目结构图: <div><img alt="项目结构图" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-03/fcb23b60-db2a-4cb6-b44f-663c8ed9c4ea.png" /><br /> 项目结构图<br /> <br /> 代码清单:<br /> <br /> spring boot配置文件application.properties: <pre> <code>#==================DataSource Config Start================== #name #spring.datasource.name=test #url #spring.datasource.url=jdbc:sqlserver://192.168.xxx.xxx;instanceName=sql_03;DatabaseName=edu;integratedSecurity=false spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 #DriverClass #spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.driver-class-name=com.mysql.jdbc.Driver #DB username spring.datasource.username=root #DB password spring.datasource.password=root #==================DataSource Config End================== #==================mybaties Config Start================== #ORM Bean Package mybatis.type-aliases-package=com.leftso.pojo mybatis.mapper-locations=classpath:/mapper/*.xml #==================mybaties Config End ==================</code></pre> </div> <br /> spring boot log简单配置,用于打印sql日志logback-spring.xml: <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <!-- 打印sql --> <logger name="com.leftso.mapper" level="DEBUG" /> </configuration></code></pre> <br /> <br /> mybaties Java config 配置MyBatiesConfig.java: <pre> <code class="language-java">package com.leftso.config; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration; /** * mybaties配置扫描mapper路径 * * @author leftso * */ @Configuration @MapperScan(basePackages = { "com.leftso.mapper" }) /** 注意,这个注解是扫描mapper接口不是xml文件,使用xml模式必须在配置文件中添加xml的配置 **/ public class MyBatiesConfig { } </code></pre> <br /> 简单测试的用户pojo对象User.java: <pre> <code class="language-java">package com.leftso.pojo; /** * 用户 * * @author leftso * */ public class User { private Long id; private String userName; private int age; private String sex; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } </code></pre> <br /> User对象的mapper接口:UserMapper.java: <pre> <code class="language-java">package com.leftso.mapper; import java.util.List; import java.util.Map; import org.springframework.stereotype.Component; import com.leftso.pojo.User; @Component("userMapper") public interface UserMapper { // 新增 int add(User user); // 修改 int remove(Long id); // 删除 int update(User user); // 查一个 User findOne(Long id); // 查多个 List<User> findList(Map<String, Object> params); } </code></pre> <br /> mapper对应的xml文件UserMapper.xml: <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.leftso.mapper.UserMapper"> <!-- 创建一个数据库user表与java中user对象关联的返回映射map --> <resultMap type="com.leftso.pojo.User" id="UserMap"> <id column="id" property="id" jdbcType="NUMERIC" /> <result column="user_name" property="userName" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="NUMERIC" /> <result column="sex" property="sex" jdbcType="VARCHAR" /> </resultMap> <!-- 新增 --> <insert id="add" parameterType="com.leftso.pojo.User" useGeneratedKeys="true" keyProperty="id"> insert into t_user <trim prefix="(" suffix=")" suffixOverrides=","><!-- 说明:使用trim的方式组合sql,属性suffixOverrides表示去除末端多余的该属性值(这里是,) --> user_name, age, sex </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> #{name,jdbcType=VARCHAR}, #{age,jdbcType=NUMERIC},<!-- 说明:数字类型都是NUMERIC,没有INT,LONG类型;其他对应类型请参照mybaties官方文档 --> #{sex,jdbcType=VARCHAR} </trim> </insert> <!-- 删除 --> <delete id="remove" parameterType="java.lang.Long"> delete from t_user where id=#{id} </delete> <!-- 修改属性 --> <update id="update" parameterType="com.leftso.pojo.User"> update t_user <set> <trim suffixOverrides=","> <if test="null!=name and ''!=name"> user_name=#{name,jdbcType=VARCHAR}, </if> <if test="null!=age"> age=#{age,jdbcType=NUMERIC}, </if> <if test="null!=sex and ''!= sex"> sex=#{sex,jdbcType=VARCHAR}, </if> </trim> </set> where id=#{id,jdbcType=NUMERIC} </update> <!-- 查询一个 --> <select id="findOne" parameterType="java.lang.Long" resultMap="UserMap"> select * from t_user where id=#{id} </select> <!-- 查询多个 --> <select id="findList" parameterType="java.util.Map" resultMap="UserMap"> select * from t_user where 1=1 <if test="null!=name and '' != name"> and user_name like '%${name}%' </if> <if test=" sex != null and '' != sex"> and sex = #{sex} </if> <if test="age > 0"> and age <![CDATA[ >=]]>#{age} <!-- 说明:在xml里面大于小于大于等于需要逆转 --> </if> </select> </mapper></code></pre> <br /> 单元测试类: <pre> <code class="language-java">package com.leftso; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.leftso.mapper.UserMapper; import com.leftso.pojo.User; @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { Logger log = LoggerFactory.getLogger(getClass()); @Autowired UserMapper userMapper; // @Test public void add() { try { User user = new User(); user.setUserName("测试用户名"); user.setAge(25); user.setSex("男"); log.info("新增前ID:" + user.getId()); userMapper.add(user); log.info("新增后ID:" + user.getId()); } catch (Exception e) { e.printStackTrace(); } } // @Test public void remove() { try { userMapper.remove(2l); } catch (Exception e) { e.printStackTrace(); } } // @Test public void update() { try { User user = new User(); user.setId(3l); user.setUserName("我是修改"); user.setAge(2); userMapper.update(user); } catch (Exception e) { e.printStackTrace(); } } // @Test public void findOne() { try { User user = userMapper.findOne(3l); log.info("\nName:" + user.getUserName() + "\nAge:" + user.getAge() + "\nSex:" + user.getSex()); } catch (Exception e) { e.printStackTrace(); } } @Test public void findList() { try { Map<String, Object> params = new HashMap<String, Object>(); params.put("name", "我"); params.put("age", 3); List<User> list = userMapper.findList(params); for (User user : list) { log.info("\nName:" + user.getUserName() + "\nAge:" + user.getAge() + "\nSex:" + user.getSex()); } } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <br /> 源码下载:<br /> GitHub:<a href="https://github.com/leftso/demo-spring-boot-mybaties3" rel="external nofollow" target="_blank">https://github.com/leftso/demo-spring-boot-mybaties3</a>
  • spring框架5.0介绍说明/概述

    java编程中spring框架5.0介绍说明/概述,spring5,spring框架,java编程spring框架一直以来都是java编程中流行的框架。 <p>Spring框架5.0介绍说明</p> <h2 style="margin-left:0px; margin-right:0px; text-align:start">1.Spring入门</h2> <div style="text-align:start"> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">本参考指南提供有关Spring框架的详细信息。它为所有功能提供了全面的文档,以及Spring所接受的基本概念(如<em>“依赖注入”</em>)的背景知识。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">如果您刚开始使用Spring,则可能需要通过创建基于Spring Boot的应用程序来开始使用Spring Framework 。Spring Boot提供了一种快速(和有意见的)方式来创建一个基于生产的基于Spring的应用程序。它基于Spring框架,有利于配置的约定,并且旨在尽可能快地让您运行。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">您可以使用start.spring.io生成一个基本项目或遵循的一个“入门”指南类的 入门构建一个RESTful Web服务 之一。除了易于消化以外,这些指南非常<em>重视任务</em>,其中大多数都是基于Spring Boot的。他们还涵盖了Spring解决方案中您可能想要考虑的其他项目。</span></span></span></span></p> </div> </div> <h2 style="margin-left:0px; margin-right:0px; text-align:start">2. Spring框架简介</h2> <div style="text-align:start"> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring Framework是一个Java平台,为开发Java应用程序提供全面的基础设施支持。Spring处理基础设施,以便您可以专注于您的应用程序。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring使您能够从“简单的Java对象”(POJO)构建应用程序,并将企业服务非侵入式应用于POJO。此功能适用于Java SE编程模型以及完整和部分Java EE。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">作为应用程序开发人员,您可以从Spring平台中获益如何:</span></span></span></span></p> </div> <div> <ul style="margin-left:1.5em; margin-right:0px"> <li> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">使Java方法在数据库事务中执行,而不必处理事务API。</span></span></span></span></p> </li> <li> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">使本地Java方法成为HTTP端点,而无需处理Servlet API。</span></span></span></span></p> </li> <li> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">使本地Java方法成为一个消息处理程序,而无需处理JMS API。</span></span></span></span></p> </li> <li> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">使本地Java方法成为管理操作,而无需处理JMX API。</span></span></span></span></p> </li> </ul> </div> </div> <div style="text-align:start"> <h3 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.1。依赖注入和控制反转</span></span></span></h3> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Java应用程序 - 从受限嵌入式应用程序到n层服务器端企业应用程序运行范围的宽松术语通常由协作形成应用程序的对象组成。因此,应用程序中的对象彼此具有<em>依赖关系</em>。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">虽然Java平台提供了丰富的应用程序开发功能,但它缺乏将基本构建块组织成一个连贯的整体的手段,将该任务留给架构师和开发人员。虽然可以使用<em>Factory</em>,<em>Abstract Factory</em>,<em>Builder</em>,<em>Decorator</em>和<em>Service Locator</em>这样的设计模式 来构成组成应用程序的各种类和对象实例,但这些模式只是简单地说明:给出名称的最佳实践,以及什么样的模式,应用于哪里,它解决的问题等等。模式是<em>您必须</em>在应用程序中<em>实现自己的</em>正式的最佳实践。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring框架<em>反转控制</em>(IoC)组件通过提供将不同组件组成完整工作应用程序的正式方法来解决这一问题。Spring Framework将形式化的设计模式作为可以集成到您自己的应用程序中的第一类对象进行编译。许多组织和机构以这种方式使用Spring框架来设计强大的,可<em>维护的</em>应用程序。</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <div style="margin-left:0px; margin-right:0px; text-align:center"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="background-color:#f1f1f1"><span style="font-family:Montserrat,sans-serif"><span style="color:#0b0a0a">背景</span></span></span></span></span></span></div> <div> <p><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="background-color:#f1f1f1"><span style="font-family:inherit">“ <em>问题是,控制的哪个方面是(他们)反转?</em> ”Martin Fowler 在2004年在他的网站上提出了有关反转控制(IoC)的 问题 .Fowler建议重新命名原则,使其更加自明,并提出<em>依赖注入</em>。</span></span></span></span></span></p> </div> </div> </div> </div> <div style="text-align:start"> <h3 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.2。框架模块</span></span></span></h3> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring框架由组织成约20个模块的功能组成。这些模块分为Core Container,数据访问/集成,Web,AOP(面向对象编程),Instrumentation,Messaging和Test,如下图所示。</span></span></span></span></p> </div> </div> <img alt="spring 框架图" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-07/2937672202ba41c6862b026d169f0528.png" /> <div style="text-align:start"> <div style="margin-left:0px; margin-right:0px"> <div style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="color:#0b0a0a"><span style="font-family:"Varela Round",sans-serif"><em>图1. Spring框架概述</em></span></span></span></span></span></div> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">以下部分列出了每个功能的可用模块及其工件名称及其涵盖的主题。工件名称与依赖关系管理工具中使用的<em>工件ID </em>相关。</span></span></span></span></p> </div> <div> <h4 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.2.1。核心集装箱</span></span></span></h4> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">所述<em>核心容器</em>由以下部分组成<code>spring-core</code>, <code>spring-beans</code>,<code>spring-context</code>,<code>spring-context-support</code>,和<code>spring-expression</code>(弹簧表达式语言)模块。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">的<code>spring-core</code>和<code>spring-beans</code>模块提供框架的基本零件,包括IOC和依赖注入特征。这<code>BeanFactory</code>是工厂模式的复杂实施。它消除了对编程单例的需要,并允许您将依赖关系的配置和规范与实际程序逻辑分离。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">所述<em>上下文</em>(<code>spring-context</code>)模块建立由设置在固体基体上<em>的核心和豆类</em>模块:它是访问一个框架式的方式是类似于一个JNDI注册表对象的装置。上下文模块从Beans模块继承其功能,并增加了对国际化的支持(例如使用资源束),事件传播,资源加载以及例如Servlet容器透明地创建上下文。Context模块还支持Java EE功能,如EJB,JMX和基本远程处理。该<code>ApplicationContext</code>接口是语境模块的焦点。 <code>spring-context-support</code>提供了将常见第三方库集成到Spring应用程序环境中的支持,特别是用于缓存(EhCache,JCache)和调度(CommonJ,Quartz)。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-expression</code>模块提供了强大的<em>表达式语言,</em>用于在运行时查询和操作对象图。它是JSP 2.1规范中规定的统一表达语言(统一EL)的扩展。该语言支持设置和获取属性值,属性分配,方法调用,访问数组,集合和索引器的内容,逻辑和算术运算符,命名变量以及从Spring的IoC容器中的名称检索对象。它还支持列表投影和选择以及通用列表聚合。</span></span></span></span></p> </div> </div> <div> <h4 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.2.2。AOP和仪器仪表</span></span></span></h4> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-aop</code>模块提供了一个符合<em>AOP</em>联盟标准的面向方面的编程实现,允许您定义方法拦截器和切入点,以便干净地解除实现应分隔的功能的代码。使用源级元数据功能,您还可以将行为信息与.NET属性类似。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">单独的<code>spring-aspects</code>模块提供与AspectJ的集成。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-instrument</code>模块提供了在某些应用服务器中使用的类检测支持和类加载器实现。该<code>spring-instrument-tomcat</code> 模块包含Spring的Tomcat测试代理。</span></span></span></span></p> </div> </div> <div> <h4 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.2.3。消息</span></span></span></h4> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring框架4包括<code>spring-messaging</code>从关键抽象模块 <em>Spring集成</em>项目,例如<code>Message</code>,<code>MessageChannel</code>,<code>MessageHandler</code>,和其他人作为基于消息的应用奠定了基础。该模块还包括一组用于将消息映射到方法的注释,类似于基于Spring MVC注释的编程模型。</span></span></span></span></p> </div> </div> <div> <h4 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.2.4。数据访问/集成</span></span></span></h4> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">所述<em>数据访问/集成</em>层由JDBC,ORM,OXM,JMS和交易模块。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-jdbc</code>模块提供了一个JDBC抽象层,无需执行繁琐的JDBC编码和解析数据库供应商特定的错误代码。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-tx</code>模块支持 针对实现特殊接口和<em>所有POJO(普通Java对象)的</em>类的编程和声明式事务管理。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-orm</code>模块为流行的对象关系映射 API(包括JPA和Hibernate)提供集成层 。使用该<code>spring-orm</code>模块,您可以将这些O / R映射框架与Spring提供的所有其他功能结合使用,例如前面提到的简单的声明性事务管理功能。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-oxm</code>模块提供了一个支持Object / XML映射实现的抽象层, 如JAXB,Castor,JiBX和XStream。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-jms</code>模块(Java消息服务)包含用于生成和消费消息的功能。从Spring Framework 4.1开始,它提供了与<code>spring-messaging</code>模块的集成 。</span></span></span></span></p> </div> </div> <div> <h4 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.2.5。卷筒纸</span></span></span></h4> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">所述<em>网络</em>层由的<code>spring-web</code>,<code>spring-webmvc</code>和<code>spring-websocket</code> 模块。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-web</code>模块提供基本的面向Web的集成功能,例如多部分文件上传功能,以及使用Servlet侦听器和面向Web的应用程序上下文初始化IoC容器。它还包含一个HTTP客户端和Spring的远程支持的Web相关部分。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-webmvc</code>模块(也称为<em>Web-Servlet</em>模块)包含用于Web应用程序的Spring的模型视图控制器(<em>MVC</em>)和REST Web Services实现。Spring的MVC框架提供了领域模型代码和Web表单之间的清晰分离,并与Spring Framework的所有其他功能集成。</span></span></span></span></p> </div> </div> <div> <h4 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.2.6。测试</span></span></span></h4> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">该<code>spring-test</code>模块支持使用JUnit或TestNG对Spring组件进行单元测试和 集成测试。它提供了Spring 的一致加载<code>ApplicationContext</code>和这些上下文的缓存。它还提供可用于孤立测试代码的模拟对象。</span></span></span></span></p> </div> </div> </div> <div style="text-align:start"> <h3 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.3。使用场景</span></span></span></h3> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">之前描述的构建模块使Spring成为许多场景中的逻辑选择,从在资源受限设备上运行的嵌入式应用程序到使用Spring的事务管理功能和Web框架集成的全面的企业应用程序。<br /> <img alt="流程" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-07/4ef2d77e2fa6406791ea64ad87b24c68.png" /></span></span></span></span><br />  </p> <div style="margin-left:0px; margin-right:0px; text-align:start"> <div style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="color:#0b0a0a"><span style="font-family:"Varela Round",sans-serif"><em>图2.典型的成熟的Spring Web应用程序</em></span></span></span></span></span></div> </div> <div style="text-align:start"> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring的声明式事务管理功能 使Web应用程序完全事务性,就像使用EJB容器管理的事务一样。所有您的定制业务逻辑都可以使用简单的POJO实现,并由Spring的IoC容器进行管理。其他服务包括支持发送电子邮件和独立于Web层的验证,可让您选择执行验证规则的位置。Spring的ORM支持与JPA和Hibernate集成; 例如,当使用Hibernate时,可以继续使用现有的映射文件和标准的Hibernate <code>SessionFactory</code>配置。表单控制器将Web层与域模型无缝集成,消除了<code>ActionForms</code>将HTTP参数转换为域模型值的需求 或其他类。<br /> <img alt="3" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-07/218014be82624d42ab35c430c91b7b98.png" /></span></span></span></span><br />  </p> <div style="margin-left:0px; margin-right:0px; text-align:start"> <div style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="color:#0b0a0a"><span style="font-family:"Varela Round",sans-serif"><em>图3.使用第三方Web框架的Spring中间层</em></span></span></span></span></span></div> </div> <div style="text-align:start"> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">有时情况不允许您完全切换到不同的框架。Spring框架并<em>没有</em>强迫你在它使用的一切; 这不是一个 <em>全无或缺的</em>解决方案。使用Struts,Tapestry,JSF或其他UI框架构建的现有前端可以与基于Spring的中间层集成,从而允许您使用Spring事务功能。您只需要使用一个连接您的业务逻辑,<code>ApplicationContext</code>并使用它<code>WebApplicationContext</code>来集成您的Web层。<br /> <img alt="4" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-07/48658620eeaf4d7d9defac9736e058f7.png" /></span></span></span></span><br />  </p> <div style="margin-left:0px; margin-right:0px; text-align:start"> <div style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="color:#0b0a0a"><span style="font-family:"Varela Round",sans-serif"><em>图4.远程使用场景</em></span></span></span></span></span></div> </div> <div style="text-align:start"> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">当你需要通过Web服务来访问现有的代码,你可以使用Spring的 <code>Hessian-</code>,<code>Rmi-</code>或<code>HttpInvokerProxyFactoryBean</code>类。启用对现有应用程序的远程访问并不困难。<br /> <img alt="5" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-07/88119b88334c40f7b0178ccc9fa384e3.png" /></span></span></span></span><br />  </p> <div style="margin-left:0px; margin-right:0px; text-align:start"> <div style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="color:#0b0a0a"><span style="font-family:"Varela Round",sans-serif"><em>图5. EJB - 包装现有的POJO</em></span></span></span></span></span></div> </div> <div style="text-align:start"> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring Framework还为Enterprise JavaBeans 提供了一个访问和抽象层,使您能够重用现有的POJO,并将其包装在无状态会话bean中,以用于可能需要声明式安全性的可伸缩的,故障安全的Web应用程序。</span></span></span></span></p> </div> <div style="text-align:start"> <h4 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.3.1。依赖管理和命名约定</span></span></span></h4> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">依赖关系管理和依赖注入是不同的。要将Spring的这些不错的功能带入应用程序(如依赖注入),您需要组装所有需要的库(jar文件),并在运行时,并且可能在编译时将它们存入您的类路径。这些依赖关系不是注入的虚拟组件,而是文件系统中的物理资源(通常为)。依赖关系管理的过程包括定位这些资源,存储它们并将其添加到类路径中。依赖关系可以是直接的(例如,我的应用程序依赖于Spring在运行时)或间接(例如我的应用程序取决于<code>commons-dbcp</code>哪个依赖于 <code>commons-pool</code>)。间接依赖关系也被称为“传递性”,它们是最难识别和管理的依赖关系。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">如果要使用Spring,您需要获取构成您所需要的Spring部分的jar库的副本。为了使这更容易,Spring被打包为一组尽可能分离依赖关系的模块,例如,如果您不想编写Web应用程序,则不需要spring-web模块。要参照本指南中,我们使用速记命名约定到Spring库模块<code>spring-*</code>或 <code>spring-*.jar,</code>其中<code>*</code>代表该模块的短名称(例如<code>spring-core</code>,<code>spring-webmvc</code>,<code>spring-jms</code>等)。您使用的实际jar文件名通常是与版本号连接的模块名称(例如<em>spring-core-5.0.0.RC2.jar</em>)。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring Framework的每个版本都会将工件发布到以下位置:</span></span></span></span></p> </div> <div> <ul style="margin-left:1.5em; margin-right:0px"> <li> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Maven Central,它是Maven查询的默认存储库,不需要任何特殊配置。Spring的许多常见的库也可以从Maven Central获得,Spring社区的大部分使用Maven进行依赖关系管理,所以这对他们来说很方便。这里的jar的名字是形式<code>spring-*-<version>.jar</code>,Maven groupId是<code>org.springframework</code>。</span></span></span></span></p> </li> <li> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">在专门用于Spring的公共Maven存储库中。除了最终的GA版本,该存储库还承载开发快照和里程碑。jar文件名与Maven Central格式相同,因此这是一个有用的地方,可以让Spring的开发版本与在Maven Central中部署的其他库一起使用。该存储库还包含捆绑包分发zip文件,其中包含所有Spring jar,捆绑在一起以便于下载。</span></span></span></span></p> </li> </ul> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">所以您需要决定的第一件事是如何管理您的依赖关系:我们通常建议使用自动化系统,如Maven,Gradle或Ivy,但您也可以手动下载所有的jar。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">下面你将会找到Spring工件的列表。有关每个模块的更完整的描述,请参阅框架模块。</span></span></span></span></p> </div> <table class="table table-bordered table-hover"> <caption style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">表1. Spring Framework人工制品</span></span></span></caption> <thead> <tr> <th style="border-color:#dedede; text-align:left; vertical-align:top"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">GroupId的</span></span></span></th> <th style="border-color:#dedede; text-align:left; vertical-align:top"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">的artifactId</span></span></span></th> <th style="border-color:#dedede; text-align:left; vertical-align:top"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">描述</span></span></span></th> </tr> </thead> <tbody> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-aop</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">基于代理的AOP支持</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-aspects</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">基于AspectJ的方面</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-beans</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">豆支持,包括Groovy</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-context</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">应用程序上下文运行时,包括调度和远程抽象</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-context-support</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">支持类将常见的第三方库集成到Spring应用程序上下文中</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-core</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">许多其他Spring模块使用的核心实用程序</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-expression</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">Spring表达语言(Spel)</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-instrument</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">用于JVM自举的仪表代理</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-instrument-tomcat</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">Tomcat的仪表代理</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-jdbc</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">JDBC支持包,包括DataSource设置和JDBC访问支持</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-jms</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">JMS支持包,包括发送/接收JMS消息的帮助类</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-messaging</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">支持消息架构和协议</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-orm</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">对象/关系映射,包括JPA和Hibernate支持</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-oxm</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">对象/ XML映射</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-test</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">支持单元测试和集成测试Spring组件</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-tx</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">交易基础设施,包括DAO支持和JCA整合</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-web</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">基础网络支持,包括Web客户端和基于Web的远程处理</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-webmvc</p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">基于HTTP的Model-View-Controller和REST端点,用于Servlet堆栈</span></span></span></span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">org.springframework</span></span></span></span></span></p> </td> <td style="background-color:#ffffff; border-color:#dedede"> <p style="text-align:left">spring-websocket</p> </td> <td style="background-color:#ffffff"> <p style="text-align:left"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><span style="color:#34302d">WebSocket和SockJS基础架构,包括STOMP消息传递支持</span></span></span></span></span></p> </td> </tr> </tbody> </table> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">Spring依赖和依靠Spring</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">虽然Spring为大量的企业和其他外部工具提供集成和支持,但它有意将其强制性依赖性保持在绝对最小值:您不必定位和下载(甚至自动)大量的jar库,以便使用Spring用于简单的用例。对于基本的依赖注入,只有一个强制性的外部依赖关系,即日志记录(有关日志记录选项的更详细描述,请参见下文)。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">接下来,我们概述了配置依赖于Spring的应用程序所需的基本步骤,首先是使用Maven,然后使用Gradle,最后使用Ivy。在任何情况下,如果有什么不清楚,请参阅依赖关系管理系统的文档,或查看一些示例代码 - Spring本身在构建时使用Gradle来管理依赖关系,而我们的示例主要使用Gradle或Maven。</span></span></span></span></p> </div> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">Maven依赖管理</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">如果您使用Maven进行依赖关系管理,则甚至不需要显式提供记录依赖关系。例如,要创建应用程序上下文并使用依赖注入来配置应用程序,您的Maven依赖项将如下所示:</span></span></span></span><br />  </p> <pre> <code class="language-xml"><dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.0.RC2</version> <scope>runtime</scope> </dependency> </dependencies></code></pre> </div> </div> </div> </div> </div> </div> </div> </div> <div style="text-align:start"> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">而已。注意,如果您不需要针对Spring API进行编译,那么范围可以被声明为运行时,通常情况下这是基本依赖注入用例的情况。</span></span></span></span></p> </div> <div style="text-align:start"> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">以上示例适用于Maven Central存储库。要使用Spring Maven存储库(例如,用于里程碑或开发人员快照),您需要在Maven配置中指定存储库位置。完整版本:</span></span></span></span><br />  </p> <pre> <code class="language-xml"><repositories> <repository> <id>io.spring.repo.maven.release</id> <url>http://repo.spring.io/release/</url> <snapshots><enabled>false</enabled></snapshots> </repository> </repositories></code></pre> <div style="text-align:start"> <div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">对于里程碑:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><repositories> <repository> <id>io.spring.repo.maven.milestone</id> <url>http://repo.spring.io/milestone/</url> <snapshots><enabled>false</enabled></snapshots> </repository> </repositories></code></pre> </div> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">对于快照:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><repositories> <repository> <id>io.spring.repo.maven.snapshot</id> <url>http://repo.spring.io/snapshot/</url> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories></code></pre> </div> </div> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">Maven“物料清单”依赖</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">当使用Maven时,可能会意外混合不同版本的Spring JAR。例如,您可能会发现第三方库或另一个Spring项目会将旧的版本的依赖关系传递给旧版本。如果您忘记自己明确声明直接依赖,可能会出现各种意外问题。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">为了克服这些问题,Maven支持“物料清单”(BOM)依赖的概念。您可以导入<code>spring-framework-bom</code>您的<code>dependencyManagement</code> 部分,以确保所有弹簧依赖(直接和传递)都是相同的版本。</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>5.0.0.RC2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement></code></pre> </div> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">使用BOM的另一个好处是,您不再需要<code><version></code> 根据Spring Framework工件指定属性:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependencies></code></pre> </div> </div> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">渐进依赖管理</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">要使用具有<a href="http://www.gradle.org/" rel="nofollow" style="box-sizing:border-box; background:transparent; color:#548e2e; text-decoration:underline; line-height:inherit" target="_blank">Gradle</a>构建系统的Spring存储库,请在该<code>repositories</code>部分中包含相应的URL :</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="background-color:#f5f5f5"><span style="font-family:Monaco,Menlo,Consolas,"Courier New",monospace"><span style="color:rgba(0, 0, 0, 0.9)"><code>repositories { mavenCentral() <span style="color:#999988"><em>// and optionally...</em></span> maven { url <span style="color:#dd2200"><span style="color:#dd1144">"</span><span style="color:#dd1144">http://repo.spring.io/release</span><span style="color:#dd1144">"</span></span> } }</code></span></span></span></span></span></span></pre> </div> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">您可以更改<code>repositories</code>从URL <code>/release</code>到<code>/milestone</code>或<code>/snapshot</code>适当。一旦存储库被配置,你可以按照通常的Gradle方式声明依赖:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="background-color:#f5f5f5"><span style="font-family:Monaco,Menlo,Consolas,"Courier New",monospace"><span style="color:rgba(0, 0, 0, 0.9)"><code>dependencies { compile(<span style="color:#dd2200"><span style="color:#dd1144">"</span><span style="color:#dd1144">org.springframework:spring-context:5.0.0.RC2</span><span style="color:#dd1144">"</span></span>) testCompile(<span style="color:#dd2200"><span style="color:#dd1144">"</span><span style="color:#dd1144">org.springframework:spring-test:5.0.0.RC2</span><span style="color:#dd1144">"</span></span>) }</code></span></span></span></span></span></span></pre> </div> </div> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">常春藤依赖管理</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">如果您喜欢使用<a href="https://ant.apache.org/ivy" rel="nofollow" style="box-sizing:border-box; background:transparent; color:#548e2e; text-decoration:underline; line-height:inherit" target="_blank">Ivy</a>来管理依赖项,那么还有类似的配置选项。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">要配置Ivy以指向Spring存储库,请将以下解析器添加到 <code>ivysettings.xml</code>:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><resolvers> <ibiblio name="io.spring.repo.maven.release" m2compatible="true" root="http://repo.spring.io/release/"/> </resolvers></code></pre> </div> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">您可以更改<code>root</code>从URL <code>/release/</code>到<code>/milestone/</code>或<code>/snapshot/</code>适当。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">配置完成后,您可以按通常的方式添加依赖项。例如(in <code>ivy.xml</code>):</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code><dependency org="org.springframework" name="spring-core" rev="5.0.0.RC2" conf="compile->runtime"/>xx</code></pre> </div> </div> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">分发Zip文件</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">虽然使用支持依赖关系管理的构建系统是推荐的获取Spring框架的方法,但仍然可以下载分发zip文件。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">分发的拉链发布到Spring Maven Repository(这只是为了方便起见,您不需要Maven或任何其他构建系统才能下载它们)。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">要下载一个分发zip打开一个Web浏览器到 http://repo.spring.io/release/org/springframework/spring,并为所需的版本选择相应的子文件夹。分发文件结束<code>-dist.zip</code>,例如spring-framework- {spring-version} -RELEASE-dist.zip。发行的分发也是针对里程碑和 快照发布的。</span></span></span></span></p> </div> </div> </div> <div style="text-align:start"> <h4 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">2.3.2。记录</span></span></span></h4> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring的日志记录设置已经修改为Spring 5:它仍然基于Apache Commons Logging API,也称为Jakarta Commons Logging(JCL)。但是,现在<code>spring-core</code> 指的是<code>spring-jcl</code>模块中定制的Commons Logging桥,具有Spring特定的<code>LogFactory</code>实现,它自动地连接到 Log4j 2,SLF4J或JDK自己的<code>java.util.logging</code>(JUL)。该实现类似于JCL-over-SLF4J桥,但具有一系列动态检测的提供程序,类似于JBoss Logging的常见目标(如Hibernate和Undertow所支持的)。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">作为一个好处,不再需要像JCL-over-SLF4J这样的外部桥接器,相应地不需要从<code>spring-core</code>依赖关系手动排除标准Commons Logging jar 。相反,它只是在运行时在Spring的自动检测样式中工作:只需将Log4j 2.x或SLF4J放在您的类路径上,而不需要任何额外的桥接jar,或者依靠通过JUL(具有可自定义的JUL设置)的默认日志记录。并且很好地对齐,默认的Hibernate设置将选择相同的通用日志目标。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">如果Log4j和SLF4J都存在,则Log4j API将被优选使用(因为它直接匹配JCL的签名,并且本机支持“致命”日志级别以及延迟解析的消息对象),类似于JBoss Logging的提供程序首选项。可以将Log4j配置为委派给SLF4J,否则SLF4J可能被配置为委托给Log4j:请检查其网站上的说明,了解如何在这种混合场景中达成一致的结果。</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <table class="table table-bordered table-hover"> <tbody> <tr> <td style="background-color:none; text-align:center; width:80px"> </td> <td style="background-color:none"> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="background-color:#ebf1e7"><span style="color:rgba(0, 0, 0, 0.6)"><span style="font-family:inherit">从Spring 5起,删除对外部Commons Logging网桥的任何引用,还可以从现有的<code>spring-core</code> 依赖关系设置中任意手动排除标准Commons Logging jar 。您的Log4j或SLF4J或JUL设置将继续工作,无需更改。请注意,您可能仍然需要<code>commons-logging</code>其他库(例如Apache HttpClient,Castor,HtmlUnit)的排除,以便代替Spring的JCL桥接器。</span></span></span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="background-color:#ebf1e7"><span style="color:rgba(0, 0, 0, 0.6)"><span style="font-family:inherit"><code>LogFactory</code>Commons Logging级别的自定义实现将不会被拾取,因为Spring的Bridge不支持定制的commons-logging.properties设置。对于任何其他日志提供程序,请设置相应的SLF4J或JUL桥(您很可能需要其他库,如Hibernate)。请注意,Log4j 1.x已经达到其使用寿命; 请迁移到Log4j 2.x.</span></span></span></span></span></span></p> </div> <div> <p><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="background-color:#ebf1e7"><span style="color:rgba(0, 0, 0, 0.6)"><span style="font-family:inherit">如果您遇到Spring Commons Logging实现的任何其他问题,请考虑排除<code>spring-jcl</code>和切换到标准<code>commons-logging</code>工件(支持<code>commons-logging.properties' setup) or to `jcl-over-slf4j</code>。</span></span></span></span></span></span></p> </div> </td> </tr> </tbody> </table> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">使用Log4j 2.x</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Log4j 2本身就是将原来的Log4j项目(1.x现在是EOL)重新进行重写。从Spring 5开始,嵌入式日志桥将在类路径上可用时自动委托给Log4j 2.x。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">因此,要使用Log4j与Spring,所有你需要做的就是把Log4j的在类路径,并为其提供一个配置文件(<code>log4j2.xml</code>,<code>log4j2.properties</code>或其他 支持的配置格式)。对于Maven用户,所需的最小依赖是:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> </dependencies></code></pre> </div> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">如果您还希望启用SLF4J委托Log4j,例如默认使用SLF4J的其他库,则还需要以下依赖关系:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.8.2</version> </dependency> </dependencies></code></pre> </div> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">以下是<code>log4j2.xml</code>登录控制台的示例:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Logger name="org.springframework.beans.factory" level="DEBUG"/> <Root level="error"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration></code></pre> </div> </div> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">使用SLF4J与Logback</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Java的简单日志门面(SLF4J)是Spring通常使用的其他库使用的流行API。它通常与Logback一起使用, 它是SLF4J API的本机实现,因此在添加到应用程序类路径时由Spring自动检测:</span></span></span></span></p> </div> <div style="margin-left:0px; margin-right:0px"> <div> <pre> <code class="language-xml"><dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.2</version> </dependency> </dependencies></code></pre> </div> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">或者,您还可以将SLF4J配置为委托给Log4j(见上文)或JUL,特别是对于默认使用SLF4J的其他库。请注意,所有图书馆通过同一个日志门面并不重要; 这只是重要的,他们最终委托给同一个日志提供商。所以当Spring可以直接去Log4j时,其他库可能会通过Log4j的SLF4J绑定,或类似于JUL。</span></span></span></span></p> </div> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">使用JUL(java.util.logging)</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit"><code>java.util.logging</code>如果类路径中没有检测到Log4j或SLF4J API,Spring将默认委派。所以没有特殊的依赖关系:只要<code>java.util.logging</code>在独立的应用程序(在JDK级别使用自定义或默认的JUL设置)或应用程序服务器的日志系统(和它的系统范围的JUL设置)。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">请注意,该<code>java.logging</code>模块在JDK 9中默认不存在,因为它不包括在内<code>java.base</code>。当使用Spring与Log4j或SLF4J时,这可以正常工作,因为在这种情况下不引用JUL API。但是,当选择使用JUL作为默认日志提供程序时,请记住激活该<code>java.logging</code>模块。</span></span></span></span></p> </div> </div> <div> <h5 style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff">Commons登录WebSphere</span></span></span></h5> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">Spring应用程序可以在本身提供JCL实现的容器上运行,例如IBM的WebSphere Application Server(WAS)。这不会引起问题本身,而是导致需要了解的两种不同的场景:</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">在“父第一”ClassLoader委托模型(WAS中的默认值)中,应用程序将始终选择Commons Logging提供的服务器版本,委托给WAS记录子系统(实际上基于JUL)。JCL的应用程序提供的变体,无论是Spring 5还是JCL-over-SLF4J桥,将随着任何本地包含的日志提供程序而被有效地忽略。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">使用“父进程”委托模式(常规Servlet容器中的默认值,但WAS上的显式配置选项),将提取应用程序提供的Commons Logging变体,使您能够设置本地包含的日志提供程序,例如Log4j或Logback,在您的应用程序。在没有本地日志提供程序的情况下,Spring(如常规Commons Logging)将默认委托给JUL,有效地记录到WebSphere的日志记录子系统,如“父第一”方案。</span></span></span></span></p> </div> <div> <p style="margin-left:0px; margin-right:0px"><span style="color:#34302d"><span style="font-family:"Varela Round",sans-serif"><span style="background-color:#ffffff"><span style="font-family:inherit">总而言之,我们建议您将“Spring”应用程序部署在“最后一个”模式中,因为它自然允许本地提供程序以及服务器的日志子系统。</span></span></span></span></p> </div> </div> </div> </div>