1.建三个类,在一个类中引用其他两个类
import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; public class BookService { @Resource(name="bookDao") private BookDao bookDao; @Autowired private OrdersDao ordersDao; public void add(){ System.out.println("BookService-------"); bookDao.add1(); ordersDao.add2(); } }
public class BookDao { public void add1(){ System.out.println("BookDao--------"); } }
public class OrdersDao { public void add2(){ System.out.println("OrdersDao-------"); } }
2.xml文件配置
<context:component-scan base-package="com.example"></context:component-scan> <bean id="bookDao" class="com.example.spring.BookDao"></bean> <bean id="ordersDao" class="com.example.spring.OrdersDao"></bean> <bean id="bookService" class="com.example.spring.BookService"></bean>