1.setter方法注入。 1)xml配置 <bean id="exampleBean" class="examples.ExampleBean"> <bean id="orderService" class="com.quamnet.testmysql.service.OrderServiceImpl" /> <!--此处将orderService注入到类OrderActiion.java中,注意entityService的取名一定要和OrderAction.java中的set方法一致,不然不能注入--> 2)类 public class ExampleBean { 2.构造器注入 1) <bean id="exampleBean" class="examples.ExampleBean"> <!-- constructor injection using the nested
2) public class ExampleBean { private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public ExampleBean( AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { this.beanOne = anotherBean; this.beanTwo = yetAnotherBean; this.i = i; } } 3.静态工厂注入 1) <bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance"> <constructor-arg ref="anotherExampleBean"/> <constructor-arg ref="yetAnotherBean"/> <constructor-arg value="1"/> </bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/> <bean id="yetAnotherBean" class="examples.YetAnotherBean"/> 2) public class ExampleBean { // a private constructor private ExampleBean(...) { ... } // a static factory method; the arguments to this method can be // considered the dependencies of the bean that is returned, // regardless of how those arguments are actually used. public static ExampleBean createInstance ( AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { ExampleBean eb = new ExampleBean (...); // some other operations ... return eb; } }
3)注释:请注意,传给 而且,重要的是,工厂方法所返回的实例的类型并不一定要与包含
|
Spring 有几种注入方式
最新推荐文章于 2024-07-24 13:08:50 发布