栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring MVC,两个配置实例,而不是一个

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring MVC,两个配置实例,而不是一个

这里的问题是,您正在为Servlet和根上下文使用相同的配置。这就是为什么您有两个配置实例。在Spring
MVC中,您有2个上下文,即servlet上下文和根上下文。servlet上下文用于控制器,而根上下文用于业务对象和服务。

如果您不想使用XML,请创建两个配置类。像这样的东西:

根上下文

@Configuration@EnableTransactionManagement@ComponentScan("pl.mbrnwsk.sklep")public class AppConfiguration {    public String hbm2ddl_auto = "update";    public AppConfiguration(){        System.out.println("AppConfiguration");    }    @Bean    public DataSource dataSource() {        DriverManagerDataSource ds = new DriverManagerDataSource();        ds.setDriverClassName("org.hsqldb.jdbcDriver");        ds.setUrl("jdbc:hsqldb:file:/SklepDB/");        ds.setUsername("SA");        ds.setPassword("");        return ds;    }    @Bean    public SessionFactory sessionFactory() {        LocalSessionFactoryBuilder ss = new LocalSessionFactoryBuilder(dataSource());        ss.scanPackages("pl.mbrnwsk.sklep.model");        ss.setProperty("hibernate.show_sql", "true");        ss.setProperty("hibernate.hbm2ddl.auto", hbm2ddl_auto);        ss.setProperty("hibernate.dialect",     "org.hibernate.dialect.HSQLDialect");        return ss.buildSessionFactory();    }    @Bean    public PlatformTransactionManager txManager(){        return new HibernateTransactionManager(sessionFactory());       }}

Servlet上下文

@Configuration@ComponentScan("pl.mbrnwsk.sklep.controller")public class ServletConfiguration {    public AppConfiguration(){        System.out.println("ServletConfiguration");    }    @Bean    public ViewResolver viewResolver(){        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();        viewResolver.setPrefix("/");        viewResolver.setSuffix(".jsp");        return viewResolver;    }}

Web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"        version="3.0">      <!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContextinstead of the default XmlWebApplicationContext -->      <context-param>          <param-name>contextClass</param-name>          <param-value>   org.springframework.web.context.support.AnnotationConfigWebApplicationContext          </param-value>      </context-param>      <!-- Configuration locations must consist of one or more comma- or space-delimitedfully-qualified @Configuration classes. Fully-qualified packages may also bespecified for component-scanning -->      <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>pl.mbrnwsk.sklep.config.AppConfiguration</param-value>      </context-param>      <!-- Bootstrap the root application context as usual using ContextLoaderListener -->      <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>      <!-- Declare a Spring MVC DispatcherServlet as usual -->      <servlet>          <servlet-name>dispatcher</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext    instead of the default XmlWebApplicationContext -->          <init-param>   <param-name>contextClass</param-name>   <param-value>       org.springframework.web.context.support.AnnotationConfigWebApplicationContext   </param-value>          </init-param>          <!-- Again, config locations must consist of one or more comma- or space-delimited    and fully-qualified @Configuration classes -->          <init-param>   <param-name>contextConfigLocation</param-name>   <param-value>pl.mbrnwsk.sklep.config.ServletConfiguration</param-value>          </init-param>      </servlet>      <!-- map all requests for / to the dispatcher servlet -->      <servlet-mapping>          <servlet-name>dispatcher</servlet-name>          <url-pattern>/</url-pattern>      </servlet-mapping></web-app>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/635757.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号