Spring IOC AutoWire

自动装配:

    a) byName

b)     byType

c)     如果所有的bean都用同一种,可以使用beans的属性:default-autowire

 

 

 <bean name="userDAO" class="com.dao.impl.UserDAOImpl">
   <property name="daoId" value="1"></property>
  </bean>
 
  <bean name="userDAO2" class="com.dao.impl.UserDAOImpl">
   <property name="daoId" value="2"></property>
  </bean>
 
  <bean id="userService" class="com.service.UserService" scope="prototype" >
  </bean>

 

UserDAOImpl中重写toString():

@Override
 public String toString() {
  return "daoId=" + daoId;
 }

 

测试UserService类自动装配UserDAOImpl

  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  UserService service = (UserService)ctx.getBean("userService");
  System.out.println(service.getUserDAO()); //1

 

注意:byType时,如果出现了两个相同类型时,则报错。原因很简单,Spring不知道要注入到哪个中

byName是根据 bean中的属性名

 

The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory. The autowiring functionality has five modes. Autowiring is specified per bean and can thus be enabled for some beans, while other beans will not be autowired. Using autowiring, it is possible to reduce or eliminate the need to specify properties or constructor arguments, thus saving a significant amount of typing. [2] When using XML-based configuration metadata, the autowire mode for a bean definition is specified by using the autowire attribute of the <bean/> element. The following values are allowed

Autowiring modes

Mode Explanation
no

No autowiring at all. Bean references must be defined via a ref element. This is the default, and changing this is discouraged for larger deployments, since explicitly specifying collaborators gives greater control and clarity. To some extent, it is a form of documentation about the structure of a system.

byName

Autowiring by property name. This option will inspect the container and look for a bean named exactly the same as the property which needs to be autowired. For example, if you have a bean definition which is set to autowire by name, and it contains a master property (that is, it has asetMaster(..) method), Spring will look for a bean definition namedmaster, and use it to set the property.

byType

Allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byTypeautowiring for that bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting thedependency-check="objects" attribute value specifies that an error should be thrown in this case.

constructor

This is analogous to byType, but applies to constructor arguments. If there isn't exactly one bean of the constructor argument type in the container, a fatal error is raised.

autodetect

Chooses constructor or byType through introspection of the bean class. If a default constructor is found, the byType mode will be applied.

 

你可能感兴趣的:(spring ioc)