Table of contenst
Introduction
PlusWorkflow has an application context, which allows us to use annotations to control the process of creating objects, and to use additional functionality that enables us to manage transactions, for example.
Create a custom class managed by the application context
In order to take advantage of the possibilities provided by the application context, we need to create a class that will be loaded into the context and fetch an instance of it in an appropriate method.
Method 1 - @Component annotation
The @Component annotation is the most general one causes our class to be scanned by the system and the appropriate objects will be loaded during instance retrieval. For example, the ServiceFactory class is marked with this annotation. Example:
@Component public class CustomComponent { @Autowired private SessionFactory sf; @Autowired private UserService us; private String customField; public User getUser(){ return us.getByUserId("jkowalski"); } }
The code above will cause the call to the getUser method not to throw a NullPointerException, but to return the user. This is because of the @Autowired annotation, which searches for the appropriate interface implementation and substitutes it for the object.
Method 2 - @Service annotation
The @Service annotation has exactly the same effect as @Component, however, it is recommended to use it for service(Service) classes used to manage business objects. This annotation is used for classes implementing service interfaces, e.g.UserService, PositionService etc.
Description of the use of the @Service annotation:
Method 3 - @Repository annotation
The @Repository differs from the others in that it converts exceptions thrown during an error in performing a database operation into a standard exception from the DataAccessException family. As a result, regardless of the type of database, driver or library you use, you will always get the same exception. This annotation is used to mark classes implementing service interfaces, e.g. UserDao, PositionDao itd.
Description of the use of the Description of the use of the @Repository annotation:annotation:
Method 4 - @Controller annotation
The most powerful mechanism that allows you to define servlets in a very simple way.
A description of how to create a servlet using the A description of how to create a servlet using the @Controller annotation:annotation:
Note
For a class to be scanned it must be in the com.suncode.pwfl package tree