Does a class implementing service operations need to be wired up for every action? - topshelf

If a class implements ServiceControl, ServiceSuspend, ServiceShutdown, ServiceCustomCommand, ServicePowerEvent and/or ServiceSessionChange, do those actions still have to be wired up through HostConfigurators WhenXXX methods?

If the service class implements the interfaces, such as ServiceControl, then there is no need to configure the WhenStarted and other methods since Topshelf will use the interface methods.
From the docs: http://topshelf.readthedocs.io/en/latest/configuration/config_api.html#simple-service

Related

Spring proxy default impl

I think I have a good idea of how spring uses proxy concept with #Transactional annotation but I can't find anything about "default" implementation. Basically what I'm looking for is the code which wraps the invocation of original method (method from wrapped object).
There is no such thing as a default implementation because it all depends on the class implementing the method where you add the #Transactional annotation to.
If that class inherits from an interface then JDK Dynamic Proxy will be used.
If not, then an external library called CGLIB will be used to create the proxy.
Dynamic proxy will create a proxy that implements all interfaces your target class also implements while CGLIB will create a proxy that extends your target class.
Also make sure to read this SO question concerning the difference between Dynamic Proxy and CGLIB proxy as it contains valuable information as well.

In spring why the interface is injected?

In spring why the interface is injected rather than the class implementing the interface?
In fact even if you use interface a class is injected. Spring looks for a bean - class which implements the interface.
In some cases like e.g. Spring Data dynamic proxy is created for the interfaces which in fact a class to listen interface's methods calls and do some logic on the calls.
If you set a breakpoint and check what is really injected you will find a class (either your class or some Proxy object)

Why implement interface for creating Apache Felix services?

I noticed multiple ways in which developers create an Apache Felix Service. Each of the attached snippets seem to work. Would need some help to understand, which syntax is best for which scenario
Sample 1: Service created without interface
Declaration of Service
D
#Component
#Service(ServiceViaClass.class)
public class ServiceViaClass{
}
Using service via #Reference annotation
private ServiceViaClass serviceViaClass;
Sample 2:Service implementing interface. No value attribute for #Service annotation
- Declaration of Service
#Component
#Service
public class ServiceViaInterfaceImpl implements ServiceViaInterface{
}
Using service via #Reference annotation
private ServiceViaInterface serviceViaInterface;
Sample 3: Service implementing interface with value attribute for #Service annotation
- Declaration of Service
#Component
#Service(ServiceViaInterface.class)
public class ServiceViaInterfaceImpl implements ServiceViaInterface{
}
Using service via #Reference annotation
private ServiceViaInterface serviceViaInterface;
A component implements an interface and publishes itself as a service under that interface, so that clients can find the component using only the interface.
Sample 1 — publishing a service using the concrete type of the component — is nearly always useless. The service can only be found using the concrete type, and if clients can see the concrete type then why not just instantiate it directly rather than get an instance from the service registry??
Sample 2 — publishing a service by implementing an interface and then just adding the #Service annotation — is what you should usually do. When you use #Service and the component directly implements an interface, the build tooling infers that your component wants to be published as a service under that interface.
Sample 3 has the exact same effect at runtime as sample 2, it's just a bit more explicit in the code. Some people prefer this because it's explicit, others (including me) dislike it because it is redundant.

How to hide spring data repository functions in service class?

I am using spring data JPA repository, my requirement is when i call repository class methods in service class it should show only custom methods like addUser(X,Y) instead of save().
Few things i understand, implementation of spring repository is provided by spring framework at runtime, So we cannot provide out own implementation. (This will overhead).
All methods in JPARepository is public only, so its obivious when we implement this interface all methods will be visible through out.
I am thinking of using DAO and Repository both at same time. DAO will provide custom function signature and repository will implement DAO interface.
Any Hack ?
If you don't want the methods from JpaRepository or CrudRepository, don't extend those but just Repository instead. It is perfectly fine to have a repository interface like
MyVeryLimitedRepository extends Repository<User, Long> {
User findByName(String name);
}
Of course methods like addUser(X,Y) will need a custom implementation.
You can very well use DAO pattern in this case .
By implementing DAO Pattern in Service Class
You create a wrapper between Service and Repository.
You can custom code your DAO layer to only expose custom methods to service layer

Why interface is injected instead of class in Spring MVC

I am reading the Spring Hibernate CRUD tutorial from this url
http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/
Please can anyone tell me why in ContactController.java, ContactService interface is autowired instead of the class ContactServiceImpl.
Similarly in ContactServiceImpl ContactDAO interface is injected. Shouldn't we supposed to inject class instead of an interface?
When your code is dependent on interface and its implementation is injected by Spring, your code becomes decoupled with the implementation. This has an advantage that now you can swap in a different implementation without having to change the code that makes use of interface.
Spring is smart. It will find the implementation of the interface and inject it appropriately (or a proxy thereof.)
You should be programming to interfaces, not implementations.

Resources