In the last control reversal we saw that relying on a bean file to achieve the control of the code is very convenient, no longer have to instantiate the object, 2333~~~
1. Manual assembly
1 class= "Com.eco.daoimp.Usertodo1" ></bean>2 3 <!-- Defines a reference to the internal interface of the UserService class (Userdao) to a specific implementation class object (USERTODO1)--4class= " Com.eco.service.Userservice ">5 <property name="userdao"ref=" Todo "></ Property>
6 </bean>
Here we are manually assembling the bean, specifying the member variable under the UserService class Userdao is an instantiated object of Usertodo1.
2. Automatic assembly
class= "Com.eco.daoimp.Usertodo2"/>
class= "Com.eco.daoimp.Usertodo1"/>
3 class auto-wire= "ByName" />
Look at this automatic assembly type:
ByName: For member variables under the UserService class Userdao automatically assemble the bean with the Id/name attribute Userdao , which is the second bean, if the name is not found
For Userdao beans, will be an error!!!
Bytype: For member variables under the UserService class Userdao automatically assemble beans of the same type (Userdao), because Usertodo1 and Usertodo2
are Userdao implementation of the class, so will meet the requirements, then the program is tangled: exactly whether the assembly ID is Userdao bean or assembly ID is
Userdao2 Bean, slow down the decision, had to error!!! So use caution ~ ~
In addition there is a default-auto-wire= "byname" defined in the beans tag, which is equivalent to a global declaration that tells all the bean tags to be used I say
To assemble in a declarative manner.
3. Annotation assembly
annotation annotations, annotations are declared with an @ Plus field, like our common @test, @Override, etc.
Before you assemble with annotations, the Bean.xml file has a change from the previous one:
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"4 xmlns:context= "Http://www.springframework.org/schema/context"5Xsi:schemalocation= "Http://www.springframework.org/schema/beans6http//www.springframework.org/schema/beans/spring-beans-3.1.xsd7 Http://www.springframework.org/schema/context8 / http www.springframework.org/schema/context/spring-context-3.1.xsd">9 Ten<context:component-scan base-package= "Com.eco"></context:component-scan>
</beans>
The Scarlet Letter is a new declaration field that is more manually assembled/automatically assembled than before, and then there is only one label inside, which tells the container which package to parse the bean under.
1 @Service2 Public classUserService {3 //to define a reference to an interface4 PrivateUserdao Userdao;5 6 //defines the setter method, sets the interface's reference to which implementation class's Object7 @Autowired8 Public voidSetuserdao (Userdao Userdao) {9 This. Userdao =Userdao;Ten } One A Public voidUseradd (User newuser) { - //at this point the Userdao has been implemented to point to a specific interface implementation class object after spring dependency injection - //then the method that invokes the interface is actually called the method of the particular implementation class . the Userdao.adduser (newuser); - } -}
1 @Repository2 Public classUsertodo1ImplementsUserdao {3 //an interface implementation class adds a method body to a method4 Public voidadduser (user user) {5 //use Hibernate's factory class to get session objects and transaction objects transaction6Session session =hibernatesessionfactory.getsession ();7Transaction Transaction =session.begintransaction ();8 //Database Add user Action9 session.save (user);Ten //Commit a transaction One transaction.commit (); A //Close Session Object - session.close (); -System.out.println ("Todo1 Create the user"); the } -}
The three annotations of the scarlet Letter mean that for the UserService class of the @Service declaration, the Userdao variable of the internal @Autowired declaration is injected into the
The Usertodo1 implementation class for the @Repository declaration.
The dependency injection has ① interface injection ②setter Injection ③ construction method injection, it is obvious that the above example is setter injection,
Interface injection writes annotations on defined member variables;
The setter injection will write the annotations on the setter method;
Construction method Injection writes annotations on the construction method (obviously there is no construction method defined above).
The method of invocation is still the same as before:
1 Public classTest1 {2 @Test3 Public voidAdd () {4 //Spring reads beans. XML file5ApplicationContext CTX =NewClasspathxmlapplicationcontext ("Beans.xml");6 //Parse bean tag with ID UserService, internally implemented Userdao Userdao = new Usertodo1 ()7UserService service = (userservice) Ctx.getbean ("UserService");8User NewUser =NewUser ("Orange Mulberry", 31);9 //The Useradd () method called at this point is the Useradd () method of the interface implementation class USERTODO1Ten Service.useradd (newuser); One } A}
Just the name of the bean, by default, the name of the class that is declared by @service is lowercase, and the rest remains unchanged as the Bean's id/name;
We can also customize the name of the Bean, @Service ("Eco"), so you can customize the name in parentheses like this.
Java Spring Assembly Bean (manual assembly, automatic assembly, annotation assembly)