Spis treści

Wstęp

System PlusWorkflow posiada kontekst aplikacji, dzięki któremu możemy za pomocą adnotacji sterować procesem tworzenia obiektów, oraz korzystać z dodatkowych funkcjonalności umożliwiających np. zarządzanie transakcjami.

Utworzenie własnej klasy zarządzanej przez kontekst aplikacji

W celu skorzystania z możliwości jakie daje kontekst aplikacji musimy utworzyć klasę, która zostanie załadowana do kontekstu i pobrać w odpowiedni sposób jej instancję.

Sposób 1 - adnotacja @Component

Adnotacja @Component jest najbardziej ogólna powoduje, że nasza klasa zostanie przeskanowana przez system i podczas pobierania instancji zostaną załadowane odpowiednie obiekty. Tą adnotacją są oznaczona jest np. klasa ServiceFactory. Przykład:

@Component
public class CustomComponent
{
    @Autowired
    private SessionFactory sf;
 
	@Autowired
	private UserService us;
	
	private String customField;
 
	public User getUser(){
		return us.getByUserId("jkowalski");
	}
}

Powyższy kod spowoduje, że wywołanie metody getUser nie rzuci wyjątku NullPointerException, lecz zwróci użytkownika. Dzieje się tak z powodu adnotacji @Autowired, która wyszukuje odpowiednią implementację interfejsu i podstawia ją pod obiekt.

Sposób 2 - adnotacja @Service

Adnotacja @Service ma dokładnie takie samo działanie jak @Component jednak zaleca się jej stosowanie w przypadku klas usług(Service) służących do zarządzania obiektami biznesowymi. Tą adnotacją są oznaczone klasy implementujące interfejsy usług np. UserService, PositionService itd.

Opis użycia adnotacji @Service:

Sposób 3 - adnotacja @Repository

@Repository różni się od pozostałych tym, że konwertuje wyjątki rzucone podczas błędu wykonania operacji na bazie danych na standardowy wyjątek z rodziny DataAccessException. Dzięki temu niezależnie od rodzaju bazy danych, sterownika, czy biblioteki jakiej używamy zawsze otrzymamy taki sam wyjątek. Tą adnotacją są oznaczone klasy implementujące interfejsy usług np. UserDao, PositionDao itd.

Opis użycia adnotacji @Repository:

Sposób 4 - adnotacja @Controller

Najbardziej rozbudowany mechanizm pozwalający na definiowanie serwletów w bardzo prosty sposób.

Opis tworzenia serwletu za pomocą adnotacji @Controller:

 

Aby klasa mogła zostać zeskanowana musi znajdować się w drzewie pakietu com.suncode.pwfl


 

 

 

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:

 

For a class to be scanned it must be in the com.suncode.pwfl package tree