个人插件、配置、依赖整理
(LTV)
1、freemarker 相关依赖导入:
1.1.pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cl</groupId>
<artifactId>freemarker01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>freemarker01 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- freemarker的坐标依赖,可根据需要随时更换 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<!-- servlet-api的坐标依赖,可换 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<finalName>freemarker01</finalName>
<plugins>
<!-- jetty的坐标依赖,巨坑,连跪数把,若不加configuration就需要在启动项配置:
jetty:run -Djetty.port=9090
-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<httpConnector>
<port>9090</port>
</httpConnector>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.2.web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- FreeMarker 的Servlet配置 -->
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<init-param>
<!-- 模板路径 -->
<param-name>TemplatePath</param-name>
<!-- 默认在webapp⽬录下查找对应的模板⽂件 -->
<param-value>/</param-value>
</init-param>
<init-param>
<!-- 模板默认的编码:UTF-8 -->
<param-name>default_encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<!-- 处理所有以.ftl结尾的⽂件;ftl是freemarker默认的⽂件后缀 -->
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
</web-app>
2.servlet容器的部署
2.1.Jetty插件:
<!-- 设置在plugins标签中 -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version><!-- 版本可随时更换 -->
<configuration>
<!-- 热部署,每10秒扫描⼀次 -->
<scanIntervalSeconds>10</scanIntervalSeconds>
<!-- 可指定当前项⽬的站点名 -->
<contextPath>/</contextPath> <!-- 设置项目初始路径,此时默认在根路径下 -->
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port> <!-- 设置启动的端⼝号 -->
</connector>
</connectors>
</configuration>
</plugin>
2.2.Tomcat插件:
<!-- 设置在plugins标签中 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version><!-- 可随时去官网搜索新版本更换 -->
<configuration>
<port>8081</port> <!-- 启动端⼝ 默认:8080 -->
<path>/test</path> <!-- 项⽬的站点名,即对外访问路径 -->
<uriEncoding>UTF-8</uriEncoding> <!-- 字符集编码 默认:ISO-8859-1 -->
<server>tomcat7</server> <!-- 服务器名称 -->
</configuration>
</plugin>
3.Maven的setting文件设置
3.1.修改默认仓库位置
打开maven⽬录 -> conf -> settings.xml
添加仓库位置配置
<localRepository>F:/m2/repository</localRepository>
注:仓库位置改为⾃⼰本机的指定⽬录,"/"不要写反 ,以后就不用下载太多依赖
3.2.更换阿⾥镜像,加快依赖下载
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
3.3.紧随其后的Maven配置
重点:other setting中配置全局变量
倒数第三项修改到自己的maven的bin目录的上一级
倒数第二项在maven的conf目录下找到settings.xml文件,勾选其后的Override
最后一项改成本地设置的仓库地址,参照3.1,这样以后创建新项目的时候不用下载重复的依赖包了。但是需要注意的是随着时间增长仓库可能会存下较多依赖,比较占用空间。
4.Spring框架配置整合
4.1.Spring框架的依赖
添加到pom.xml中:
<!-- 添加Spring框架的核⼼依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
4.2.Spring配置文件
在src下添加Resource源目录,添加spring.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
xmlns 即 xml namespace xml使⽤的命名空间
xmlns:xsi 即xml schema instance xml 遵守的具体规范
xsi:schemaLocation 本⽂档xml遵守的规范 官⽅指定
-->
<!-- 然后可以配置bean对象 -->
<bean id="userService" class="com.xxxx.service.UserService"></bean>
</beans>
4.3.导入dom4j的依赖,解析xml文件
<!-- dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- XPath -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
自己配置的bean文件一般有六种:
1.普通 xml文件中的bean对象
<beans>
<bean id="userService" class="com.xxxx.service.UserService"></bean>
<bean id="accountService" class="com.xxxx.service.AccountService"></bean>
</beans>
2.配置标签注入
<bean id="instanceFactory" class="com.xxxx.factory.InstanceFactory"></bean>
<bean id="userService" factory-bean="instanceFactory" factorymethod="createUserService"></bean>
<!--==========================无情的分割线======================-->
<bean id="userDao" class="com.xxxx.dao.UserDao"></bean>
<bean id="userService" class="com.xxxx.service.UserService">
<!--业务对象 注⼊-->
<property name="userDao" ref="userDao"/>
</bean>
3.构造器注入
<bean id="userDao" class="com.xxxx.dao.UserDao" ></bean>
<bean id="accountDao" class="com.xxxx.dao.AccountDao" ></bean>
<bean id="userService" class="com.xxxx.service.UserService">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
<constructor-arg name="accountDao" ref="accountDao"></constructor-arg>
</bean>
4.set方法注入
<!--修改为set⽅法注⼊-->
<bean id="accountService" class="com.xxxx.service.AccountService">
<property name="roleService" ref="roleService"/>
</bean> <bean id="roleService" class="com.xxxx.service.RoleService">
<property name="accountService" ref="accountService"/>
5.静态及实例化工厂注入
<!--
静态⼯⼚注⼊:
静态⼯⼚注⼊也是借助set⽅法注⼊,只是被注⼊的bean对象的实例化是通过静态⼯⼚实例化的
-->
<bean id="typeDao" class="com.xxxx.factory.StaticFactory" factorymethod="createTypeDao"></bean>
<!--
实例化⼯⼚注⼊:
实例化⼯⼚注⼊也是借助set⽅法注⼊,只是被注⼊的bean对象的实例化是通过实例化⼯⼚实例化的
-->
<bean id="instanceFactory" class="com.xxxx.factory.InstanceFactory"></bean> <bean id="typeDao" factory-bean="instanceFactory" factory-method="createTypeDao">
</bean>
6.P名称空间注入
<!--
p:属性名:="xxx" 引⼊常量值
p:属性名-ref:="xxx" 引⼊其他Bean对象的id属性值
-->
<bean id="userService" class="com.xxxx.service.UserService"
p:userDao-ref="userDao"
p:host="127.0.0.1" />
5.文件约束
5.1.config的约束
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5.2.mapper约束
<?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">