SpringMVC容器和Spring Bean容器结合
2016-07-28 16:14
537 查看
创建项目springMVC管理controller层 springBean管理 dao service层
Spring是根容器,SpringMVC是其子容器,并且在Spring根容器中对于SpringMVC容器中的Bean是不可见的,而在SpringMVC容器中对于Spring根容器中的Bean是可见的,也就是子容器可以看见父容器中的注册的Bean,反之就不行。
解决两次加载bean的原因
1.MVC容器可以引用bean容器的bean
2.Bean容器不能引用MVC容器的bean
工程网络
![](https://img-blog.csdn.net/20160728161830585)
web.xml
Spring是根容器,SpringMVC是其子容器,并且在Spring根容器中对于SpringMVC容器中的Bean是不可见的,而在SpringMVC容器中对于Spring根容器中的Bean是可见的,也就是子容器可以看见父容器中的注册的Bean,反之就不行。
解决两次加载bean的原因
1.MVC容器可以引用bean容器的bean
2.Bean容器不能引用MVC容器的bean
工程网络
web.xml
<?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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringTest</display-name> <!-- 初始化mvc容器 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:resources/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 初始化bean容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:resources/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>applicationContext.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: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-4.2.xsd"> <context:component-scan base-package="com.hellojava"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"> </bean> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref=""></property> <property name="typeAliasesPackage" value="com.hellojava.entity"></property> <property name="mapperLocations" value="classpath:com/hellojava.mapper/*.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value=""></property> <property name="sqlSessionFactory" ref=""></property> </bean> </beans>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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <context:component-scan base-package="com.hellojava" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> <context:include-filter type="annotation" expression="org.springframework.stere 9222 otype.Repository"/> </context:component-scan> </beans>
相关文章推荐
- java多线程详细解析
- spring mvc路径匹配原则
- java后台返回json数据前台接收并解析(json)
- 新版本struts2的注册和登录校验
- 线性时间排序
- SVN中基于Maven的Web项目更新到本地过程详解
- spring AOP 自定义注解
- svn分支以及eclipse上的svn插件
- log4j
- Java中JComboBox下拉框和JButton不显示问题
- 再探HashTable和HashMap
- Spring Boot 构建应用——使用 Spring Data JPA
- Java编程中的equals方法使用全解
- Java:单例模式的七种写法
- Java中泛型小结
- kettle+java代码调用Webservice
- eclipse中maven项目交付svn忽略配置文件
- java不同的编码格式下字符串所占用的字节数
- Java基础概要总结
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法