(Turn) Simple understanding of Spring's autowiring (byName; byType)

Spring's automatic assembly (byName;byType) Advantages: greatly reduce Spring configuration
   Disadvantages: dependencies cannot be managed explicitly, and there may be multiple beans that meet the injection rules at the same time. There are no clear dependencies.
          1, byName is automatically assembled according to the property name. This option will check the container and find
                                  a bean that exactly matches the property by name, and autowire it with the property.
            2. byType If there is a bean of the same type as the specified attribute in the container, it will be
                                  automatically assembled with the attribute; if there are multiple beans of this type, an exception will be thrown,
                                  and it will not be possible to use the byType method for automatic assembly; if no
                                  To the matching bean, nothing happens. You can also set it
to see the code:
The implementation class of UserDAO has a property daoId
public class UserDAOImpl implements UserDAO {
private int daoId;
public int getDaoId() {
  return daoId;
}
public void setDaoId(int daoId) {
  this.daoId = daoId;
}
public void save(User user) {
  System.out.println("user saved!");
}
@Override
public String toString() {
  return "daoId=" + daoId;
}
}
UserService 依赖了UserDAO
public class UserService {

private UserDAO userDAO; 
public void add(User user) {
  userDAO.save(user);
}
public UserDAO getUserDAO() {
  return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
}

}
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-2.5.xsd"
          >
  <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
   <property name="daoId" value="1"></property>
  </bean>
 
  <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
   <property name="daoId" value="2"></property>
  </bean>

  <bean id="userService" class="com.bjsxt.service.UserService" autowire="byName"><!-- The byName here is matched according to the attribute name. Here we did not inject UserDAO but your UserService attribute name is UserDAO So it is equivalent to you injecting UserDAO-->
equivalent to this <property name="userDAO" ref="userDAO"/>
  </bean>
 
</beans>

Test:
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml" );
 
 
  UserService service = (UserService)ctx.getBean("userService");
 
  System.out.println(service.getUserDAO());
The print is 1, indicating that UserDAO is injected by default
----------
If it is changed to byType, it will be automatically assembled according to the type. If you put autowire="byType"> according to the above writing method, then an error will be reported because your UserDAO and UserDAO1 are of the same type, both of which are class="com.bjsxt.dao.impl. UserDAOImpl" of

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326906640&siteId=291194637