- <tx:annotation-driven />声明性事务等配置无法用简单代码来实现
- web.xml无法去掉
Java代码
- @WebServlet(urlPatterns="/hello")
- public class HelloServlet extends HttpServlet {}
Java代码
- @HandlesTypes(WebApplicationInitializer.class)
- public class SpringServletContainerInitializer implements ServletContainerInitializer {
- public void onStartup(Set<Class<?>> webAppInitializerClasses,
- ServletContext servletContext) throws ServletException {
- //implemention omitted
- }
- }
Java代码
- public class WebInit implements WebApplicationInitializer {
- @Override
- public void onStartup(ServletContext sc) throws ServletException {
- sc.addFilter("hibernateFilter", OpenSessionInViewFilter.class).addMappingForUrlPatterns(null, false, "/*");
- // Create the 'root' Spring application context
- AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
- root.scan("septem.config.app");
- // Manages the lifecycle of the root application context
- sc.addListener(new ContextLoaderListener(root));
- AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
- webContext.setConfigLocation("septem.config.web");
- ServletRegistration.Dynamic appServlet = sc.addServlet("appServlet", new DispatcherServlet(webContext));
- appServlet.setLoadOnStartup(1);
- appServlet.addMapping("/");
- }
- }
Java代码
- @Configuration
- @ComponentScan(basePackages="septem.controller")
- @EnableWebMvc
- public class WebConfig {
- }
Java代码
- @Configuration
- @EnableTransactionManagement
- public class DataConfig {
- public AnnotationSessionFactoryBean sessionFactory() {
- AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
- sessionFactoryBean.setDataSource(dataSource());
- sessionFactoryBean.setNamingStrategy(new ImprovedNamingStrategy());
- sessionFactoryBean.setPackagesToScan("septem.model");
- sessionFactoryBean.setHibernateProperties(hProps());
- return sessionFactoryBean;
- }
- private DataSource dataSource() {
- BasicDataSource source = new BasicDataSource();
- source.setDriverClassName("org.hsqldb.jdbcDriver");
- source.setUrl("jdbc:hsqldb:mem:s3demo_db");
- source.setUsername("sa");
- source.setPassword("");
- return source;
- }
- public HibernateTransactionManager transactionManager() {
- HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
- hibernateTransactionManager.setSessionFactory(sessionFactory().getObject());
- return hibernateTransactionManager;
- }
- private Properties hProps() {
- Properties p = new Properties();
- p.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
- p.put("hibernate.cache.use_second_level_cache", "true");
- p.put("hibernate.cache.use_query_cache", "true");
- p.put("hibernate.cache.provider_class",
- "org.hibernate.cache.EhCacheProvider");
- p.put("hibernate.cache.provider_configuration_file_resource_path",
- "ehcache.xml");
- p.put("hibernate.show_sql", "true");
- p.put("hibernate.hbm2ddl.auto", "update");
- p.put("hibernate.generate_statistics", "true");
- p.put("hibernate.cache.use_structured_entries", "true");
- return p;
- }
- }
Java代码
- @Configuration
- @ComponentScan(basePackages="septem.service")
- public class AppConfig {
- }
Java代码
- @Transactional
- public class GreetingService {
- @Autowired
- private SessionFactory sessionFactory;
- @Transactional(readOnly=true)
- public String greeting() {
- return "spring without xml works!";
- }
- @Transactional(readOnly=true)
- public Book getBook(Long id) {
- return (Book) getSession().get(Book.class, id);
- }
- @Transactional(readOnly=true)
- public Author getAuthor(Long id){
- return (Author) getSession().get(Author.class, id);
- }
- public Book newBook() {
- Book book = new Book();
- book.setTitle("java");
- getSession().save(book);
- return book;
- }
- public Author newAuthor() {
- Book book = newBook();
- Author author = new Author();
- author.setName("septem");
- author.addBook(book);
- getSession().save(author);
- return author;
- }
- private Session getSession() {
- return sessionFactory.getCurrentSession();
- }
- }
Xml代码
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.1.1</version>
- <configuration>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
Java代码
- appServlet.addMapping("/s3/");
Java代码
- public SessionFactory sessionFactory() {
- org.hibernate.cfg.Configuration config = new org.hibernate.cfg.Configuration();
- config.setProperties(hProps());
- config.addAnnotatedClass(Book.class);
- return config.buildSessionFactory();
- }
Java代码
- public AnnotationSessionFactoryBean sessionFactory() {
- AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
- sessionFactoryBean.setDataSource(dataSource());
- sessionFactoryBean.setNamingStrategy(new ImprovedNamingStrategy());
- sessionFactoryBean.setPackagesToScan("septem.model");
- sessionFactoryBean.setHibernateProperties(hProps());
- return sessionFactoryBean;
- }
Shell代码
- svn checkout http://spring-no-xml.googlecode.com/svn/trunk/ spring_no_xml
- cd spring_no_xml
- mvn integration-test