当前位置:   article > 正文

Spring的xml文档配置_spring的xml文件配置

spring的xml文件配置

1基于XML的注解配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--把对象的创建交给spring来管理-->
  6. <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
  7. scope="" init-method="" destroy-method="" >
  8. <property name="" value/ref=""></property>
  9. </bean>
  10. </beans>

2注解的context名称空间

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
  10. context名称空间和约束中-->
  11. <context:component-scan base-package="com.itheima"></context:component-scan>
  12. </beans>

base-package属性,指定了spring要扫描的包及其子包的所有类的注解

3spring框架中XML文件头部常用配置

spring框架的xml配置文件头部和springmvc的xml配置文件头部是一样的

默认配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. </beans>

context配置:扫描注解

  1. xmlns:context="http://www.springframework.org/schema/context"
  2. http://www.springframework.org/schema/context
  3. https://www.springframework.org/schema/context/spring-context.xsd

AOP配置:切面约束

  1. xmlns:aop="http://www.springframework.org/schema/aop"
  2. http://www.springframework.org/schema/aop
  3. http://www.springframework.org/schema/aop/spring-aop.xsd

TX配置:事务的通知

  1. xmlns:tx="http://www.springframework.org/schema/tx"
  2. http://www.springframework.org/schema/tx
  3. http://www.springframework.org/schema/tx/spring-tx.xsd

MVC配置:MVC的命名空间

  1. xmlns:mvc="http://www.springframework.org/schema/mvc"
  2. http://www.springframework.org/schema/mvc
  3. https://www.springframework.org/schema/mvc/spring-mvc.xsd

dubbo配置

  1. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  2. http://dubbo.apache.org/schema/dubbo
  3. http://dubbo.apache.org/schema/dubbo/dubbo.xsd

 spring-security配置

  1. http://www.springframework.org/schema/security
  2. http://www.springframework.org/schema/security/spring-security.xsd">

案例:

基于XML的IOC案例

Maven

  1. <dependencies>
  2. <dependency>
  3. <!--spring容器-->
  4. <groupId>org.springframework</groupId>
  5. <artifactId>spring-context</artifactId>
  6. <version>5.2.13.RELEASE</version>
  7. </dependency>
  8. <dependency>
  9. <!--生产Connection对象的工厂类-->
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-jdbc</artifactId>
  12. <version>5.2.13.RELEASE</version>
  13. </dependency>
  14. <dependency>
  15. <!--在spring框架中实现事务管理功能-->
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-tx</artifactId>
  18. <version>5.2.13.RELEASE</version>
  19. </dependency>
  20. <dependency>
  21. <!--mysql连接器:帮助java程序操作mysql的驱动程序-->
  22. <groupId>mysql</groupId>
  23. <artifactId>mysql-connector-java</artifactId>
  24. <version>8.0.27</version>
  25. </dependency>
  26. <dependency>
  27. <!--支持切入点表达式等等-->
  28. <groupId>org.aspectj</groupId>
  29. <artifactId>aspectjweaver</artifactId>
  30. <version>1.9.6</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>junit</groupId>
  34. <artifactId>junit</artifactId>
  35. <version>4.13.1</version>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <!--spring整合junit-->
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-test</artifactId>
  42. <version>5.0.2.RELEASE</version>
  43. </dependency>
  44. </dependencies>

bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--配置业务层对象(service)-->
  6. <bean id="accountService" class="com.itheima.service.impl.AccountImpl">
  7. <!--注入dao对象:set方法注入-->
  8. <property name="accountDao" ref="accountDao"></property>
  9. </bean>
  10. <!--配置dao对象-->
  11. <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
  12. <!--注入QueryRunner对象-->
  13. <property name="runner" ref="runner"></property>
  14. </bean>
  15. <!--配置QueryRunner对象-->
  16. <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
  17. <!--注入数据源:构造方法注入-->
  18. <constructor-arg name="ds" ref="dataSource"></constructor-arg>
  19. </bean>
  20. <!--配置数据源-->
  21. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  22. <!--连接数据库的必备信息-->
  23. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  24. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property>
  25. <property name="user" value="root"></property>
  26. <property name="password" value="密码"></property>
  27. </bean>
  28. </beans>

表现层web层->业务层service层->持久层dao层->数据库

因为这里没有用到web,所以xml的配置也是从service层开始的

  1. 操作数据库的Maven
  2. <dependencies>
  3. <dependency>
  4. <!--spring的核心容器-->
  5. <groupId>org.springframework</groupId>
  6. <artifactId>spring-context</artifactId>
  7. <version>5.2.13.RELEASE</version>
  8. </dependency>
  9. <dependency>
  10. <!--对jdbc进行了薄薄的封装-->
  11. <groupId>org.springframework</groupId>
  12. <artifactId>spring-jdbc</artifactId>
  13. <version>5.2.13.RELEASE</version>
  14. </dependency>
  15. <dependency>
  16. <!--和事务相关-->
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-tx</artifactId>
  19. <version>5.2.13.RELEASE</version>
  20. </dependency>
  21. <dependency>
  22. <!--数据库-->
  23. <groupId>mysql</groupId>
  24. <artifactId>mysql-connector-java</artifactId>
  25. <version>8.0.27</version>
  26. </dependency>
  27. </dependencies>

4springMVC框架

Maven

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.kaikeba</groupId>
  7. <artifactId>springmvc01</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>war</packaging>
  10. <properties>
  11. <maven.compiler.source>11</maven.compiler.source>
  12. <maven.compiler.target>11</maven.compiler.target>
  13. </properties>
  14. <dependencies>
  15. <!--依赖-->
  16. <dependency>
  17. <!--这个jar包中已经包含了spring-context jar包中的所有内容了,所以就不需要再导入spring-context jar包了-->
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring-webmvc</artifactId>
  20. <version>5.2.13.RELEASE</version>
  21. </dependency>
  22. <dependency>
  23. <!-- springMVC底层是封装的servlet,所以这个包必须要导入-->
  24. <groupId>javax.servlet</groupId>
  25. <artifactId>javax.servlet-api</artifactId>
  26. <version>4.0.0</version>
  27. <scope>provided</scope><!--因为servlet在tomcat中已经提供了,这里代表未来打包的时候不会把javax.servlet-api打包进去
  28. 如果这个不写的话,未来运行会报错的-->
  29. </dependency>
  30. </dependencies>
  31. <build>
  32. <!--插件-->
  33. <plugin>
  34. <!--tomcat插件-->
  35. <groupId>org.apache.tomcat.maven</groupId>
  36. <artifactId>tomcat7-maven-plugin</artifactId>
  37. <version>2.2</version>
  38. <configuration>
  39. <path>/</path>
  40. <port>8888</port>
  41. </configuration>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. </project>

web.xml配置 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--进行spring的配置-->
  7. <context-param><!--配置文件的上下文参数-->
  8. <!--这里是对spring配置文件的位置进行说明-->
  9. <param-name>contextConfigLocation</param-name><!--contextConfigLocation这个参数是不可以变的-->
  10. <param-value>classpath:applicationContext.xml</param-value>
  11. </context-param>
  12. <listener>
  13. <!--监听器-->
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16. <!--springmvc的前端/核心/中央控制器配置-->
  17. <servlet>
  18. <servlet-name>dispatcherServlet</servlet-name><!--这里叫什么都可以-->
  19. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  20. <init-param>
  21. <!--如果没有这段代码,那么系统会默认查找WEB-INF文件夹下的名为dispatcherServlet-servlet.xml的配置文件,没有会报错-->
  22. <!--这里查找的文件名为:servlet-name的参数+"-servlet",
  23. 比如这里的servlet-name值为springMVC,那么配置文件就是springMVC-servlet.xml-->
  24. <param-name>contextConfigLocation</param-name>
  25. <param-value>classpath:springmvc.xml</param-value>
  26. </init-param>
  27. <load-on-startup>1</load-on-startup>
  28. <!--表明创建的对象是不是在容器创建的时候创建,如果没有写就是什么时候用,什么时候创建。写了就是立即创建-->
  29. <!--当有多个servlet时,数值越小优先级越高,当为负数时,就是什么时候用,什么时候创建-->
  30. </servlet>
  31. <servlet-mapping>
  32. <servlet-name>dispatcherServlet</servlet-name>
  33. <url-pattern>*.do</url-pattern>
  34. </servlet-mapping>
  35. </web-app>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/309220
推荐阅读
相关标签