1:springmvc.xml配置要点
一般它主要配置Controller的组件扫描器和视图解析器
下为: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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<!-- 使用注解开发,不用配置controller,需要配置一个组件扫描器 -->
<context:component-scan base-package="com.edu.test.controller"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根目录到指定目录一端路径 ,建议指定浅一点的目录-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2:applicationContext.xml配置要点(在web.xml文件需要加<listener>)
下为:applicationContext.xml文件
<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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置组件扫描器,使用注解方式开发,不用配置dao和service -->
<!-- 在springmvc.xml文件中也可以配置这个属性 -->
<context:component-scan base-package="com.edu.test"/>
<!-- 数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- 配置session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置AOP通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性 -->
<tx:attributes>
<!-- 添加事务管理的方法 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP,为添加事务管理的操作配置AOP -->
<aop:config>
<!-- 引入的Spring定义的事务通知,需要使用aop:advisor -->
<!-- 下面难 -->
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.edu.test.service.*.*(..))"
/>
</aop:config>
</beans>
3:在web.xml文件中,将springmvc.xml和applicationContext.xml一起引入
下为:web.xm文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 中央控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置Spring提供的字符编码过滤器 -->
<filter>
<filter-name>SpringCharacterEncodingFilter</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>SpringCharacterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
以上内容转载自https://www.cnblogs.com/kaiwen1/p/6864458.html
spring和springmvc父子容器
在说父子容器之前,咱们首先了解什么是容器:
java容器:
可以管理对象声明周期,对象间的依赖关系。还可以配置对象名称,属性,产生方式等。不用程序员自己编写程序来管理。
java中常用的容器类有:List、HashMap、HashTable等。
spring容器:
借用以为博主的总结:
spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口。他们都可代表Spring容器,Spring容器是生成Bean实例的工厂,并且管理容器中的Bean。
Bean是Spring管理的基本单位,在基于Spring的Java EE应用中,所有的组件都被当成Bean处理,包括数据源、hibernate的SessionFactory、事务管理器等。在Spring中,Bean的是一个非常广义的概念,任何的Java对象、Java组件都被当成Bean处理。
了解了什么是容器,接下来就进入正题:
在一个项目中,容器不一定只有一个,就拿我正在学习的ssm框架来说:spring可以包含多个容器,比如说springmvc容器。在搭建项目的时候我们做了好多的配置:
比如:在springmvc.xml中配置了一个扫描包,扫描controller中的注解。(后边的mvc:annotation-driven代表使用注解驱动,可以不写)
又比如:在service.xml中配置了扫描包,要扫描service中的注解
那问题就来了,既然spring容器是一个大的容器,那我可不可以,写一个全局扫描包把service/dao/controller都扫描到spring容器中呢?这样会不会更方便一点不用配置那么多的xml文件?
针对这个问题,我们一起来学习一下:
Spring 是一个容器,springmvc也是一个容器。他们两者之间是父子容器关系。springmvc是子容器,包含在spring容器中。在项目中的配置如下:
我们在web.xml中配置了一个spring容器:用Listener加载初始化srping容器(这是一个大的容器)
还配了一个springmvc的前端控制器:(这是一个包含在springmvc中的一个容器。而springmvc又包含在spring容器中)
就相当于:service.dao在spring容器中,controller在springmvc容器中。
父子容器约定如下:
1、子容器可以访问父容器中的对象:
也就是service 可以注入到controller中。
但反过来,controller就不可以注入到service中。
当然,如果controller和service在一个容器中,就可以注入。
2、父容器不可以访问子容器中的对象:
如果我们在spirng中配置一个全局的扫描包,spring容器会把service,controller,dao都扫描放入spring这个大的容器中。这样导致,springmvc中就没有对象了,页面访问找不到对应的controller,就会报404错误。
相反,如果我们在springmvc中写一个扫描包,把service、dao都注入到springmvc中,(也就是把spingmvc.xml中扫描包中的.controller去掉)是没有问题的。因为springmvc也是一个spring容器。
3、我们为什么要用spring加springmvc呢?
当然是为了系统以后的扩张方便。spring可以整合多个框架,这样我们后边如果想再加入一个struts到spring容器里面,也是可以的。
4、我们可不可以把事务配置到controller中?
如果是spirng+springmvc这个架构是不可以,因为事务管理器是在spring中配置的,在父容器中是访问不了springmvc子容器中的controller的。所以不可以。
但是如果把所有扫描包都配置到springmvc中,那么在controller是可以配置事务的。
所以结论如下:不可以在service.xml中配置一个全局扫描包来使用。
了解了父子容器,我们在搭建框架的时候,就会很容易的知道,我要配置什么,配置到哪些地方。就会更深一步的了解框架的原理。
课外拓展:
我在上网查询父子容器的时候,发现了两派配置容器的使用。
第一派:网友称为传统派,就是spring+springmvc的配置。和我上面讲的一样。没什么可说的
第二派:叫激进派,就是把所有东西都配置到springmvc中,不要service或者dao层的接口,把做项目一全套的数据源啦,事务啦,dao啦,service啦,都配置到子容器中。