-
2021-03-03 08:08:43
一般在 web 工程中都会用到 web.xml 来配置 Servlet、Filter、Listener 欢迎页面等,可以方便的开发web工程。需要知道的是 web.xml 并不是必须的,一个web 工程可以没有 web.xml 文件,只不过网站的功能复杂起来后,web.xml 有非常大用处,因此在 web 工程中创建一个 web.xml 还是非常有必要的。
1. 命名空间
在 web.xml 中,使用的 web-app_4_0.xsd 文件定义的规则,需要指定这个文件的位置。
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> </web-app>
关于命名空间这部分内容,就不在这里详细讲了,需要了解的可以看这篇博文XML 中的 xmlns、xmlns:xsi、xsi:schemaLocation
2. 欢迎页面配置
欢迎页面的配置不是很重要,了解即可。
<project...> <welcome-file-list> <welcome-file>wecome1.jsp</welcome-file> <welcome-file>wecome2.jsp</welcome-file> </welcome-file-list> </project>
上面代码中就配置了两个欢迎页面 wecome1.jsp 和 wecome2.jsp,显示时按照配置顺序显示,如果能找到 wecome1.jsp 文件就显示该文件,如果找不到就找第二个,依次类推。
欢迎页面是用于访问 web 工程时,只给了根名,没有给出具体的页面,这时就回去访问配置的欢迎页面,如果没有配置欢迎页面,不同的应用服务器可能会有不同的行为。对于 Tomcat 来说,会默认先查找 index.html 文件,如果找到了,就将其返回给浏览器;如果没有找到,就继续查找 index.jsp 文件,如果都没有找到,那么 Tomcat 就会显示 The requested resource is not available 的页面。
欢迎页面只是在没有给出具体的访问页面时会被用到,如果指定了具体页面,只要访问的路径正确,是可以正常访问的。3. Servlet 配置
这是我们常用的一个配置,主要使用的标签是
<servlet></servlet>
为Servlet 命名和<servlet-mapping></servlet-mapping>
为 Servlet 定制 URL。<project..> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>D20210220.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> </project>
在 <servlet> 标签中, <servlet-name> 中设置的是为 Servlet 注册的名字,要保证该名字在 web.xml 文件中是唯一的,一般直接使用类名即可,<servlet-class> 中设置的是需要配置的 Servlet 的全类名。
在 <servlet-mapping> 中, <servlet-name> 中的名字,需要和 <servlet> 中的名字一致,<url-pattern> 为这个 Servlet 映射一个对外访问路径。servlet 和 url-pattern 之间的关系是 一对多的关系。
4. Filter 配置
在学习了 Servlet 的配置之后,学习 Filter 的配置就很简单了。
Filter 的配置和 Servlet 的配置很相似,只是将 servlet 换成 filter 而已。<filter> <filter-name>Filter1</filter-name> <filter-class>D2020128.TestFilter</filter-class> </filter> <filter-mapping> <filter-name>Filter1</filter-name> <url-pattern>/TestServlet</url-pattern> </filter-mapping>
Filter 的配置中和 Servlet 配置差别较大的地方在于 url-pattern,Servlet 的 url-pattern 中配置的路径是访问 Servlet 的路径,而 Filter 的 url-pattern 中配置的路径是访问已经存在的网络资源,如静态页面、jsp、servlet等。
filter 和 url-pattern 之间的关系是 多对多的关系。即,一个 filter 可以有多个 url-pattern(使用多个 URL 地址,都会访问同一个 FIlter),多个 filter 可以对应一个 url-pattern(多个 filter 会根据 web.xml 中配置的顺序组成 filter 链)。
因此,Filter 的 url-pattern 有一种配置是<url-pattern>/*</url-pattern>
,表示所有外部访问都需要先经过该过滤器。
Filter 应用场景:防盗链、编码过滤器5. Listener 配置
Listener 不需要处理外界访问,所以它的配置不需要定制 URL,因此只需要配置<listener>
<listener> <listener-class>Listener的全类名<listener-class> </listener>
注解配置
在 Servlet 3.0 版本以上,可以使用
@WebServlet
注解代替 web.xml 进行配置,@WebServlet
的配置是一套独立的用法。Servlet 注解配置
一般用
@WebServlet( "/TestServlet1")
即可,其中 TestServlet1 的位置可以自己起一个名字,但是需要注意不能在项目中有重名。下面展示一下
@WebServlet
注解的复杂一些的用法,不做详解@WebServlet(urlPatterns = "/TestServlet1",initParams = { @WebInitParam(name = "name1",value="value1"), @WebInitParam(name = "name2",value="value2") }, name = "Hello")
Filter 注解配置
在 Filter 类文件的开头处写上
@WebFilter("/TestFilter")
更多相关内容 -
maven settings.xml配置文件
2017-12-04 11:41:03maven settings.xml配置文件,亲试无问题,可以使用,eclipse和Myeclipse都可以 -
Spring XML配置文件详解
2020-04-22 11:21:52Spring框架的配置文件是基于xml的,Spring强大的功能依赖于类型繁多的配置项,这些配置项纷繁复杂难以记忆,下面将常用的配置项示例记录下来,以备后续查看使用。 一、Spring配置文件示例 <?xml version="1.0" ... spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Spring框架的配置文件是基于xml的,Spring强大的功能依赖于类型繁多的配置项,这些配置项纷繁复杂难以记忆,下面将常用的配置项示例记录下来,以备后续查看使用。
一、Spring配置文件示例
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 注解包扫描 --> <context:component-scan base-package="com.test.fx.service"></context:component-scan> <!-- 引入DB配置文件 --> <context:property-placeholder location="classpath:oracleDriver.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${name}"></property> <property name="password" value="${password}"></property> </bean> <!-- 创建SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:com/baizhi/fx/dao/*DaoImpl.xml"></property> </bean> <!-- 扫描Mapper文件并生成dao对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.test.fx.dao"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务管理器注解 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
二、配置文件详解
- 上面给出了Spring最基本的配置文件示例,下面就其标签元素进行详细说明
1.XML结构体概述
这是一个最基本的Spring XML配置文件结构体,这些格式基本都是固定的,可以直接复用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" <!-- 默认bean命名空间 --> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- xml约束命名空间定义 --> xmlns:aop="http://www.springframework.org/schema/aop"<!-- AOP命名空间的scheme约束 --> xmlns:context="http://www.springframework.org/schema/context" <!-- context命名空间的scheme约束 --> xsi:schemaLocation=" <!-- 上面各个scheme的location信息(网络地址) --> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> </beans>
2.<Bean>标签
<!-- 一个最基本的bean标签 --> <bean id="bean名称" class="bean的类全名(类全限定路径)" scope="作用域">
上面bean配置了3条最常见的属性,实际上bean的属性有以下内容:
属性 说明 id bean的名称,可以随便起名但在spring定义中,一般为类名的驼峰命名。例如<bean id=“userServiceImpl”> class bean的完全限定名,就是类的全路径名。<bean class=“com.test.demo.UserServiceImpl”> scope bean的作用域,或者叫声明周期。可选值有singleton(单例),prototype(多例,又叫原型),request,session,global session lazy-init lazy-init 属性表明了bean是否为延迟加载。false为立即加载,表示在spring启动时,立刻进行实例化。为true时将不会在ApplicationContext启动时提前被实例化,而是第一次向容器通过getBean索取bean时实例化才会加载。 init-method 用于在bean初始化时指定执行方法。只有一个类完整的实例被创建出来后,才能走初始化方法 destroy-method 用于容器在销毁bean之前调用的方法。注意spring容器在销毁bean时会先执行对应的destroy方法后销毁该bean abstract 是否为抽象Bean,spring对于抽象bean不产生实例,主要用于继承 parent 父Bean的名称,会继承父Bean的属性,与Java的Class无任何关系,也就是说定义了一个抽象类,这个抽象类被作为父类,其他子类可以继承父类的属性。这个父类在spring的配置文件中定义时,无需指定class属性 factory-bean 创建该bean时使用的工厂类(名字) factory-method 要调用的工厂类方法 depends-on 依赖对象,这个Bean在初始化时依赖的对象,这个对象会在这个Bean初始化之前创建。 dependency-check 依赖检查它用来确保Bean组件通过JavaBean描述的所以依赖关系都得到满足。在与自动装配功能一起使用时,它特别有用。可选值:none(不进行依赖检查),objects(只做对象间依赖的检查),simple(只做原始类型和String类型依赖的检查),all(对所有类型的依赖进行检查。它包括了前面的objects和simple) autowire 自动装配,它定义了Bean的自动装载方式。 可选值:no(不使用自动装配功能),byName(通过Bean的属性名实现自动装配),byType(通过Bean的类型实现自动装配),constructor(构造函数的参数的自动组装),autodetect通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式 -
lazy-init属性需要注意的是:
-
如果一个设置了立即加载的bean1,引用了一个延时加载的bean2,那么bean1在容器启动时被实例化,而bean2由于被bean1引用,所以也被实例化,这种情况也符合延时加载的bean在第一次调用时才被实例化的规则。
-
在容器层次中通过在<beans/>元素上使用’default-lazy-init’属性来控制延时初始化也是可能的。如下面配置:
<beans default-lazy-init="true"><!-- no beans will be eagerly pre-instantiated... --></beans>
-
如果一个bean的scope属性为scope="pototype"时,即使设置了lazy-init=“false”,容器启动时不实例化bean,而是调用getBean方法实例化的
-
-
init-method属性说明:
-
此属性用于替代的是 InitializingBean接口。InitializingBean接口为bean提供了初始化方法的方式,它只有afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法
// 示例 import org.springframework.beans.factory.InitializingBean; public class TestInitializingBean implements InitializingBean{ @Override public void afterPropertiesSet() throws Exception { System.out.println("TestInitializingBean "); } }
<!-- 配置文件 --> <bean id="testInitializingBean" class="com.TestInitializingBean" ></bean>
// 测试类如下 public class Main { public static void main(String[] args){ ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/beans.xml"); } } // 打印结果为:TestInitializingBean
-
如果在配置文件中指明了bean的init-method方法,则不用实现InitializingBean接口。示例如下
public class TestInitMethod{ public void testInit(){ System.out.println("test init-method"); } }
<bean id="testInitMethod" class="com.TestInitMethod" init-method="testInit"></bean>
public class Main { public static void main(String[] args){ ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext("spring.xml"); } } // 打印结果为:test init-method
-
-
destroy-method属性:
- 与之init初始化方法类似的是,此属性替代的是DisposableBean接口中的destroy()方法
- 当bean的作用域scope = "prototype"时,该属性将失效,因为在spring中,如果一个bean为多例的,那么该bean被创建后,spring将不再去管理了
-
关于abstract和praent属性:
// 这里有一个类叫做XiaoMing public class XiaoMing{ private String id; private String loginName; private String loginPwd; private String sex; } // 又有另一个类叫XiaoHong public class XiaoHong{ private String id; private String loginName; private String loginPwd; private String sex; }
此时,我们发现,这两个类有公用的字段属性,此后不管叫小刚,李四,王五也好,他们都会有共同的属性
,假设他们拥有同一个登录帐号,那么我们可以将登录字段属性设置为一个抽象类,其他子类可以继承
<bean id="loginAbstract" abstract="true"> <property name="loginName" value="admin"/> <property name="loginPwd" value="admin"/> </bean> <!-- 其他子类继承了父类中的通用属性 --> <bean id="xiaoMing" class="com.test.demo.XiaoMing" parent="loginAbstract"> <property name="id" value="001" /> <property name="sex" value="man" /> </bean> <bean id="xiaoHong" class="com.test.demo.XiaoHong" parent="loginAbstract"> <property name="id" value="002" /> <property name="sex" value="woman" /> </bean>
-
factory-bean & factory-method说明
这两个属性可以同时出现,也可以只指定factory-method,下面进行详细说明。
-
情况一:同时使用factory-bean 和 factory-method
// 汽车生产工厂类 public class CarFactory { // 非静态方法 public Car createCar(){ Car car = new Car(); car.setBrand("BMW"); return car; } }
<!-- 配置文件中调用 --> <!-- 工厂方法--> <bean id="carFactory" class="com.factory.demo.CarFactory"/> <!-- 由工厂创建该类时,可以省略class属性 --> <bean id="car1" factory-bean="carFactory" factory-method="createCar"> </bean>
我们在一个bean 元素上同时配置 factory-bean 和 factory-method, 那么意思就是说, 这个bean 的创建就使用工厂模式,即由CarFactory工厂类创建car对象。factory-method属性指向了工厂类中的具体产生car对象的方法。此处的方法必须是非静态的(如果是静态的则会抛出异常)。至于原因你也一定能想到,static方法可以直接通过类调用而无需实例化,何必再去多此一举实例化工厂类呢
-
情况二:只配置了factory-method属性
public class CarFactory { // 静态方法 public static Car createStaticCar(){ Car car = new Car(); return car; } }
<!-- 指定该bean的class类型为该bean的工厂类 --> <bean id="car2" class="com.bean.demo.CarFactory" factory-method="createStaticCar"></bean>
这里该bean的class类型为产生该bean的工厂类,factory-method指向该工厂类的非静态方法。
另外工厂方法是可以有参数的,如果有参数,那么我们可以通过 constructor-arg 进行配置,原理跟 普通的 构造方法注入是一样的
public class CarFactory { // 静态方法 public static Car createStaticCar(String brand){ Car car = new Car(); car.setBrand(brand); return car; } }
<bean id="car2" class="com.bean.demo.CarFactory" factory-method="createStaticCar"> <constructor-arg name="brand" value="GTR" /> </bean>
-
-
ref引用属性
用于指定属性值为spring容器中的其它bean.两个基本属性是local和bean
local:如果一个bean与被参考引用的bean在同一个xml 文件中而且被引用参考的bean是用id来命名的,那么就可以使用ref的local属性。这样会让项目里解析器更早的在xml文档解析时,验证bean的id
**Bean:**用ref元素的bean属性指定被参考引用的bean是spring中最常见的形式,它允许指向的bean可以在同一个xml,也可以不在同一个xml中。bean属性的值可以与被参考引用的bean的id属性相同,也可以与被参考引用的bean的属性不相同
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/db_ssm" /> <property name="username" value="hc" /> <property name="password" value="123456" /> </bean> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> <!-- <ref bean="dataSource"> --> </property> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean>
3.<aop>标签
<!-- 定义切面bean --> <bean id="切面bean的id" class="类的全限定路径"></bean> <!-- 目标对象 --> <bean id="目标对象id" class="目标对象类的全限定路径"></bean> <!-- 定义切面的配置:注解 --> <aop:config> <aop:aspect id="切面ID" ref="要引用的切面实例名称"> <!--定义一个切点 --> <aop:pointcut id="切入点名称" expression="切入点表达式" /> <!-- 定义四类通知--> <aop:before method="切面类前置通知方法名" pointcut-ref="引用的切入点名称"/> <aop:after method="切面类后置通知方法名" pointcut-ref="引用的切入点名称"/> <aop:after-returning method="切面类最终通知方法名" pointcut-ref="引用的切入点名称"/> <aop:after-throwing method="切面类异常通知方法名" pointcut-ref="引用的切入点名称"/> <!-- 定义环绕通知 --> <aop:around method="切面类环绕通知方法名" pointcut-ref="引用的切入点名称"/> <!--定义增强类 --> <aop:declare-parents types-matching="com.test.CurdServiceImpl(匹配的类型)" implement-interface="com.test.CurdService(实现的接口)" default-impl="com.test.CurdServiceImpl(默认实现类)"/> </aop:aspect> </aop:config>
有关AOP的XML配置使用请移步本博客:
AOP XML的配置使用 https://blog.csdn.net/zxcbnm7089/article/details/1043595984.各种bean的值注入
请移步本博客:
Spring Bean值注入 - 基于XML配置文件 https://blog.csdn.net/zxcbnm7089/article/details/104394070
5.<tx>事务标签
- xml配置格式如下
<!-- 配置事务管理器 --> <bean id="事务管理bean id" class="类的全限定路径"> <property name="数据源属性名称" ref="引用的数据源实例名称" /> </bean> <!-- 配置一个事务通知 --> <tx:advice id="事务通知名称" transaction-manager="事务管理器实例名称"> <tx:attributes> <tx:method name="方法名" read-only="是否只读" propagation="事务类型"/> <!-- 其他所有方法,以默认事务运行 --> <tx:method name="*" /> </tx:attributes> </tx:advice> <!-- 配置事务切入点 --> <aop:config> <aop:pointcut id="事务切入点id" expression="事务切入点表达式" /> <aop:advisor advice-ref="事务通知名称" ponitcut-ref="事务切入点id" /> </aop:config>
- 代码使用示例
<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${name}"></property> <property name="password" value="${password}"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务通知 --> <!-- 使用<tx:advice>元素声明事务通知,需要指定id属性,以便AOP把通知和切入点关联起来 --> <!-- 还需要指定transaction-manager属性,其值为Bean配置文件中事务管理器的id属性值 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 在<tx:advice>元素下声明<tx:attributes>元素,用于指定事务属性 --> <!-- 在<tx:attributes>元素下可以使用多个<tx:method>元素指定不同方法的不同事务属性 --> <tx:attributes> <!-- 根据方法名指定事务的属性 --> <tx:method name="BookShopXmlService" propagation="REQUIRED"/> <!--方法名以get开头的事务属性 --> <tx:method name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置事务切入点,以及把事务切入点和事务属性关联起来 --> <!-- 在<aop:config>元素下,使用<aop:advisor>元素声明一个增强器,将事务通知和切入点关联起来 --> <!-- 使用 advice-ref属性指定事务通知,用pointcut-ref属性指定切入点 --> <aop:config> <aop:pointcut expression="execution(* com.sqp.spring.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>
6.<context>标签
-
**<context:annotaion-config>**注解装配在默认情况下是不开启的,为了使用注解装配,我们必须在Spring配置文件中配置 <context:annotation-config/>元素。也就是说,如果我们想要使用@Autowired、@Resoure等注解时可以通过此标签配置,让Spring隐式的帮我们生成这些Bean。
<!-- 使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean: --> <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> <!-- 使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean --> <bean class="org.springframework.beans.factory.annotation. RequiredAnnotationBeanPostProcessor"/> <!-- 类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。 --> <!-- 这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。 --> <context:annotation-config/>
-
**<context:component-scan>**组件扫描
Spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进Spring容器中管理。
<!-- base-package 指定了要扫描的包路径 --> <context:component-scan base-package="com.myapp" />
这个配置隐式注册了多个对注解进行解析处理的处理器,包括
<context:annotation-config/>
该配置注册的处理器,也就是说写了<context:component-scan base-package="" />
配置,就不用写<context:annotation-config/>
配置了。另外此标签还有一个属性是use-default-filter
,默认为true这就意味着会扫描指定包下的全部的标有@Component,@Controller,@Service等等的类,并注册成bean-
另外,<context:component-scan>标签还有两个子标签,其实就是扫描时的过滤器
<context:exclude-filter> 指定的不扫描 <context:include-filter> 指定的扫描
<!-- 例如,我们只想扫描指定包下面的Controller --> <!-- 此处如果省略了use-default属性,不仅会把指定包下的Controller扫描出来,还会把带有@Service等注解的其他组件扫描出来 --> <context:component-scan base-package="com.sparta.trans" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
-
-
**<context:property-placeholder>**配置文件处理
用来处理用一个proerties文件里面的内容来替换spring配置文件中的${}内容,例如:
<!-- 引入DB配置文件 --> <context:property-placeholder location="classpath:oracleDriver.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${name}"></property> <property name="password" value="${password}"></property> </bean>
属性 说明 location 表示引入配置文件的位置,多个之间用逗号分隔 file-encoding 指定文件编码,如GBK,UTF-8 ignore-resource-not-found 如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常 ignore-unresolvable 是否忽略解析不到的属性,如果不忽略,找不到将抛出异常 properties-ref 配置类的引用,如果在xml中声明了properties bean,那么将引用这个bean的id local-override 是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性 system-properties-mode 系统属性模式:ENVIRONMENT(默认),OVERRIDE,NEVER ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer如果是本地覆盖模式:那么查找顺序是:properties-ref、location、environment,否则正好反过来; OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment如果是本地覆盖模式:那么查找顺序是:properties-ref、location、System.getProperty(),System.getenv(),否则正好反过来; NEVER:只查找properties-ref、location;
-
mybatis xml配置
2020-02-24 21:27:283.创建Mybatis的主配置文件 SqlMapConifg.xml 4.创建映射配置文件IUserDao.xml 注意事项: 1.在配置mysql连接的基本信息时要注意配置url 2.mybatis的映射配置文件位置必须和dao接口的包结构相同 3.映射配置文件的...分析:
1.新建一个maven工程并导入坐标
2.创建文件实体类和dao的接口
3.创建Mybatis的主配置文件 SqlMapConifg.xml
4.创建映射配置文件IUserDao.xml注意事项:
1.在配置mysql连接的基本信息时要注意配置url2.mybatis的映射配置文件位置必须和dao接口的包结构相同
3.映射配置文件的mapper标签必须是namespace属性的取值必须是dao接口的全限定
类名。
4.映射配置文件的操作配置,id属性的取值必须是dao接口的方法名文件配置
Pom.xml文件配置<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>org.example</groupId> <artifactId>mybatis_01</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <!-- mybatis--> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <!-- 日志--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies> </project>
在resources文件下 新建SqlMapConfig.xml配置文件
<?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"> <configuration> <!-- 配置环境--> <environments default="mysql"> <!-- 配置mysql环境 id要与default一致 --> <environment id="mysql"> <!-- 配置事务的类型--> <transactionManager type="JDBC"></transactionManager> <!-- 配置连接池--> <dataSource type="POOLED"> <!-- 配置连接数据库的基本信息--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=UTC&useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件--> <mappers> <mapper resource="cn/mybatis/dao/IUserDao.xml"/> </mappers> </configuration>
配置IUser.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.mybatis.dao.IUserDao"> <!-- 配置查询所有 id为方法名 resultType指定封装的实体类型--> <select id="findAll" resultType="cn.mybatis.domain.User"> select *from user </select> </mapper>
IUserDao接口
public interface IUserDao { /** * 查询所有操作 * @return */ List<User> findAll(); }
测试
public static void main(String[] args) throws Exception { //1.读取配置文件 InputStream in= Resources.getResourceAsStream("SqlMapConfig.xml"); //2.创建SalSessionFactory工厂 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); //3.使用工厂生产Sqlsession对象 SqlSession session=factory.openSession(); //4.使用SqlSession创建Dao接口的代理对象 IUserDao userDao=session.getMapper(IUserDao.class); //5.使用代理对象执行方法 List<User> users=userDao.findAll(); for (User user:users){ System.out.println(user); } //6.释放资源 session.close(); in.close(); }
-
Dubbo的注解配置和XML配置
2022-03-23 16:38:57dubbo的注解配置和XML配置dubbo注解配置
dubbo的注解配置,服务提供者和消费者都需要进行包扫描
服务提供者
服务消费者
服务提供者方加上Service注解,注册服务,以便消费者使用
服务消费者方加上Reference注解,远程调用服务
dubbo的XML配置
服务提供者
服务消费者
然后就不需要写Service注解和Reference,但需要自动注入的注解 (@Autowired)
-
web.xml配置文件详解
2020-05-14 10:34:26web.xml配置文件详解 web.xml表头文件详解: xml的版本和解码方式: <?xml version="1.0" encoding="UTF-8"?> servlet版本,固定写死: <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ... -
apollo配置中心实现logback.xml配置管理与热更新
2019-12-08 15:07:55因为apollo配置中心不支持.xml配置的写入,文档说明了当logback.xml文件放在apollo配置中心时,如何通过读取apollo缓存在本地的文件,实现日志配置的写入与热更新,实现通过apollo对logback.xml配置进行管理。... -
详解Spring项目中的web.xml配置文件
2020-10-13 17:21:47SpringMVC是创建java web项目时比较常用的框架,其中web.xml配置文件是更不可少的,那么首先先了解java web中的web.xml文件。web.xml的学名叫做部署描述文件(DD),它不是Spring所特有的,而是在Servlet规范中定义... -
Spring MVC 的 xml 配置详解
2019-05-28 17:39:57文章目录Spring 配置 properties的多种方法1.PropertyPlaceholderConfigurer2. 标签 Spring 配置 properties的多种方法 1.PropertyPlaceholderConfigurer PropertyPlaceholderConfigurer是个bean工厂后置处理器的... -
spring配置:xml配置和java注解配置对比
2018-08-16 16:55:30虽然spring早就推出...这里展示3种spring配置文件,来对比xml配置和java注解配置的区别,分别是spring\mvc\shiro的配置 先说总结: 对比2种配置方式会发现xml方法更繁琐(xml那恶心的头部约束),拿shiro来说... -
logback-spring.xml配置详解
2021-10-23 12:19:44日志配置文件,官方文档建议使用-spring 命令格式的配置,日志框架不直接加载,由SpringBoot解析日志配置,如:logback-spring.xml。如果直接定义为logback.xml 将直接被日志框架识别。下面详细解释下logback-spring... -
Mybatis-config.xml配置文件基础配置详解
2020-11-07 22:46:27mybatis-config.xml是MyBatis核心配置文件,该文件配置了MyBatis的一些全局信息,包含数据库连接信息和Mybatis运行时所需的各种特性,以及设置和影响Mybatis行为的一些属性 二、Mybatis核心配置文件结构 ... -
log4j.xml配置文件详解
2019-06-26 22:50:55log4j.xml配置文件详解 一 log4j.xml 配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration> <!-- 将日志信息输出到... -
tomcat服务器的Server.xml配置详解
2018-09-10 11:07:37Server.xml是tomcat一个重要的配置文件,下面结合网上看到的和自己使用经验来做的总结,有什么不对的希望各位大佬明确指出,小弟不胜感激。 server.xml实例: 一: 1、最外层是<server></server>... -
Java Web工程中的web.xml配置文件
2018-12-18 08:57:01Java Web工程中的web.xml配置文件 前言 1. xml文件 xml文件:Extentsible Markup Language即可扩展标记语言,是用来定义其它语言的一种元语言,其前身是SGML(标准通用标记语言)。xml文件是互联网数据传输的重要... -
springboot @ImportResource 加载(导入)外部xml配置文件
2020-12-06 18:04:23我们知道,传统的spring 一些 bean的信息(比如扫描配置,数据库配置等等)都是放在xml配置文件里面。然后启动服务的时候就通过导入这个配置文件完成spring容器的注入。 这种情况大多出现在对一些老旧项目进行改造的... -
logback.xml配置详细说明
2020-09-10 14:04:09原文链接: ... 配置查询1: <?xml version="1.0" encoding="utf-8" ?> <!-- 从高到地低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL --> <!-- 日志. -
Spring boot AOP通过 XML 配置文件声明
2020-06-19 16:09:48通过 XML 配置文件声明 在前两篇博文和示例中,我们已经展示了如何通过注解配置去声明切面,下面我们看看如何在 XML 文件中声明切面。下面先列出 XML 中声明 AOP 的常用元素: AOP配置元素 用途 aop:advisor ... -
springboot-mybatis xml配置
2018-05-06 12:47:07本机主要介绍mybatis通过xml配置方式操作数据库,这也是项目开发中通用的一种,将sql编写在xml中,便于维护、开发、升级等,不需要做太多的改动即可使用。 创建maven项目 在pom.xml中引入依赖包 xml version ="1.0... -
springboot读取xml配置文件
2020-06-23 21:39:20xml文件内容 在src/main/resources下面添加《operator-config.xml》文件,文件内容如下: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <operator-config group="business"> <operator... -
java web工程web.xml配置详解
2018-09-06 11:20:01当启动一个WEB项目时,容器包括(JBoss、Tomcat等)首先会读取项目web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常地被启动起来。 1. 启动WEB项目的时候,容器首先会去它的配置文件web.xml... -
C# 保存和读取XML配置文件
2008-12-20 13:50:18应用程序中,经常需要保存一些用户的设置...这个代码就是用C#写的保存和读取程序运行目录下的XML配置文件的程序,配置文件也会自动生成,很灵活的。 共享给大家,大家可以很方便的调用,然后保存和读取自己的程序设置。 -
java web项目web.xml配置文件详解
2019-07-12 23:22:26这篇文章是由一个默认页面怎么配置引发的一系列问题。是时候补基础了,谢谢带我小哥哥给我指出的问题,还有小哥哥们的指导。哈哈哈哈,切入正题吧! 虽然已经自己动手写过很多个web项目了(参加比赛,写着玩的,... -
nacos作为配置中心兼容xml配置文件
2019-09-13 06:15:38最近公司想要用配置中心,因为公司用的有传统的spring项目,有springboot项目,为了兼容都能够采用配置中心,做了一些尝试,经过比较还是倾向于使用nacos,传统dubbo采用spring方式读取xml读取配置文件的方式启动,... -
Ehcache(02)——ehcache.xml配置简介
2017-05-09 10:31:51ehcache.xml简介 ehcache.xml文件是用来定义Ehcache的配置信息的,更准确的来说它是定义CacheManager的配置信息的。根据之前我们在《Ehcache简介》一文中对CacheManager的介绍我们知道一切Ehcache的应用都是从... -
Spring整合Dubbo简易教程(一)-基于XML配置实现
2021-06-03 16:27:15应用程序可以通过XML、 注解和API这3种方式来编写, 本节教程主要介绍基于XML的实现。 所有的Dubbo服务接口都可以直接通过配置对外暴露, 但用户不需要额外编写服务暴露的代码, 因为这些都被Dubbo框架隐藏了, ... -
pom.xml、web.xml、springmvc.xml配置文件分别都配置什么?(通俗易懂)
2019-10-14 18:00:43使用SpringMVC框架虽然使用注解可以免去我们很多的配置,但是光使用注解是不能完全取代所有的配置问价的,使用SpringMVC主要有三种配置文件,pom.xml、web.xml、springmvc.xml三种,下面我们分别说一下三种配置文件... -
tomcat-web.xml配置
2018-05-18 23:56:59web.xml配置什么是Web.xmlWeb.xml是Web应用部署的重要的描述条件,它支持的元素及属性来自于Servlet规范定义。在tomcat中,Web应用的部署描述信息文件包括tomcat/conf/web.xml以及Web应用的WEB-INF/web.xml下的文件... -
Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
2019-03-08 11:22:12二、再看web.xml中的配置情况。 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="... -
xml配置文件基础
2018-09-09 18:47:13xml:eXtensible Markup Language,可拓展标记语言。作用主要是做配置文件和数据传输载体。本文只说明它做配置文件的知识。下面介绍它的文档结构、元素&amp;amp;amp;amp;amp;属性、注释&amp;amp;amp...