1基于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">
<!--把对象的创建交给spring来管理-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
         scope="" init-method="" destroy-method=""   >
             <property name="" value/ref=""></property>
         </bean>
</beans>

2注解的context名称空间

<?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">

    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中-->
    <context:component-scan base-package="com.itheima"></context:component-scan>
</beans>

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

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

spring框架的xml配置文件头部和springmvc的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">

</beans>

context配置:扫描注解

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

AOP配置:切面约束

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

TX配置:事务的通知

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

MVC配置:MVC的命名空间

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

dubbo配置

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

 spring-security配置

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

案例:

基于XML的IOC案例

Maven

<dependencies>
        <dependency>
            <!--spring容器-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <!--生产Connection对象的工厂类-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <!--在spring框架中实现事务管理功能-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <!--mysql连接器:帮助java程序操作mysql的驱动程序-->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
        <dependency>
            <!--支持切入点表达式等等-->
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!--spring整合junit-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

bean.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">
    <!--配置业务层对象(service)-->
    <bean id="accountService" class="com.itheima.service.impl.AccountImpl">
        <!--注入dao对象:set方法注入-->
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <!--配置dao对象-->
    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <!--注入QueryRunner对象-->
        <property name="runner" ref="runner"></property>
    </bean>
    <!--配置QueryRunner对象-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--注入数据源:构造方法注入-->
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>
    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接数据库的必备信息-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property>
        <property name="user" value="root"></property>
        <property name="password" value="密码"></property>
    </bean>
</beans>

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

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

操作数据库的Maven
<dependencies>
        <dependency>
            <!--spring的核心容器-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <!--对jdbc进行了薄薄的封装-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <!--和事务相关-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <!--数据库-->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
    </dependencies>

4springMVC框架

Maven

<?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.kaikeba</groupId>
    <artifactId>springmvc01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <!--依赖-->
        <dependency>
            <!--这个jar包中已经包含了spring-context jar包中的所有内容了,所以就不需要再导入spring-context jar包了-->
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
           <!-- springMVC底层是封装的servlet,所以这个包必须要导入-->
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope><!--因为servlet在tomcat中已经提供了,这里代表未来打包的时候不会把javax.servlet-api打包进去
            如果这个不写的话,未来运行会报错的-->
        </dependency>
    </dependencies>

    <build>
        <!--插件-->
        
            <plugin>
                <!--tomcat插件-->
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <port>8888</port>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml配置 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--进行spring的配置-->
    <context-param><!--配置文件的上下文参数-->
        <!--这里是对spring配置文件的位置进行说明-->
        <param-name>contextConfigLocation</param-name><!--contextConfigLocation这个参数是不可以变的-->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <!--监听器-->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--springmvc的前端/核心/中央控制器配置-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name><!--这里叫什么都可以-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <!--如果没有这段代码,那么系统会默认查找WEB-INF文件夹下的名为dispatcherServlet-servlet.xml的配置文件,没有会报错-->
            <!--这里查找的文件名为:servlet-name的参数+"-servlet",
                比如这里的servlet-name值为springMVC,那么配置文件就是springMVC-servlet.xml-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
        <!--表明创建的对象是不是在容器创建的时候创建,如果没有写就是什么时候用,什么时候创建。写了就是立即创建-->
        <!--当有多个servlet时,数值越小优先级越高,当为负数时,就是什么时候用,什么时候创建-->
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>