首页 > 代码库 > (原)SpringMVC全注解不是你们那么玩的
(原)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:
@Controllerpublic 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>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。