1. Spring IoC container
1.1 Spring BeanFactory container
package com.tutorialspoint; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class MainApp { public static void main(String[] args) { XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("Beans.xml")); HelloWorld obj = (HelloWorld) factory.getBean("helloWorld"); obj.getMessage(); } }
1.2 Spring Application Context container
The most commonly used application context interface implementation:
-
FileSystem Xml Application Context: This container loads defined bean s from an XML file. Here, you need to provide the complete path to the constructor's XML file
-
ClassPathXml Application Context: This container loads defined beans from an XML file. Here, you don't need to provide the full path of the XML file, just configure the CLASSPATH environment variables correctly, because the container will search the bean configuration file from CLASSPATH.
- WebXml Application Context: This container loads bean s defined in an XML file within the scope of a web application.
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext ("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); } }
2 Spring Bean
2.1 definition
attribute | describe |
---|---|
class | This property is mandatory and specifies the bean class used to create beans. |
name | This property specifies a unique bean identifier. In XML-based configuration metadata, you can use ID and/or name attributes to specify bean identifiers. |
scope | This property specifies the scope of the object created by a specific bean definition, which will be discussed in the section on bean scope. |
constructor-arg | It is used to inject dependencies and will be discussed in the next chapter. |
properties | It is used to inject dependencies and will be discussed in the next chapter. |
autowiring mode | It is used to inject dependencies and will be discussed in the next chapter. |
lazy-initialization mode | The delayed initialization bean tells the IoC container to create a bean instance when it is first requested, rather than at startup. |
initialization method | After all the required properties of the bean are set by the container, the callback method is invoked. It will be discussed in the section on the life cycle of beans. |
destruction method | When the container containing the bean is destroyed, the callback method is used. It will be discussed in the section on the life cycle of beans. |
2.2 Scope
Scope of action | describe |
---|---|
singleton | This scope limits the definition of bean s to a single instance (default) in each Spring IoC container. |
prototype | This scope limits the definition of a single bean to any number of object instances. |
request | This scope limits the definition of bean s to HTTP requests. Only works in the context of web-aware Spring Application Context. |
session | This scope limits the definition of bean s to HTTP sessions. Only works in the context of web-aware Spring Application Context. |
global-session | This scope limits the definition of bean s to global HTTP sessions. Only works in the context of web-aware Spring Application Context. |
2.3 Life Cycle
It's easy to understand the life cycle of Spring bean s. When a bean is instantiated, it may need to perform some initialization to make it available. Similarly, when beans are no longer needed and removed from containers, some cleanup may be required.
Although there are some activities that occur between bean instantiation and destruction, this chapter will only discuss two important life cycle callback methods that are necessary for bean initialization and destruction.
To define the installation and disassembly of a bean, we simply declare that it has init-method and/or destroy-method parameters. The init-method attribute specifies a method that is called immediately when the bean is instantiated. Similarly, destroy-method specifies a method that can be invoked only after the bean is removed from the container.
3 Spring Annotation Assembly
3.1 Spring@Required annotation
@ Required annotation applies to the setter method of bean attributes, which indicates that the affected bean attributes must be placed in the XML configuration file when they are configured, or the container will throw a BeanInitializationException exception. The following is an example of using the @Required annotation.
3.2 Spring@Autowire annotation
Autowired in Setter Method
Property @Autowired
Autowired in the constructor
3.3 Spring@Qualifier annotation
There may be situations where when you create multiple beans of the same type and want to assemble only one of them with an attribute, in which case you can use @Qualifier and @Autowired annotations to eliminate confusion by specifying which real beans will be assembled. An example of using the @Qualifier annotation is shown below.
package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class Profile { @Autowired @Qualifier("student1") private Student student; public Profile(){ System.out.println("Inside Profile constructor." ); } public void printAge() { System.out.println("Age : " + student.getAge() ); } public void printName() { System.out.println("Name : " + student.getName() ); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for profile bean --> <bean id="profile" class="com.tutorialspoint.Profile"> </bean> <!-- Definition for student1 bean --> <bean id="student1" class="com.tutorialspoint.Student"> <property name="name" value="Zara" /> <property name="age" value="11"/> </bean> <!-- Definition for student2 bean --> <bean id="student2" class="com.tutorialspoint.Student"> <property name="name" value="Nuha" /> <property name="age" value="2"/> </bean> </beans>
3.4 Annotations to Spring JSR-250
Spring also uses JSR-250-based annotations, which include @PostConstruct, @PreDestroy and @Resource annotations. Because you have other choices, and although these notes are not really needed, let me give you a brief introduction about them.
@ PostConstruct and @PreDestroy annotations:
To define the installation and uninstallation of a bean, we use init-method and/or destroy-method parameters to make a simple statement. The init-method attribute specifies a method that is called immediately during the instantiation phase of the bean. Similarly, destroy-method specifies a method that is called only before a bean is deleted from the container.
You can use the @PostConstruct annotation as an alternative to the initialization callback function and the @PreDestroy annotation as an alternative to the destroy callback function, which is explained in the following example.
@ Resource annotation:
You can use the @Resource annotation in fields or setter methods, which works the same as in Java EE 5. @ The Resource annotation uses a'name'attribute, which is injected as a bean name. You can say that it follows by-name automatic join semantics.
3.5 Spring Java-based configuration
@ Configuration and @Bean annotations
Annotation classes with @Configuration indicate that this class can use the Spring IoC container as the source of bean definitions. @ Bean annotations tell Spring that an annotation method with @Bean will return an object that should be registered as a bean in the Spring application context. The simplest possible @Configuration class is as follows:
package com.tutorialspoint; import org.springframework.context.annotation.*; @Configuration public class HelloWorldConfig { @Bean(initMethod = "init", destroyMethod = "cleanup" ) @Scope("prototype") public HelloWorld helloWorld(){ return new HelloWorld(); } }
The above code will be equivalent to the following XML configuration:
<beans> <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" /> </beans>
@ Import Note:
@ The import annotation allows the @Bean definition to be loaded from another configuration class. Consider the ConfigA class, as follows:
@Configuration public class ConfigA { @Bean public A a() { return new A(); } }
You can import the above Bean declaration in another Bean declaration, as follows:
@Configuration @Import(ConfigA.class) public class ConfigB { @Bean public B a() { return new A(); } }