http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html
The ApplicationContext
is the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies. Using the method T getBean(Stringname, Class<T> requiredType)
you can retrieve instances of your beans.
The ApplicationContext
enables you to read bean definitions and access them as follows:
1 // create and configure beans 2 ApplicationContext context = 3 new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"}); 4 5 // retrieve configured instance 6 PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class); 7 8 // use configured instance 9 List userList service.getUsernameList();
接口ApplicationContext中还有其它的方法来取回beans实例,但是理想的情况是你的应用应该不使用它们。从而使应用不dependency on Spring APIs。For example, Spring's integration with web frameworks provides for dependency injection for various web framework classes such as controllers and JSF-managed beans.
1 <bean id="clientService" 2 class="examples.ClientService" 3 factory-method="createInstance"/> 4 5 public class ClientService { 6 private static ClientService clientService = new ClientService(); 7 private ClientService() {} 8 9 public static ClientService createInstance() { 10 return clientService; 11 } 12 }
1 <!-- the factory bean, which contains a method called createInstance() --> 2 <bean id="serviceLocator" class="examples.DefaultServiceLocator"> 3 <!-- inject any dependencies required by this locator bean --> 4 </bean> 5 6 <!-- the bean to be created via the factory bean --> 7 <bean id="clientService" 8 factory-bean="serviceLocator" 9 factory-method="createClientServiceInstance"/> 10 11 public class DefaultServiceLocator { 12 private static ClientService clientService = new ClientServiceImpl(); 13 private DefaultServiceLocator() {} 14 15 public ClientService createClientServiceInstance() { 16 return clientService; 17 } 18 }
Dependency injection (DI)的两种方法:Constructor-based dependency injection and Setter-based dependency injection.
1 package x.y; 2 3 public class Foo { 4 5 public Foo(Bar bar, Baz baz) { 6 // ... 7 } 8 } 9 10 <beans> 11 <bean id="foo" class="x.y.Foo"> 12 <constructor-arg ref="bar"/> 13 <constructor-arg ref="baz"/> 14 </bean> 15 16 <bean id="bar" class="x.y.Bar"/> 17 <bean id="baz" class="x.y.Baz"/> 18 19 </beans> 20 21 ////////////////////////////////////////////////////////////////// 22 package examples; 23 24 public class ExampleBean { 25 26 // No. of years to the calculate the Ultimate Answer 27 private int years; 28 29 // The Answer to Life, the Universe, and Everything 30 private String ultimateAnswer; 31 32 public ExampleBean(int years, String ultimateAnswer) { 33 this.years = years; 34 this.ultimateAnswer = ultimateAnswer; 35 } 36 } 37 38 <bean id="exampleBean" class="examples.ExampleBean"> 39 <constructor-arg type="int" value="7500000"/> 40 <constructor-arg type="java.lang.String" value="42"/> 41 </bean> 42 43 <bean id="exampleBean" class="examples.ExampleBean"> 44 <constructor-arg index="0" value="7500000"/> 45 <constructor-arg index="1" value="42"/> 46 </bean> 47 48 <bean id="exampleBean" class="examples.ExampleBean"> 49 <constructor-arg name="years" value="7500000"/> 50 <constructor-arg name="ultimateanswer" value="42"/> 51 </bean> 52 53 package examples; 54 55 public class ExampleBean { 56 57 // Fields omitted 58 59 @ConstructorProperties({"years", "ultimateAnswer"}) 60 public ExampleBean(int years, String ultimateAnswer) { 61 this.years = years; 62 this.ultimateAnswer = ultimateAnswer; 63 } 64 }
Spring容器验证每个bean的配置当容器被创建的时候。然而bean的属性只有bean创建时才被设置。
只有被设置为singleton-scoped并且pre-instantiated (默认)的bean才在容器被创建时被创建。否则bean直到被请求时才被创建。
Creation of a bean potentially causes a graph of beans to be created, as the bean's dependencies and its dependencies' dependencies (and so on) are created and assigned.
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
1 <beans default-lazy-init="true"> 2 <!-- no beans will be pre-instantiated... --> 3 </beans>