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?

2
  • create method named myMetier put @Bean method on it. Commented Jul 23, 2015 at 10:41
  • how? PLZ an example i'm beginner in Spring Commented Jul 23, 2015 at 10:42

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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 .. }
In some class which is annotated with @Configuration annotation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.