Configure a project based on Spring annotations

Source: Internet
Author: User

This document describes how to configure annotations in a project to reduce additional project configurations. Take a system as an example to describe how to configure and use annotations (including transaction annotations, Dao layer annotations, Service layer annotations, Action layer annotations), annotations are provided by Spring. 1. The following describes how to configure the annotation: first, introduce the jar package. The following three jars are directly related to the annotation configuration. For other associated packages, introduce them by yourself: Step 2, configure the following in Spring's applicationContext-xx.xml: [html] <! -- Configure the annotation scan directory --> <context: component-scan base-package = "com. bodatech"> </context: component-scan> <! -- Introduce annotation --> <context: annotation-config/> after the preceding configuration, the @ Autowired, @ Service, @ Repository, @ Compant, and other labels provided by Spring can be used in the project, please Google the specific meanings of the above labels. I will not go into details here. Step 3: configure the transaction annotation: [html] <! -- Transaction Manager --> <bean id = "transactionManager" class = "org. springframework. jdbc. datasource. dataSourceTransactionManager "> <property name =" dataSource "ref =" dataSource "/> </bean> <! -- Configure transaction control --> <tx: annotation-driven transaction-manager = "transactionManager"/> Note: Because jdbcTemplate is used in the project, DataSourceTransactionManager is used in the transaction management class. If ibatis and hibernate are used, use other transaction management classes. Step 4: Configure jdbcTemplate to inject Bean: [html] <! -- Configure jdbctemplate to inject Bean --> <bean id = "jdbcTemplate" class = "org. springframework. jdbc. core. jdbcTemplate "> <property name =" dataSource "ref =" dataSource "/> </bean> 2. Annotations can be used in the project through the preceding configuration. The following describes how to use Annotations: first, because the project has a base class Dao, other user-defined classes all inherit from this class, so you only need to inject jdbcTemplate in BaseDao. As follows: [java] @ Autowired private JdbcTemplate jdbcTemplate; Note: using the annotation form, you do not need to configure additional setter and getter methods, the same below. Step 2: configure the Annotation on the Dao implementation class as follows: [java] @ Repository public class NewsPublishedDaoImpl extends BaseDao implements INewsPublishedDao {} Note: Because NewsPublishedDaoImpl inherits BaseDao, therefore, jdbcTemplate can be used directly in this class without any further injection. If no BaseDao exists in the project, remember to configure jdbcTemplate in each user-defined Dao. Step 3: Configure annotations on the Service implementation class as follows: [java] @ Service public class NewsPublishedServiceImpl implements INewsPublishedService {@ Autowired private INewsPublishedDao newsPublishedDao;} Step 4, configure the transaction annotation in the Service implementation class as follows: [java] @ Transactional public boolean insertNewsPublished () {} Note: add this annotation to the method that requires transaction control, you do not need to add a transaction method. The preceding transaction annotation configuration is equivalent to [java] @ Transactional (propagation = Propagation. REQUIRED, isolation = Isolation. DEFAULT, readOnly = false) and the DEFAULT rollback principle of the preceding configuration is to catch the RuntimeException exception. To catch other exceptions, configure rollbackFor = YourException. class. Step 5: Introduce the annotation in the Action, as follows: [java] public class NewsPublishedAction {@ Autowired private INewsPublishedService newsPublishedService;} In summary, the annotation configuration is complete. For the Action/Controller layer, annotations can be used to implement Zero Configuration. This content is not in the scope of this explanation. please Google it on your own. Yanbi. @ Will _ awokE.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.