When SSH is developed, the project is generally divided into 3 layers: The Web layer, the service layer, and the DAO layer. The basic process of development is to define the DAO interface, then implement the interface, define the same type of service interface, and then implement the Service interface (using DAO interface injection), then call the service layer from the Web layer.
The DAO layer completes the underlying database operation, and the service layer accomplishes pure business logic, and the data manipulation part of the layer is implemented by injecting the DAO layer. Service layer is the function of the data obtained from the DAO layer closer to the implementation of the business, DAO layer only to achieve the data increase, delete, change, check operation. Using this layering method is more conducive to project expansion and maintenance.
First, set value injection
SetPoint injection refers to an IOC container that uses setter methods to inject a dependent instance. The setter-based di can be implemented by invoking the Bean's setter method after invoking the parameterless constructor or the non-parametric static factory method to instantiate the bean.
- Create a new project, configure the runtime environment (add spring and other dependent jar packages in Build path)
- The user class, as a saved model object
PackageCom.deciphering.model; Public classUser {PrivateString username; PrivateString password; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; }}User.java
- The User class interface Userdao (the benefit of programming for interfaces is that Userdao can have several different implementation classes, respectively, to operate on different databases)
Package Com.deciphering.dao; Import Com.deciphering.model.User; Public Interface Userdao { publicvoid Save (user user);}Userdao
- Userdao Implementation class Userdaoimpl (simplified processing)
PackageCom.deciphering.dao.impl;ImportCom.deciphering.dao.UserDAO;ImportCom.deciphering.model.User; Public classUserdaoimplImplementsUserdao { Public voidSave (User user) {System.out.println (User.getusername ()+ "saved in oracle!"); //System.out.println (User.getusername () + "saved in db2!"); //System.out.println (User.getusername () + "saved in mysql!"); }}Userdaoimpl
- Business Logic Interface UserService
PackageCom.deciphering.service;ImportCom.deciphering.dao.UserDAO;ImportCom.deciphering.model.User; Public classUserserviceimpl {PrivateUserdao Userdao; Public voidAdd (user user) {userdao.save (user); } PublicUserdao Getuserdao () {returnUserdao; } Public voidSetuserdao (Userdao Userdao) { This. Userdao =Userdao; }/*Public Userserviceimpl (Userdao Userdao) {super (); This.userdao = Userdao; } */}UserService
- Implementation class Userserviceimpl for the business logic interface
PackageCom.deciphering.service;ImportCom.deciphering.dao.UserDAO;ImportCom.deciphering.model.User; Public classUserserviceimpl implements userservice{PrivateUserdao Userdao; Public voidAdd (user user) {userdao.save (user); } PublicUserdao Getuserdao () {returnUserdao; } Public voidSetuserdao (Userdao Userdao) { This. Userdao =Userdao; } }
- Spring configuration file Beans.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 "> class=" Com.deciphering.dao.impl.UserDAOImpl "> </bean> Class= "Com.deciphering.service.UserServiceImpl" > <property name= "Userdao" > <ref bean= "U"/> </property> </bean></beans>
- Testing class Test
PackageCom.deciphering.service;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.deciphering.model.User;ImportCom.deciphering.service.UserServiceImpl; Public classuserservicetest {@Test Public voidTestadd ()throwsException {applicationcontext ctx=NewClasspathxmlapplicationcontext ("Beans.xml"); Userserviceimpl Service= (Userserviceimpl) ctx.getbean ("UserService"); User u=NewUser (); U.setusername ("Test User"); U.setpassword ("123456"); Service.add (U); } }
Ii. Construction Method Injection
Construct method injection means that the IOC container uses a constructor method to inject a dependent instance. The constructor-based di is implemented by invoking a constructor with parameters, each of which represents a dependency.
In the project file above, make a small number of changes, you can become the construction method injection.
- Modify the Userserviceimpl class, you must explicitly give the constructor to the parameter when using the constructor method injection.
PackageCom.deciphering.service;ImportCom.deciphering.dao.UserDAO;ImportCom.deciphering.model.User; Public classUserserviceimpl {PrivateUserdao Userdao; Public voidAdd (user user) {userdao.save (user); } PublicUserdao Getuserdao () {returnUserdao; } Public voidSetuserdao (Userdao Userdao) { This. Userdao =Userdao; }//Defining construction Methods PublicUserserviceimpl (Userdao Userdao) {Super(); This. Userdao =Userdao; } }
- Modify the Beans.xml file, add bold code to modify part
<?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 id= "U"class= "Com.deciphering.dao.impl.UserDAOImpl" > </bean> <bean id= "UserService"class= "Com.deciphering.service.UserServiceImpl" > <!--<property name= "Userdao" > <ref bean = "U"/> </property>//Using construction method injection to inject U instance for userservice instance<constructor-arg> <ref bean= "u"/> </constructor-arg></bean> </beans>
File structure:
Implementation of Dependency Injection: Setting value injection and constructing method injection