There are 3 ways to automatically assemble beans in sprring: First, ByName II, Bytype, constructor
First step: Configure our Spring-autowriting.xml, we first use the first method byname
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns= "Http://www.springframework.org/schema/beans"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "
Default-autowire= "ByName" >
<bean id= "Autowritingservice" class= "Com.imooc.autowriting.AutoWritingService" ></bean>
<bean id= "Autowritingdao" class= "Com.imooc.autowriting.AutoWritingDAO" ></bean>
</beans>
Step Two: Create 3 classes, one Test class testautowriting, one is reference class Autowritingservice, one is referenced class Autowritingdao
Test class, needless to say, when you introduce XML, you get an instance of the reference class through Getbean.
Package com.imooc.test.autowriting;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.imooc.autowriting.AutoWritingService;
public class Testautowriting {
public static void Main (string[] args) {
ApplicationContext ctx=new classpathxmlapplicationcontext ("Spring-autowriting.xml");
Autowritingservice Service=ctx.getbean ("Autowritingservice", Autowritingservice.class);
Service.say ("This is a Test");
}
}
Reference Class A set encapsulation is required for an instance of the referenced class when the ByName method is adopted
Package com.imooc.autowriting;
public class Autowritingservice {
Private Autowritingdao Autowritingdao;
public void Setautowritingdao (Autowritingdao Autowritingdao) {
This.autowritingdao = Autowritingdao;
}
public void Say (String word)
{
This.autoWritingDAO.say (word);
}
}
Referenced class
Package com.imooc.autowriting;
public class Autowritingdao {
public void Say (String word) {
System.out.println ("Autowritingdao:" + word);
}
}
Step Three: Test
The principle of the ByName method is worth saying: Setautowritingdao This method is performed because we have default-autowire= "ByName" configured in the XML, and when we enter the referenced class, The Setautowritingdao method is executed by looking for a bean with name Autowritingdao in the XML and not executing if it is not found.
The second method Bytype the same principle as byname, but this time it's looking for conditions that turn into class classes, and it's understandable that when performing Setautowritingdao, comparing class classes and the parameters we're wearing are not a type,
If it is to execute, not to do, it is worth noting: here is not filled in the name does not matter.
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns= "Http://www.springframework.org/schema/beans"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "
Default-autowire= "Bytype" >
<bean id= "Autowritingservice" class= "Com.imooc.autowriting.AutoWritingService" ></bean>
<bean class= "Com.imooc.autowriting.AutoWritingDAO" ></bean>
</beans>
You can test the success by changing the XML
The third method constructor, when using this method, the referenced class should be written to change, no need to write the set method, but to write the constructor of the class
Package com.imooc.autowriting;
public class Autowritingservice {
Private Autowritingdao autoWritingDAO22;
Public Autowritingservice (Autowritingdao Autowritingdao)
{
This.autowritingdao22=autowritingdao;
}
public void Say (String word)
{
This.autoWritingDAO22.say (word);
}
}
You can test success by changing the methods in the XML
We have said so much, so what are we doing above for?
The answer is to make loose coupling by relying on injection.
First, we throw together the concept of coupling. What is coupling.
Without spring,
Package com.imooc.autowriting;
public class Autowritingservice {
Private Autowritingdao autoWritingDAO22;
Public Autowritingservice (Autowritingdao Autowritingdao)
{
This.autowritingdao22=autowritingdao;
}
public void Say (String word)
{
This.autoWritingDAO22.say (word);
}
Let's say Autowritingdao is an interface, so it's definitely not going to work, we have to have its implementation class. Public Autowritingservice (Autowritingdaoiml Autowritingdao)
But one of the fatal drawbacks of this writing is tight coupling.
In the Autowritingservice class we created an instance of Autowritingdao, so that these 2 classes are tightly linked, which is tight coupling. Coupling is certainly necessary, but tight coupling can do a lot of harm. With spring's dependency injection, we can use the implementation class of any one of the Autowritingservice interfaces. In this way we can not understand the source code in the case of secretly replace the implementation of the interface.