在pom文件中定义依赖。如果深度为同一级别,则以更先声明的依赖为准。

2.最短路径优先原则

比如:上面的情况,如果m *** en-dao传递了log4j,如果你直接把log4j的依赖直接写到pom文件中,那么项目就不会再使用其他依赖传递的log4j了,因为你直接在pom中定义了log4j。比其他依赖项传递的路径更近。

3.53、排除依赖

 <dependency>
      <groupId>com.woniu</groupId>
      <artifactId>m *** en-dao</artifactId>
      <version>1.0-SNAPSHOT</version>
      <exclusions>
          <exclusion>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
          </exclusion>
      </exclusions>
</dependency>

3.5.4,锁定版

面对众多的依赖,有一种方法可以通过直接锁定版本来确定依赖组件的版本,而无需考虑依赖路径和声明优化等因素。项目中添加哪个都行,这种方式在企业开发中比较常用。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://m *** en.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://m *** en.apache.org/POM/4.0.0 http://m *** en.apache.org/xsd/m *** en-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.woniu</groupId>
    <artifactId>com-woniu-dao</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.6</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.10</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies> 
</project>

3.6. 编写模块代码 编写模块代码

实体类写好后,需要其他模块直接引用,所以要把实体类发布到本地仓库

b. 编写dao模块代码

添加属性文件:db.

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123456

添加--dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描dao中的注解-->
    <context:component-scan base-package="cn.woniu.dao"></context:component-scan>
    <!--引用db.properties-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--配置别名-->
        <property name="typeAliasesPackage" value="cn.woniu.domain"> </property>
        <!--配置mapper.xml与dao可以不在同一包下-->
        <property name="mapperLocations" value="classpath:cn/woniu/mapper/*Dao.xml"></property>
    </bean>
    <!--配置dao扫描-->
    <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.woniu.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

编写 .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="cn.woniu.dao.UserDao">
</mapper>

C。编写模块代码

添加--.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--扫描service中的注解-->
    <context:component-scan base-package="cn.woniu.service.impl"></context:component-scan>
    <!--配置事务管理器  此处dataSource报错不用管-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置通知(service中的方法命名规则)-->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes> 
            <!--查询方法规则
				SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行
			-->
            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
            <!--配置增删改方法规则
				REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。
			-->
            <tx:method name="insert*" propagation="REQUIRED"></tx:method>
            <tx:method name="s *** e*" propagation="REQUIRED"></tx:method>
            <tx:method name="add*" propagation="REQUIRED"></tx:method>
            <tx:method name="update*" propagation="REQUIRED"></tx:method>
            <tx:method name="del*" propagation="REQUIRED"></tx:method>
            <tx:method name="delete*" propagation="REQUIRED"></tx:method>
            <!--其他方法规则-->
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!--配置aop切面-->
    <aop:config proxy-target-class="true">
        <!--配置切入点表达式-->
        <aop:pointcut id="pot"  expression="execution(* cn.woniu.service.impl.*.*(..))"></aop:pointcut>
        <!--将切入点表达式和通知关联-->
        <aop:advisor advice-ref="txadvice" pointcut-ref="pot"></aop:advisor>
    </aop:config>
</beans>

d. 编写模块代码

d1. 配置 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://m *** en.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://m *** en.apache.org/POM/4.0.0
  http://m *** en.apache.org/xsd/m *** en-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.woniu</groupId>
    <artifactId>m *** en-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--web模块的打包方式为war-->
    <packaging>war</packaging>
    <name>m *** en-web M *** en Webapp</name>
    <url>http://www.example.com</url>
    <!-- 锁定(统一管理)jar包版本 -->
    <properties>
        <spring.version>5.0.9.RELEASE</spring.version>
    </properties>
    <dependencies>
        <!--配置web模块引用service模块-->
        <dependency>
            <groupId>cn.woniu</groupId>
            <artifactId>m *** en-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--springmvc坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--servlet坐标-->
        <dependency>
            <groupId>j *** ax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <!--jsp api坐标-->
        <dependency>
            <groupId>j *** ax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <!--jstl标签库-->
        <dependency>
            <groupId>j *** ax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--m *** en tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.m *** en</groupId>
                <artifactId>tomcat7-m *** en-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!--请求路径-->
                    <path>/</path>
                    <!--指定端口-->
                    <port>8080</port>
                    <!--配置tomcat插件编码格式-->
                    <uriEncoding>UTF-8</uriEncoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

d2、配置.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
  <!--日志记录位置 -->
  <property name="LOG_PATH" value="${catalina.base}/logs/webapps"/>
  <property name="LOG_FILE" value="${LOG_PATH}/spring.log"/>
  <!--把日志输出到控制台 -->
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{yyyy-MM-dd HH:mm:ss} %5p | %-40.40logger{39} : %m%n</pattern>
      <charset>utf8</charset>
    </encoder>
  </appender>
  <!-- 指定类与<looger>的关联关系 -->
  <logger name="cn.woniu" level="DEBUG" additivity="false">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="FILE" />
  </logger>
  <!--把日志级别大于info输出到控制台 -->
  <root level="INFO">
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="FILE" />
  </root>
</configuration>

d3,配置-.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--引用dao模块中的配置文件-->
    <import resource="classpath*:spring-config-dao.xml" />
    <!--引用service模块中的配置文件-->
    <import resource="classpath*:spring-config-service.xml" />
</beans>

d4,配置-.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--扫描controller中的spring注解-->
    <context:component-scan base-package="cn.woniu.controller"></context:component-scan>
    <!--开启springmvc注解扫描-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--springmvc忽略静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/view/"></property>
        <!--后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

添加 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://j *** a.sun.com/xml/ns/j *** aee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://j *** a.sun.com/xml/ns/j *** aee http://j *** a.sun.com/xml/ns/j *** aee/web-app_3_0.xsd"
         version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <!--配置中文乱码过滤器-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*
    
    
    
        contextConfigLocation
        classpath*:spring-config-*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            
            contextConfigLocation
            classpath:springmvc-config.xml
        
        
        1
    
    
        dispatcherServlet
        /
    

3.7、m *** en私服

公司在自己的局域网内搭建自己的远程仓库服务器,称为私有服务器。私服是公司内部的m *** en远程仓库。每个员工在他们的计算机上安装m *** en 软件并连接到私有服务器。jar发布到私服,其他项目组从私服下载依赖组件(jar)。

3.7.1. 搭建m *** en私服环境 a. 下载关系

Nexus 是一个 M *** en 仓库管理器。通过nexus,可以搭建一个m *** en仓库。同时,nexus还提供了强大的仓库管理功能和组件搜索功能。下载Nexus,下载地址——3

但它不再可供下载。

解压数据中的私服,放在没有中间的文件路径下

#### b、启动nexus服务器,进入bin目录

nexus.exe \run nexus 

C。停止私服

以管理员身份运行命令提示符工具,切换到刚刚解压的私服文件的bin目录下

输入停止命令:nexus.exe /stop

d. 找到私服端口

找到nexus-3.33.0-01-win64\nexus-3.33.0-01\etc\nexus-。

\# nexus访问端口
application-port=8081
\# nexus 主机监听配置(不用修改)
application-host=0.0.0.0
\# nexus 工程目录
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
\# nexus 的 web 访问路径
nexus-work=${bundleBasedir}/../sonatype-work/nexus
\# nexus 仓库目录
runtime=${bundleBasedir}/nexus/WEB-INF # nexus 运行程序目录

e. 访问私人服务器

在浏览器中使用url访问私服::8081/

F。nexus 仓库类型

有4种类型的nexus仓库

1.宿主仓库,部署自己的jar到这类仓库,包括和两部分,公司内部发布版本仓库,公司内部测试版本仓库,仓库只针对公司内部局域网,3rd方是一个第三方仓库,意思是当局域网需要网上的第三方包(不是我们自己写的包)的时候,先从网上的m *** en官网下载,从本地电脑下载,然后上传到第三方仓库

2.proxy,代理仓库,用于代理远程公共仓库,如m *** en中央仓库,用户连接私服,私服自动 *** 仓库下载jar包或插件。当我们私服没有jar的时候, *** 仓库下载。下载后全部放到这个代理仓库里。仓库专门用来存放从中央仓库下载的文件。

3.group,仓库组,用于合并多个/proxy仓库,通常我们配置自己的m *** en连接仓库组。

4.():兼容版本的jar或者插件基本不用

3.7.2. 在nexus上创建两个主机仓库a,并新建一个仓库

b、选择()

c、填写m *** en仓库名称和类型

d、加入m *** en仓库组

3.7.2、上传jar到私服a、手动上传

选择本地jar包,填写内容,勾选一个POM文件,有这些:生成pom文件

上传后效果:

b、mvn上传

企业内多个团队协同开发,通常会将一些通用的组件和开发模块发布到私有服务器上,供其他团队或模块开发者使用。本例假设多个团队分别开发m *** en-、m *** en-dao、m *** en-、m *** en-web。一个团队开发完m *** en-dao后,会把m *** en-dao发布到私服上找私服,供m *** en-team使用。本例中将m *** en-项目打包为jar包发布到私服

打开本地m *** en\conf\.xml,节点下添加私服登录账号和密码

 <!--配置要访问的私服帐号与密码-->
    <server>
        <id>releases</id> 
        <username>admin</username> 
        <password>admin123</password>
     </server> 
     <server> 
        <id>snapshots</id> 
        <username>admin</username> 
        <password>admin123</password> 
     </server>

在要上传到私服的项目上配置上传路径,在s *** 项目的父项目pom.xml中添加如下配置

 <!--配置上传jar包到m *** en私服中的路径-->
    <distributionManagement>
        <!--公司内部仓库-->
        <repository>
            <id>releases</id>
            <name>m *** en-releases</name>
            <url>http://localhost:8081/repository/m *** en-releases/</url>
        </repository>
        <!--公司测试仓库-->
        <snapshotRepository>
            <id>snapshots</id>
            <name>m *** en-snapshots</name>
            <url>http://localhost:8081/repository/m *** en-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

*注:pom.xml的id对应.xml的配置id!而且必须配置在坐标上,否则会报错

将项目中的jar包上传到私服

3.7.3. 下载私服jar

在配置nexus之前,如果没有本地仓库,就 *** 仓库下载。通常,在企业内部的局域网中都会部署一台私服服务器。如果你有私服本地项目,先去本地仓库找jar。如果找不到,就连接私服,从私服上下载。jar包,如果私服没有jar包,私服作为代理服务器同时从中央仓库下载jar包,

这样做的好处一方面是私服依赖公司项目的jar包统一管理,另一方面提高了 *** 。项目连接私服时下载jar包的速度比连接中央仓库的项目快很多。

本例测试从私服下载m *** en-dao包

<!-- 下载jar包配置 -->
    <profiles>
	<profile> 
		<!--profile的id -->
		<id>dev</id>
		<repositories>
			<repository> 
        <!--仓库id,repositories可以配置多个仓库,保证id不重复 -->
				<id>nexus</id> 
        <!--仓库地址,即nexus仓库组的地址 -->
				<url>http://localhost:8081/repository/m *** en-public/</url> 
        <!--是否下载releases构件 -->
				<releases>
					<enabled>true</enabled>
				</releases> 
        <!--是否下载snapshots构件 -->
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
		</repositories>
		<pluginRepositories> 
      <!-- 插件仓库,m *** en的运行依赖插件,也需要从私服下载插件 -->
			<pluginRepository> 
        <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
				<id>public</id>
				<name>Public Repositories</name>
				<url>http://localhost:8081/repository/m *** en-public/</url>
			</pluginRepository>
		</pluginRepositories>
	</profile>
</profiles>

在m *** en本地仓库找到项目的打包地址,删除m *** en-dao

发布项目,发现找不到m *** en-dao.jar

一、配置文件

配置本地m *** en的.xml,设置jar从私服下载

b、配置权限

未经允许不得转载! 作者:admin,转载或复制请以超链接形式并注明出处天心神途传奇手游发布网

原文地址:《3.53私服服务器排除依赖、锁定版本(一)》发布于:2022-12-14

发表评论

表情:
验证码
评论列表 (暂无评论,217人围观)

还没有评论,来说两句吧...