0

I'm working on a project in Spring using SpringMVC, i'm using the xml element <bean/> and i want to convert my code to @Bean

spring-bean.xml

<bean id="myDao" class="com.my.dao.EmployeImplDB" init-method="init"></bean>

<bean class="com.my.service.EmployeImplMetier" id="myMetier">
    <property name="dao" ref="myDao"></property>
</bean>

how to convert xml to annotation @Bean?

yb3prod
  • 564
  • 2
  • 10
  • 22

2 Answers2

1

You can write this way

@Repository
class EmployeImplDB{}


@Service   
EmployeImplMetier{

@Autowired
EmployeImplDB myDao;

}

@Repository signifies that your bean is a DAO class

@Autowired injects dao class EmployeImplDB in the Service class

yb3prod
  • 564
  • 2
  • 10
  • 22
underdog
  • 4,447
  • 9
  • 44
  • 89
1

Like this:

@Bean(name = "myDao", initMethod = "init")
public EmployeDao myDao() {
    EmployeDao eidb = new EmployeImplDB();
    return eidb;
}

@Bean(name = "myMetier")
public Metier employeImplDB(EmployeDao myDao) {
    Metier metier= new EmployeImplMetier(myDao);
    return metier;
}

Note: Presuming that name of EmployeImplDB superclass (interface) is EmployeeDB.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • is it like this? **com/my/Dao/EmployeImplDB.java** `@Bean(name = "myDao", initMethod = "init") public class EmployeImplDB implements EmployeDao { //my code ... } ` **com/my/service/EmployeImplMetier.java** `@Bean(name = "myMetier") public class EmployeImplMetier extends Metier{ //my code .. } ` – yb3prod Jul 23 '15 at 11:11
  • In some class which is annotated with `@Configuration` annotation. – Branislav Lazic Jul 24 '15 at 08:17