IOC container
IOC containers, sometimes called dependency injection (DI). Through the dependencies defined by the object itself, these dependencies are injected into the bean when it is created. This process is contrary to our normal design. We usually have a new object in the object, which is our program control management object. In spring, we create objects through containers, which are controlled by containers. In spring-based applications, IOC containers instantiate, configure, assemble objects based on configuration metadata, and manage the entire life cycle of these objects.
Configuration metadata can be read through XML files, annotations and other ways. ClassPath Xml Application Context, Annotation Config Application Context and so on can be used in the context. BeanFactory and ApplicationContext are commonly used classes. Normally, we inherit BeanFactory with ApplicationContext, which contains all the functions of BeanFactory.
In spring-based applications, IOC container-managed objects are called beans. Each application consists of multiple beans, and the dependencies between beans are managed by containers.
Instance and Use of Containers
Metadata configuration, which tells Spring containers how to instantiate, configure, and assemble objects in applications.
When configuring an XML configuration file, the bean tag is used to configure the metadata. The annotations use @bean, @Configuration and so on to configure metadata.
XML configuration
Configuration file daos.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--id yes bean The unique identifier, class yes bean Full class name--> <bean id="orderDao" class="com.learn.ch1.OrderDao"/> </beans>
Configuration file services.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="orderService" class="com.learn.ch1.OrderService"/> </beans>
Test code
@Test public void test() { //Multiple XML configuration files can be directly introduced here, or one can be introduced, and then other configuration files can be introduced in this XML with the < import resource= "XXX.xml"/> tag. //Application Context for XML is ClassPath Xml Application Context ApplicationContext app = new ClassPathXmlApplicationContext("services.xml","daos.xml"); String[] names = app.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } }
The other two java classes are not written, and the results are as follows:
annotation
MyConfig
@Configuration public class MyConfig { @Bean public OrderDao orderDao() { return new OrderDao(); } @Bean public OrderService orderService() { return new OrderService(); } }
Test code
@Test public void test() { //The Application Context used under the annotation is Annotation Config Application Context ApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class); String[] names = app.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } }
The results are as follows:
XML and annotations
As can be seen from the above two examples, the configuration under annotations is shorter and more concise, but it is not better to say that annotations are better.
XML also has advantages:
- XML can directly access connection components without compiling code.
- Configuration centralized, good control.