Bean automatic assembly by:
- byName: The name of the automatic assembly, inspect the container and the attribute name lookup entirely consistent with the bean, and fitted with automatic properties
- byType: according to the type of automatic assembly, a container inspection same attribute type of the bean, it will automatically fitted with the attribute, if there are a plurality Spring Bean which the most suitable property can not be determined, an exception is thrown if not found, nothing had happened.
- constructor: The automatic assembly and configured byType is similar, if not found in the container is consistent with the parameter type constructor bean, the exception is thrown. Not recommended
A, byName automatic assembly
bean.xml configuration autowire = "byName" to
<?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-3.0.xsd"> <bean id="textId" class="Automatically.ByName.Text" autowire="byName"></bean> <!-- Note injected by name, id name to be the same as the name of the attribute name --> <bean id="speak" class="Automatically.ByName.Speak"></bean> </beans>
TestDao: automatic assembly, simply writing method set
public class TextDao { private TestService speak; public void say(){ System.out.println("Text Start"); speak.say(); } // automatic assembly, write only method for configuring a set autowire = "byName" to public void setSpeak (the TestService Speak) { the this .speak = Speak; } }
Test Service:
public class TestService { public void say(){ System.out.println ( "Speak ---- ----- say method is performed" ); } }
TestMain: Test class
public class TestMain { @Test public void Test(){ ApplicationContext context=new ClassPathXmlApplicationContext("Bean.xml"); TextDao textDao = (TextDao) context.getBean ( "textid" ); textDao.say(); } }
operation result:
Two, byType autowiring
and similar bean.xml byName simply modify the autowire = "byName" as the autowire = "the byType"
TestDao with the same byName
TestService with the same byName
TestMain with the same byName
operation result:
Three, constructor automatic assembly
and similar bean.xml byName simply modify the autowire = "byName" as the autowire = " constructor "
<?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-3.0.xsd"> <!-- ByType This mode is very similar, but it applies to constructor arguments. Spring container seen as beans, autowire property of the beans in the XML configuration file for the constructor. then, It attempts to a parameter configuration file in its constructor beans names match and wires. If a match is found, it will inject these bean, otherwise it will throw an exception --> <bean id="textId" class="Automatically.AutoConstructor.TestDao" autowire="constructor"> </bean> <bean id="testService" class="Automatically.AutoConstructor.TestService"></bean> </beans>
TestDao: and the two do not like is no set method only constructor
public class TestDao { private TestService speak; public TestDao(TestService speak){ this.speak = speak; } public void say(){ System.out.println("TestDao Start:"); speak.say(); } }
TestService with the same byName
TestMain with the same byName
operation result: