Tutorial 4 Application Context
You can find in this video tutorial. * What is application context file? * How to write an application context file? * How to set sessionFactory properties?
Application Context file is used to configure spring mvc application. It defines beans at root webapp context.
- <context:annotation-config/> - this element tells application to activate beans already registered in application context. It gives general support for annotation Like @Required, @Autowired, @PostConstructor.
- <context:component-scan base-package=”com”/> this element tells application to activate beans already registered in application context and scan package to find, register beans in application context.
- <mvc:annotation-driven /> this element gives application to support for annotation-driven MVC Controllers Like @RequestMapping, @Controller, @RequestBody, @ResponseBody.
- <bean> is a java bean. Java Bean is a simple class with private fields and setter, getter methods. In XML we can defined it for usage. So here in this file three of beans tags are used with properties. Each property is like Class Private Field.
- DataSource Bean: There are two attribute defined in this bean.
- ID Is defined for Unique Identifier for this bean as “sessionFactory”
- CLASS is defined for reference type of “org.springframework.orm.hibernate4.LocalSessionFactoryBean”.
- Properties of LocalSessionFactoryBean class:
- packagesToScan: To specify packages to search for autodetection of entity classes in the classpath.
- hibernateProperties : It sets hibernate properties.
- hibernate.dialect: To specify correct database Dialect. Each Database has different SQL code generator.
- hibernate.show_sql: Write all SQL statements to CONSOLE
- hibernate.default_schema: Qualifies unqualified Table names with Given TableName and Schema Name.
- format_sql: Pretty print the SQL in Console and LOG.
- use_sql_comments: Hibernate will generate Comments inside SQL.
- TransactionManager Bean:
- ID Is defined for Unique Identifier for this bean as “transactionManager”
- CLASS is defined for reference type of “org.springframework.orm.hibernate4.HibernateTransactionManager”.
- Properties of HibernateTransactionManager Class:
This transaction manager is appropriate for applications that use a single Hibernate SessionFactory for transactional data access