SpringMVC+Spring4+hibernate5使用java类配置spring和springmvc环境(不用配置web.xml)
来源:互联网 发布:知之者不如好之者的者 编辑:程序博客网 时间:2024/06/14 13:07
使用Spring提供的@Configuration注解配置Spring的应用上下文
//spring应用上下文配置类@Configuration@ComponentScan(basePackageClasses={SpringConfiguration.class},excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})//声明包扫描,并将@EnableWebMvc注解声明的类扫描包除去@EnableTransactionManagement@ImportResource(locations={"classpath:beans.xml"})@PropertySource({"classpath:jdbc.properties"})//导入peoperties文件public class SpringConfiguration { /** *简单配置数据源和hibernate的SessionFactory */ @Autowired private Environment env; @Bean(name="sessionFactory") public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sfb=new LocalSessionFactoryBean(); sfb.setDataSource(dataSource()); sfb.setPackagesToScan("org.learn.sshmvc.model"); Properties prop=new Properties(); prop.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); prop.setProperty("hibernate.show_sql", "true"); prop.setProperty("hibernate.hbm2ddl.auto", "update"); prop.setProperty("hibernate.format_sql", "false"); sfb.setHibernateProperties(prop); return sfb; } @Bean(destroyMethod="close") public BasicDataSource dataSource() { BasicDataSource dataSource=new BasicDataSource(); dataSource.setUsername(env.getProperty("jdbc.username")); dataSource.setPassword(env.getProperty("jdbc.password")); dataSource.setUrl(env.getProperty("jdbc.url")); dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); dataSource.setInitialSize(1); dataSource.setMinIdle(1); dataSource.setMaxIdle(20); return dataSource; } @Bean(name="transactionManager") @Autowired @Required public HibernateTransactionManager transactionManager(SessionFactory s) { HibernateTransactionManager txManager=new HibernateTransactionManager(); txManager.setSessionFactory(s); return txManager; }}
再进行和SpringMVC相关的viewReslover和Contorller的配置
@Configuration@EnableWebMvc//声明为mvc配置@ComponentScan(basePackages={"org.learn.sshmvc.controller"})//扫描web controllerpublic class WebConfig extends WebMvcConfigurerAdapter{ /** *继承WebMvcConfigurerAdapter类 */ @Bean//配置InternalResourceViewResolver,和xml配置一样 public ViewResolver viewResolver() { InternalResourceViewResolver resolver=new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } @Override//重写configureDefaultServletHandling方法,并允许静态资源直接访问 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }}
Web环境初始化的类,相当于在web.xml配置spring的监听器和DispatcherServlet(只有在Servlet3.0后的环境中才能使用)
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;/** * 继承AbstractAnnotationConfigDispatcherServletInitializer * 抽象类覆盖三个方法 */public class WebappInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override//配置Spring应用上下文的配置类 protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringConfiguration.class}; } @Override//配置web controller和view的配置类 protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{WebConfig.class}; } @Override//相当于在DispatcherServlet中配置路径"/" protected String[] getServletMappings() { return new String[]{"/"}; }}
这样就实现了不用再web.xml中配置spring了
0 0
- SpringMVC+Spring4+hibernate5使用java类配置spring和springmvc环境(不用配置web.xml)
- java注解方式(不用xml)配置web框架spring+SpringMVC
- springmvc web.xml spring-servlet.xml 配置
- 在web.xml中配置,使用配置类替代springmvc.xml和applicationContext.xml
- SpringMVC环境配置之web.xml
- spring 和springmvc 在 web.xml中的配置
- spring4+hibernate5+springmvc整合
- springmvc+spring的web.xml配置
- web.xml中spring/springmvc配置详解
- SpringMVC与Spring整合,配置web.xml
- SpringMVC web.xml配置
- SpringMVC web.xml配置
- Spring4.25+SpringMVC+MyBatis3.2环境配置
- SpringMVC如何用Java类配置(不通过web.xml和XML方式)
- SpringMVC如何用Java类配置(不通过web.xml和XML方式)
- springmvc项目不用配置application.xml文件,web.xml不用加载contextlistener
- springmvc之web.xml配置
- web.xml配置,基于SpringMVC
- DP--直线的不同交点数可能的情况
- oracle分区表详解
- android四大组件---Service
- JDK和JRE的区别是什么?
- mybatis实现关联表查询
- SpringMVC+Spring4+hibernate5使用java类配置spring和springmvc环境(不用配置web.xml)
- Xavier initialization
- 字节序
- leetcode:4. Median of Two Sorted Arrays
- Linux下通过SSH远程连接BAE
- 异常
- Android WebView Js 交互以及Js传参
- hdu 2049 错排问题 (简单)
- 使用dex2jar和jd-gui反编译apk得到Java源代码