The Spring container can automatically assemble dependencies between beans, which has the following advantages:
- Autoassembly can greatly reduce the configuration of specified property or constructor parameters
- Autowiring can update the configuration as the object evolves. For example, if you need to add a dependency to a class, the dependency can be satisfied automatically without changing the configuration
Limitations and disadvantages of autowiring:
- Explicit dependency properties and constructor-arg Settings always override autoassembly. You cannot automatically concatenate simple properties, such as primitive Strings, and Classes (and arrays of such simple properties). This limitation is by design.
- Automatic assembly is less accurate than explicit wiring. As mentioned earlier, though, Spring is careful to avoid guesswork that can produce unexpected results. Relationships between spring-managed objects are no longer explicitly documented.
- Wiring information may not be appropriate for tools that might generate documentation from the Spring container.
- Multiple bean definitions within the container may match the type specified by setter method or constructor parameters to be autowled. This is not necessarily a problem for arrays, collections, or Map instances. However, for dependencies that require a single value, this ambiguity is not resolved arbitrarily. If no unique bean definition is available, an exception is thrown
Automatic assembly mode, refer to documentation
model | instructions |
---|---|
no | By default, no automatic assembly is required |
byName | Automatically attribute by attribute name. Spring finds the bean with the same name as the property and injects it through the set method |
byType | Automatically assemble properties by type. Spring finds the bean of that property type and injects it through the set method |
constructor | Similar to byType, but only for constructor arguments. An exception is thrown if there is no bean in the container that makes function arguments |
Enable autowire=”byType” on beans and specify global default autowire byType on beans. Beans are assembled in a higher way than beans
<? The 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" default-autowire="byType" > <bean class="com.ofwiki.AutowireApplication.UserService" primary="true" autowire="byName" /> <bean id="userService1" class="com.ofwiki.AutowireApplication.UserService" /> <bean id="dbDao" class="com.ofwiki.AutowireApplication.UserDao" /> </beans>Copy the code
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.Assert; public class AutowireApplication { public static void main(String[] args) { new ClassPathXmlApplicationContext("spring-default-autowire.xml") .getBean(UserService.class).save(); } public static class UserService { private UserDao dbDao; public void setDbDao(UserDao dbDao) { this.dbDao = dbDao; } public void save() { Assert.notNull(dbDao, "dbDao field is null"); System.out.println("UserService:save user info"); dbDao.save(); } } public static class UserDao { public void save() { System.out.println("UserDao:save user info"); }}}Copy the code