2015-08-26 80 views
-1

我最近加入的公司開發了所有基於Spring MVC的項目,充分利用註釋和最少使用XML(我目前正在使用的項目僅使用tiles.xml進行tile配置)。構建資源使用註解的Spring MVC應用程序

我過去經歷的絕大多數書籍和教程都是關於XML配置。爲了跟上開發團隊的步伐,我通過搜索我在項目中不熟悉的東西,以「逆向工程」的方式工作。因爲我有各個方面的分散引用(如何在一個教程中配置一個bean,如何在另一個教程中配置解析器等),這顯然很費時並且適得其反。

官方的Spring文檔在很多領域已被證明非常有用,但我更願意閱讀基於註釋的教程/系列教程/書籍,並使用官方文檔進行參考/交叉檢查。

有什麼建議嗎?

回答

1

對我來說這真的很有用。 http://websystique.com/spring-4-mvc-tutorial/

而我的一個現有項目的配置可能會對您有所幫助。

AppInitializer.java而不是web.xml中

@Configuration 
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

@Override 
protected Class<?>[] getRootConfigClasses() { 
    return new Class<?>[] {AppConfig.class }; 
} 

@Override 
protected Class<?>[] getServletConfigClasses() { 
    return null; 
} 

@Override 
protected String[] getServletMappings() { 
    return new String[] { "/" }; 
} 
} 

AppConfig.java用於配置Spring MVC的

@EnableWebMvc 
@Configuration 
@ComponentScan(basePackages = "com.aaaa.bbbb") 
public class AppConfig extends WebMvcConfigurerAdapter { 

@Bean 
public ViewResolver viewResolver() { 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
    viewResolver.setViewClass(JstlView.class); 
    viewResolver.setPrefix("/WEB-INF/pages/"); 
    viewResolver.setSuffix(".jsp"); 
    return viewResolver; 
} 

public MessageSource messageSource() { 
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); 
    messageSource.setBasename("messages"); 
    return messageSource; 
} 
} 

和Hibernate配置

@Configuration 
@EnableTransactionManagement 
@PropertySource(value = { "classpath:application.properties" }) 
public class HibernateConfiguration { 
@Autowired 
private Environment environment; 

@Bean 
public LocalSessionFactoryBean sessionFactory() { 
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
    sessionFactory.setDataSource(dataSource()); 
    sessionFactory.setPackagesToScan(new String[] { "com.jjjj.kkkk.pojo" }); 
    sessionFactory.setHibernateProperties(hibernateProperties()); 
    return sessionFactory; 
} 

@Bean(name = "dataSource") 
public DataSource dataSource() { 
    DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); 
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); 
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username")); 
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password")); 
    return dataSource; 
} 

private Properties hibernateProperties() { 
    Properties properties = new Properties(); 
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); 
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql")); 
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql")); 
    properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.automaticschema")); 
    properties.put("hibernate.search.default.directory_provider", 
      environment.getRequiredProperty("hibernate.search.default.directory_provider")); 
    properties.put("hibernate.search.default.indexBase", 
      environment.getRequiredProperty("hibernate.search.default.indexBase")); 

    return properties; 
} 

@Bean 
@Autowired 
public HibernateTransactionManager transactionManager(SessionFactory s) { 
    HibernateTransactionManager txManager = new HibernateTransactionManager(); 
    txManager.setSessionFactory(s); 
    return txManager; 
} 
} 

感謝