Spring: automatically assemble User and springbean according to Bean name
This example describes how to automatically assemble a User object by Bean name!
The autowire attribute of the <bean> element is used to automatically assemble the <bean> tag and define the attributes of the JavaBean. In this way, you can save a lot of tag code for configuring the JavaBean attribute to make the code clean and beautiful;
But it also has a negative impact: After automatic assembly is used, you cannot read the attributes required by the JavaBean from the configuration file.
1. Compile the User object with the following code:
Package com. importnew; public class User {private String name = "test"; private String sex = "male"; private int age = 29; private String tel = "123456789 "; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getTel () {return tel;} public void setTel (String tel) {this. tel = tel ;}@ Override public String toString () {return "User [name =" + name + ", sex =" + sex + ", age = "+ age +", tel = "+ tel +"] ";}}
2. Define Bean (TestUtil) and inject the User object into the TestUtil object. The Code is as follows:
package com.importnew;
public class TestUtil { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
3. Set automatic Bean Assembly in the Spring configuration file applicationContext. xml. Spring will automatically inject the User object to the specified Bean according to the attribute name in the Bean. The Code is as follows;
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="user" class="com.importnew.User" /> <bean autowire="byName" id="testUtil" class="com.importnew.TestUtil" /> </beans>
4. Compile the test class TestSpring with the following code:
package test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.importnew.TestUtil;import com.importnew.User;public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); TestUtil testUtil = (TestUtil) context.getBean("testUtil"); User user = testUtil.getUser(); System.out.println(user.getAge()); System.out.println(user.getName()); }}
---------------------------------------------------
Note:
1. Problems with automatic assembly by Bean name.
Automatic Assembly by Bean name may cause incorrect assembly of JavaBean. If the configuration file defines a JavaBean of the same type as the name of the JavaBean to be automatically assembled, different types of JavaBean are injected incorrectly.
2. If you change autowire = "byName" in the configuration file to "byType", the User can be automatically assembled according to the Bean type. The code in the actual configuration file is as follows:
<Bean autowire = "byType" id = "testUtil" class = "com. importnew. TestUtil"/>
3. Differences between byName and byType:
ByName is automatically assembled by Bean name. If the name of a Bean is the same as that of another Bean, it is automatically assembled.
ByType is automatically assembled by Bean property types. If the attribute type of one Bean is compatible with the attribute type of another Bean, it is automatically assembled.
4. Automatic Assembly may sometimes fail when it is based on the Bean type or Bean name. For example, if you add another implementation object of the User class to the configuration file, the byType automatic assembly type will throw the org. springframework. beans. factory. UnsatisfiedDependencyException exception because it cannot automatically identify and assemble the JavaBean. To solve this problem, you can only use the hybrid use of manual assembly to specify which JavaBean to assemble.
/// End