Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a Spring bean using annotation instead of XML?

I have defined in a xml config file:

<bean id="bootstrap" class="com.package.Bootstrap"></bean>

this works fine.

The bootsrap class :

public class Bootstrap {

   @PostConstruct
   public void onServerStart() {
      System.out.println("PRINTSSSSSSSSSSSSSSSSSSS");
   }
}

The method gets fired.

But how can I get rid of the xml part, and annotate bootstrap to be a bean instead?

I have

<mvc:annotation-driven />
<context:annotation-config /> 

and

<context:component-scan base-package="com.package" />

But I was wondering what the annotation used should be that replaces:

<bean id="bootstrap" class="com.package.Bootstrap"></bean>

I could not find anything about this online and in the spring docs :(

like image 506
mjs Avatar asked Apr 12 '12 15:04

mjs


1 Answers

There's documentation regarding this; you'll want a stereotype annotation like @Component.

Stereotype bean annotations

like image 94
Dave Newton Avatar answered Sep 21 '22 02:09

Dave Newton