The project structure is as follows:
How to think? Programming towards abstraction?
Let’s take a look at a sequence diagram first
What can be seen from the above? If you are a beginner, it’s weird if you can see what it is, so leave it alone. Let’s take a look at the specific implementation
First, we need to create an entity class: User, which is placed under the model package.
package com.wwj.model; /** * Entity class * @author wwj * Spring */ public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
The following is how to program for abstraction. In short, how to program for interface. Next, define an interface.
package com.wwj.dao; import com.wwj.model.User; /** * Abstraction-oriented programming* @author wwj * Advantages: increased flexibility*/ public interface UserDAO { public void save(User user); }
It can be clearly seen that this interface declares a method, the save method, which has a parameter User object. We can think of it as being used to save the User object to the database.
Leave the specific implementation to the implementation class
package com.wwj.dao.impl; import com.wwj.dao.UserDAO; import com.wwj.model.User; /** *Interface implementation class* @author wwj * */ public class UserDAOImpl implements UserDAO{ @Override public void save(User user) { System.out.println("save user"); } }
The advantage of this is that if you want to change the database environment, you can flexibly define different database codes.
How to call the above method? In order to separate business logic from database operations, we need to define a business logic class.
package com.wwj.service; import com.wwj.dao.UserDAO; import com.wwj.dao.impl.UserDAOImpl; import com.wwj.model.User; /** * Service class, implement business logic* @author wwj * */ public class UserService { private UserDAO userDAO; public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public void add(User user) { this.userDAO.save(user); } }
We can see, there's a thing on it, something? UserDAO, have you seen its effect? It would be strange if you did. A concept of IOC, also called DI, is designed here. The Chinese meaning is called dependency injection, also called inversion of control. This is a very important concept in Spring. Only by understanding it can you have a good understanding of the principles of Spring.
The following truly simulates the implementation of Spring, which is a bit like a factory pattern. Using Spring, we can assemble different objects together for use.
First take a look at the configuration file beans.xml
<beans> <bean id="u" /> <bean id="userService" > <property name="userDAO" bean="u"/> </bean> </beans>
a factory method
package com.wwj.spring; public interface BeanFactory { public Object getBean(String name); }
A class that parses xml files and implements BeanFactory
package com.wwj.spring; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jdom.Document; import org.jdom.Element ; import org.jdom.input.SAXBuilder; public class ClassPathXmlApplicationContext implements BeanFactory{ //Define a container to store objects private Map<String,Object> beans = new HashMap<String, Object>(); public ClassPathXmlApplicationContext() throws Exception{ SAXBuilder sb = new SAXBuilder(); Document doc = sb.build (this.getClass().getClassLoader().getResourceAsStream("beans.xml")); Element root = doc.getRootElement(); //Get the root node List list = root.getChildren("bean"); //All elements named bean for(int i = 0; i < list.size(); i++) { Element element = (Element) list.get(i); String id = element.getAttributeValue("id"); //Get the id value String cla = element.getAttributeValue("class"); //Get the class value Object o = Class.forName(cla).newInstance(); System.out.println(id); System.out.println(cla); beans.put(id,o); for(Element propertyElement : (List<Element> )element.getChildren("property")){ String name = propertyElement.getAttributeValue("name"); //UserDAO String bean = propertyElement.getAttributeValue("bean"); //u Object beanObject = beans.get(bean);//UserDAOImpl instance //Patch together the method name and implement the setUserDAO method String methodName = "set" + name.substring(0, 1) .toUpperCase() + name.substring(1); System.out.println("method name = " + methodName); //Use the reflection mechanism to obtain the method object Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m.invoke(o, beanObject); //Invoke the method} } } @Override public Object getBean(String name) { return beans.get("id"); } }
Let’s have a test class
package com.wwj.service; import org.junit.Test; import com.wwj.model.User; import com.wwj.spring.BeanFactory; import com.wwj.spring.ClassPathXmlApplicationContext; /** * Unit test class* @ author wwj * */ public class UserServiceTest { @Test public void testAdd() throws Exception{ BeanFactory beanFactory = new ClassPathXmlApplicationContext(); UserService service =(UserService)beanFactory.getBean("userService"); User u = new User(); service.add(u); } }
We can see some clues from the test class. First, define a BeanFactory object. Use this object to exchange its getBean method to obtain the business logic class object. Later, you can add the user object to the database by calling the add method of this service class. Go in. Of course, insertion into the database is not implemented here, but testing is simply implemented. In fact, the whole process is very clear. Spring's core configuration file controls the object. When it is to be used, the object is injected into the service class. The service class can use the objects of the DAO layer to perform database-related operations.
The above is the spring configuration content compiled online. I hope it can be helpful to everyone.