Question: I have a question about declarative OSGi Services. I have the following interface : 

```
  public interface PrintService {
    public void print();
  }

```

and its implementation: 

```
    public class PrintServiceImpl implements PrintService {

      @Override
      public void print() {
        System.out.println("Hello from PrintServiceImpl!");
      }
    }

```

OSGI-INF/component.xml :

```
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="service">
   <implementation class="service.PrintServiceImpl"/>
   <service>
      <provide interface="print.PrintService"/>
   </service>
</scr:component>

```

MANIFEST.MF :

```
Service-Component: OSGI-INF/component.xml

```

After I install the service and start it nothing happend. How can I activate it and print "*Hello from PrintServiceImpl!*" to console.

Answer: 
You should make explicit that you want you service to start when the bundle is started. Otherwise, it will be instantiated lazily, and give that no other service requires the PrintService, it will not be instantiated at all.

You can control your service lifecycle (with regard to the bundle lifecycle) using the attributes `enabled` and `immediate`. `enabled` is trivial: indicates whether your service is enabled or not when the bundle is started. `immediate` indicates whether this service is lazy ot not. `immediate="true"` will start your service *immediatly*, `immediate="false"` will apply lazy instantiation (i.e. wait until some other service requests this service as a dependency)

e.g.

```
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="PrintService"
activate="activate" 
deactivate="deactivate"
modified="modified"
enabled="true" 
immediate="true">

```

As Chris mentioned, if you want your service to do something when it's activated, you should add some code to the `activate` lifecycle method (note that the name of the method can be specified in the component descriptor like it was done explicitly above)

```
protected final void activate() {
    print();
}

```
When you are use annotations, XML file will be generated automatically under OSGI-INF/... folder. 

```
  public interface PrintService {
    public void print();
  }

  implementation with annotations: 

  @Component(service = PrintService.class)
  public class PrintServiceImpl implements PrintService {

      @Activate
      public void activate() {
         print();
      }

      @Override
      public void print() {
        System.out.println("Hello from PrintServiceImpl!");
      }
    }

```

As i said before, XML file created automatically. With @Component(service = PrintService.class) annotations, we register PrintService to OSGi framework(like registerService method in ServiceTracker). When service is registered, Activate method will be called. Method name can be anything. Just add @Activate annotations to say "when service is registered, call the method which is under @Activate annotations" to OSGi Framework.