SpringMVC全注解不是你们那么玩的

前言:忙了段时间,忙得要死要活,累了一段时间,累得死去活来。

偶尔看到很多零注解配置SpringMVC,其实没有根本的零注解。

1)工程图一张:

web.xml在servlet3.0里面已经被注解完全替代,但是spring里面的DispatcherServlet并没有被使用,本打算修改下源码弄成3.0的,奈何没啥时间。

这是一个标准的SpringMVC,重点是AppConfig与DBConfig,在Web.xml里面申明两个类的配置路径:

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.test.commom.AppConfig</param-value>
</init-param>
<!-- use annotation replace xml configuration. @Configuration class is required. -->
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

AppConfig:

@Configuration
@ComponentScan(basePackageClasses = AppConfig.class)
@EnableTransactionManagement //The code equals aop config or provider annotation transaction.
@EnableAspectJAutoProxy
@PropertySource({"classpath:site-jdbc.properties"})
public class AppConfig extends DBConfig { /**
* 国际化
* @return
*/
@Bean
@Qualifier("messageSource")
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource bundleMessageSource = new ResourceBundleMessageSource();
bundleMessageSource.setBasename("i18n.u1wan-i18n");
bundleMessageSource.setUseCodeAsDefaultMessage(true);
return bundleMessageSource;
} /**
* file upload
* @return
*/
@Bean
public CommonsMultipartResolver getCommonsMultipartResolver() {
return new CommonsMultipartResolver();
} @Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
return localeResolver;
} @Bean
public HessianProxyFactory loadHessianProxyFactory() {
HessianProxyFactory hessianProxyFactory = new HessianProxyFactory();
return hessianProxyFactory;
}
/**
* 定义spring MVC返回显示视图
* @return
*/
@Bean
public TilesViewResolver viewResolver() {
return new TilesViewResolver();
} @Bean
public LoginInterceptor loginInterceptor() {
return new LoginInterceptor();
} @Bean
public SystemInterceptor systemInterceptor() {
return new SystemInterceptor();
} @Bean
public PermissionsInterceptor permissionsInterceptor() {
return new PermissionsInterceptor();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor());
registry.addInterceptor(systemInterceptor());
registry.addInterceptor(permissionsInterceptor());
} /**
* 定义xml显示位置
* @return
*/
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setDefinitions(new String[] { "classpath*:config/tiles/page-tiles.xml", "classpath*:config/tiles/common-tiles.xml" });
return tilesConfigurer;
} /**
* 定义Spring MVC显示
* @return
*/
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
} }

DBConfig:

public class DBConfig extends DefaultWebConfig {

    @Inject
Environment env; /**
* 数据源
* @return
*/
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getRequiredProperty("jdbc.driver.name"));
dataSource.setUrl(env.getRequiredProperty("jdbc.writedb.proxy.url"));
dataSource.setUsername(env.getRequiredProperty("jdbc.username"));
dataSource.setPassword(env.getRequiredProperty("jdbc.password"));
dataSource.setTestOnBorrow(true);
dataSource.setValidationQuery("select 1");
return dataSource;
} @Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource())
.scanPackages(AppConfig.class.getPackage().getName());
builder.setProperty(org.hibernate.cfg.Environment.DIALECT, MySQL5Dialect.class.getName());
return builder.buildSessionFactory();
} @Bean
public MongoDBAccess mongoDBAccess() {
MongoDBAccess mongoDBAccess = new MongoDBAccess();
mongoDBAccess.setMongoServerIpAddress(env.getRequiredProperty("mongodb.ip"));
mongoDBAccess.setCollectionName(env.getRequiredProperty("mongodb.collection"));
mongoDBAccess.setMongoServerPort(Integer.parseInt(env.getRequiredProperty("mongodb.port")));
mongoDBAccess.setDbName(env.getRequiredProperty("mongodb.db"));
mongoDBAccess.initDB();
return mongoDBAccess;
} @Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
return localeResolver;
} /**
* hibernate事物
* @return
*/
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory());
return transactionManager;
} /**
* ORM 映射--hibernate
* @return
*/
@Bean
public HibernateAccess hibernateAccess() {
HibernateAccess hibernateAccess = new HibernateAccess();
hibernateAccess.setSessionFactory(sessionFactory());
return hibernateAccess;
} /**
* JDBC--性能要求高场合
* @return
*/
@Bean
public JDBCAccess jdbcAccess() {
JDBCAccess jDBCAccess = new JDBCAccess();
jDBCAccess.setDataSource(dataSource());
return jDBCAccess;
} @Bean(name = "hibernateTransaction")
public HibernateTransactionManager hibernateTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory());
return transactionManager;
}
}

通过类的配置,完全替代spring的配置文件,基本上不用配置文件,个人比较喜欢no配置文件的东西。

举例一个Controler:

@Controller
public class WebsiteController { @Inject
private WebsiteServer websiteServer; @RequestMapping(value = "/website/test", method = RequestMethod.GET)
public String test(Map<String, Object> map, WebsiteRequest request) {
map.put("test", "test");
return "test_page";
} @RequestMapping(value = "/website/testbody", method = RequestMethod.GET)
@ResponseBody
public String testBody(Map<String, Object> map) {
try {
websiteServer.test();
} catch (Exception e) {
e.printStackTrace();
}
return "test";
} }

在第一个方法中,会去找test_page这个配置试图,该试图对应一个页面

第二个方法,直接返回到body中。

在page-tiles.xml与common-tiles.xml中设置对应视图位置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd"> <tiles-definitions> <definition name="cookie-error" template="/WEB-INF/common/error/cookie-error.jsp"></definition> <definition name="default-403" extends="intranet-template">
<put-attribute name="main-content1" value="/WEB-INF/common/error/default-403.jsp" />
<put-attribute name="title" value="Forbidden" />
</definition> <definition name="default-404" template="/WEB-INF/common/error/default-404.jsp"></definition> <definition name="default-error" extends="intranet-template">
<put-attribute name="main-content1" value="/WEB-INF/common/error/default-500.jsp" />
<put-attribute name="title" value="Server Error" />
</definition> </tiles-definitions>

SpringMVC全注解的更多相关文章

  1. (原)SpringMVC全注解不是你们那么玩的

    前言:忙了段时间,忙得要死要活,累了一段时间,累得死去活来. 偶尔看到很多零注解配置SpringMVC,其实没有根本的零注解. 1)工程图一张: web.xml在servlet3.0里面已经被注解完全 ...

  2. SpringMVC 全注解实现 (1) servlet3.0以上的容器支持

    一. Spring MVC入门 1.1 request的处理过程 用户每次点击浏览器界面的一个按钮,都发出一个web请求(request).一个web请求的工作就像一个快递员,负责将信息从一个地方运送 ...

  3. 【转载】springMVC表单校验+全注解

    在这篇文章中,我们将学习如何使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS, ...

  4. springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

    自己从事java开发工作也有一年多了,自己却没有亲手搭建一个完整的框架.于是今天自己动手搭建一个,过程中遇到一些问题,倒腾了大半天终于搞定了. 现在给大家分享一下过程,自己也记录下来,以后学习参考使用 ...

  5. 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建

    概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...

  6. Spring3+SpingMVC+Hibernate4全注解环境配置

    Spring3+SpingMVC+Hibernate4全注解环境配置 我没有使用maven,直接使用Eclipse创建动态Web项目,jar包复制在了lib下.这样做导致我马上概述的项目既依赖Ecli ...

  7. Spring RESTful + Redis全注解实现恶意登录保护机制

    好久没更博了... 最近看了个真正全注解实现的 SpringMVC 博客,感觉很不错,终于可以彻底丢弃 web.xml 了.其实这玩意也是老东西了,丢弃 web.xml,是基于 5.6年前发布的 Se ...

  8. java spring mvc 全注解

    本人苦逼学生一枚,马上就要毕业,面临找工作,实在是不想离开学校.在老师的教导下学习了spring mvc ,配置文件实在繁琐,因此网上百度学习了spring mvc 全注解方式完成spring的装配工 ...

  9. spring4全注解web项目demo

    记得没接触框架的时候,写demo测试时真的很爽,新建web项目,然后随便写写servlet随便调试 框架越来越多,配置记不得了,整合容易出问题,集成新东西越来越少了,不敢动了. 这是个spring4的 ...

随机推荐

  1. CSS3图片轮播效果

    原文:CSS3图片轮播效果 在网页中用到图片轮播效果,单纯的隐藏.显示,那再简单不过了,要有动画效果,如果是自己写的话(不用jquery等),可能要费点时间.css3的出现,让动画变得不再是问题,而且 ...

  2. XStream 用法汇总

            XStream是一家Java对象和XML转换工具,很好很强大.它提供了所有的基本型.排列.收集和其他类型的支持,直接转换.因此XML在数据交换经常使用.对象序列化(和Java对象的序列 ...

  3. N-gram统计语言模型(总结)

    N-gram统计语言模型 1.统计语言模型 自然语言从它产生開始,逐渐演变成一种上下文相关的信息表达和传递的方式.因此让计算机处理自然语言.一个主要的问题就是为自然语言这样的上下文相关特性建立数学模型 ...

  4. jQuery中的.height()、.innerHeight()和.outerHeight()

    jQuery中的.height()..innerHeight()和.outerHeight()和W3C的盒模型相关的几个获取元素尺寸的方法.对应的宽度获取方法分别为.width()..innerWid ...

  5. Unity+NGUI打造网络图片异步加载和本地缓存工具(一)

    我们已经开发了在移动终端中,异步网络图片被装入多,在unity其中尽管AssetBundle存在,通常第一个好游戏的资源,然后加载到现场,但也有很多地方可以使用异步网络加载图像以及其缓存机制. 我也写 ...

  6. 基于STM32旋转编码器

    ..\..\SYSTEM\usart\usart.c(1): error:  #5: cannot open source input file "sys.h": No such ...

  7. 浅谈JavaScript中的字符串操作

      我想,最为一名开发人员,最实际开发过程中,任何一门语言在开发实际的项目的过程中,都是逃不开字符串的操作的下面笔者就自己日常开发过程中所用到的一些字符串的操作方法做一些陈述和总结,当然,如若读者觉得 ...

  8. Code Forces 414B 很不错的双手,以促进合规

    http://codeforces.com/problemset/problem/414/B 题目挺不错的.留个纪念,活动脑筋不错的题目 #include<iostream> #inclu ...

  9. C#操作Xml:linq to xml操作XML

    LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...

  10. Advance Installer安装问题

    一,在Advance Installer中注冊dll 1,首先将文件加入到Files And Folders中.此处以InstallValidate.dll为例. 2,在Custom Action处进 ...