15

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 :(

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
mjs
  • 21,431
  • 31
  • 118
  • 200

2 Answers2

18

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

Stereotype bean annotations

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 3
    `@Component` ties you to Spring. Consider using JSR-330 annotations (`@Named`, `@Inject`)instead: http://www.mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example/ – JJ Zabkar Jun 04 '13 at 16:04
  • @JJZabkar So does ``, they're already tied to Spring. – Dave Newton Jun 04 '13 at 16:54
  • Yes, but in separate files though. Suppose you annotate your beans with @Component/@Service/etc. That would mean all your classes have `import` (compile-time) dependencies on the Spring Framework. You'll still have your XML configuration. (Continued) – JJ Zabkar Jun 05 '13 at 14:51
  • 1
    Now consider a scenario where you need to re-factor--say you need to get rid of Spring (i.e., replace it with GWT or some other DI provider). If you had used `@Inject`/`@Named`--Java standard annotations--you could have been more loosely coupled. It's akin to using JPA annotations instead of Hibernate annotations--don't tie yourself to an implementation (Read: Spring Framework) where you don't have to. – JJ Zabkar Jun 05 '13 at 14:53
  • 4
    @JJZabkar From a purely technical standpoint, this is obvious--I don't think explaining platform migration is necessary. From a realistic standpoint, meh. – Dave Newton Jun 05 '13 at 15:25
  • @JJZabkar - I'm with you on this one. Whenever you can write code that doesn't tie you into a particular framework - do it! – Volksman Apr 18 '16 at 06:31
4

this is a simple example that I have just made:

Main.java

package the.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

public class Main {

public static void main(String[] args) {

    AbstractApplicationContext aac = new AnnotationConfigApplicationContext(Person.class, Phones.class);
    Person person = aac.getBean(Person.class);
    System.out.println(person.getPhones().getPhoneOne());
    System.out.println(person.getPhones().getPhoneTwo());
    System.out.println(person.getSurname());
    System.out.println(person.getName());
    System.out.println(person.getAge());
    aac.close();

    }
}


Person.java

package the.test;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration

//you may use @ComponentScan("the.test") here and omit declaring 
//"Phone.class" in the main method 

public class Person {
private int age;
private String name;
private String surname;

private Phones phones;

public int getAge() {
    return age;
}
@Value("33")
public void setAge(int age) {
    this.age = age;
}

public String getName() {
    return name;
}

@Value("John")
public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

@Value("Due")
public void setSurname(String surname) {
    this.surname = surname;
}

public Phones getPhones() {
    return phones;
}
@Resource
public void setPhones(Phones phones) {
    this.phones = phones;
}
}


Phones.java

package the.test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Phones {
private String PhoneOne;
private String PhoneTwo;

public String getPhoneOne() {
    return PhoneOne;
}

@Value("987654321")
public void setPhoneOne(String phoneOne) {
    PhoneOne = phoneOne;
}

public String getPhoneTwo() {
    return PhoneTwo;
}

@Value("123456")
public void setPhoneTwo(String phoneTwo) {
    PhoneTwo = phoneTwo;
}

}

this is completely based on Spring Annotation and is made on spring framework 4.2.5

hope it helps.

Farzan.s
  • 945
  • 8
  • 9