使用Annotations构建Spring MVC应用程序的资源

时间:2015-08-26 14:30:04

标签: spring spring-mvc configuration annotations

我最近加入的公司开发了所有基于Spring MVC的项目,充分利用了Annotations,并且很少使用XML(我目前正在开发的项目仅使用tiles.xml进行tile配置)。

我过去经历的绝大多数书籍和教程都专注于XML配置。为了加快开发团队的速度,我参与了“逆向工程”#34;在我的项目中搜索我不熟悉的东西。

显然,这是耗时且适得其反的,因为我已经分散了各个方面的参考(如何在一个教程中配置bean,如何在另一个教程中配置解析器等)。

官方的Spring文档在许多方面都证明非常有用,但我更喜欢阅读基于Annotations的教程/系列教程/书籍,并使用官方文档进行参考/交叉检查。

有什么建议吗?

1 个答案:

答案 0 :(得分: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[] { "/" };
}
}

配置弹簧mvc的AppConfig.java

@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;
}
}

和休眠配置

@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;
}
}

由于