Java SpringBoot framework must be familiar with the use of fast and easy to build their own projects, which is the original intention of the design of the SpringBoot framework. The SpringBoot framework also encapsulates many configuration items based on Spring to achieve “convention over configuration”. However, the over-reliance on the SpringBoot framework may have caused us to forget some of the principles and configuration of the Spring infrastructure, which is also a common question in Java interviews. This blog is about dependency injection of XML configuration beans in Spring.
1. Create a Maven-built Spring project
I used the IDEA development tool, File -> New -> Project to select Maven Project, click Finish, and the Spring Project is ready. As shown below:
2. Create the package and Student entity class
Create a new Student entity class that contains most of the member variables of the type. The set method must be injected as follows:
public class Student {
private String name;
private Integer age;
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge(a) {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString(a) {
return "Student{" +
"name='" + name + '\' ' +
", age=" + age +
'} ';
}
Copy the code
3. Create an XML configuration file
Beans.xml = beans.xml; beans.xml = beans.xml; XML file first put the following XML tag code, you can also go to the Spring Chinese document to copy.
<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 https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
Copy the code
4. Register the Student class in XML
A pair of tags represent a bean of a class. The bean must be configured with the class ID and the class attribute. The ID can be understood as the unique identifier of the class, which is required when retrieved from the Spring IOC container context. The class attribute specifies which class to register. The specific code is as follows:
<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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<! -- Object creation is handled by the Spring container equivalent to the new class name () specifying the ID and class attributes -->
<bean id="student" class="com.hebeu.dao.Student"></bean>
</beans>
Copy the code
Inject values for the attributes of the Student entity class
Define the attribute configuration tag in the tag. If the previous entity class did not have the attribute set method, the attribute cannot be injected here. Attribute configuration tags have name and value attributes. Name is the name of the attribute in the entity class; Value is the value you want to assign. The specific code is as follows:
<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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<! -- Object creation is managed by the Spring container.
<bean id="student" class="com.hebeu.pojo.Student">
<property name="name" value="Zhang"></property>
<property name="age" value="99"></property>
</bean>
</beans>
Copy the code
6. Test whether Student injected successfully
Go to test -> Java -> create a new MyTest test class. Through new ClassPathXmlApplicationContext class loading beans. The XML configuration file for the Spring IOC container context, again through the id attribute of the bean can obtain a Student object, strong need to turn the type. The MyTest class code is as follows:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student", Student.class); System.out.println(student); }}Copy the code
The running results are as follows:
7. To summarize
Now that Spring’s XML configuration beans are done, there are many types of entity class member variables (reference classes; The list; Set, etc.), their configuration methods are not quite the same, you can find their configuration methods in the Spring Chinese documentation, practice or can. Use this blog to document the dependency injection of the Spring XML file configuration bean.