Reputation: 49
Does @Injectable
mean
"allow injection of whatever into the class which the decorator is above"
Or
Does it mean "allow me to inject this class (which the decorator is above) into 'wherever' in the application"?
Upvotes: 2
Views: 1936
Reputation: 5758
@Injectable
is just a marker that tells to angular engine that the class is available to be created by Injectors. At runtime angular tells to Injectors to read all the @Injectable
classes and instantiate them and make them available to be injected to the classes that reference them.
For example lets suppose that there is a service in angular called UserService
and you need to use that service in a component called RegistrationComponent
.
@Injectable()
export class UserService {
saverUser(User user)
.....
}
Then in the RegistrationComponent
constructor declare an input parameter that reference to UserService
, it tells to angular that UserService should be injected to RegistrationComponent
, of course previously the @Injectable
marker should be declared in UserService
RegistrationComponent.ts
export class RegistrationComponent
constructor(private userService: UserService) { }
In the Spring context the @Component
plays a similar job than @Injectable
of course there are many difference between them in the implementation, but both of them play a similar role. @Component
is an annotation that tells to Spring that some specific class has to be considered a candidate for auto-detection and that class can lives in the Spring Container. The components (beans) living in the Spring Container can be injected to other classes.
@Autowired
is not the same as @Component
. @Autowired
means that an specific class member should be provided or injected by Spring DI container.
For more information please see the following links:
@Autowired Spring Documentation
@Component Spring Documentation
Upvotes: 3