在Spring框架中,主要是基于构造函数、基于setter方法
1.基于构造函数
基于构造函数的依赖注入(DI )是通过调用具有多个参数的构造函数的金额容器来完成的。 每个参数表示依赖关系。 以下是只能使用构造函数注入的依赖注入的类: 对于此类,POJO与特定于容器的接口、基类或注释无关。
publicclasssimpleorderlistener//simpleorderlistener是orderfinderprivateorderfinder; //Spring容器通过生成器指向orderfinderpublicsimpleorderlistener (orderfinderorderfinder ) {super; this.orderFinder=orderFinder; }} 2.基于setter方式的注入
基于setter方法的依赖注入(DI )是通过调用无参数构造函数或无参数的静态工厂方法实例化bean,然后使用bean的setter方法完成的。 以下是仅使用与容器特定接口、基类或注释无关的POJO类的setter依赖于注入的类。
publicclasssimpleorderlistener//simpleorderlistener是orderfinderprivateorderfinder; //Spring容器在setter中包含orderfinderpublicvoidsetorderfinder (order finder ) {this.orderFinder=orderFinder; }