In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how java uses BeanFactory to achieve service and dao layer decoupling, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Decoupling service and dao layer by BeanFactory
In the actual website development process, the service will new a dao to call the method in the dao, as shown in the following code, but once the database needs to be changed, the direction of the dao needs to be changed and the new project needs to be redeployed.
UserDao UserDao=new UserDaoImpl ()
Use BeanFactory in this article to configure the direction of dao in xml. Once the change occurs, there is no need to redeploy, you only need to change the configuration in xml. The code is as follows:
UserDao UserDao= (UserDao) BeanFactory.createObject ("UserDao")
All the implementation codes are as follows. Please focus on the idea of decoupling, that is, the implementation in BeanFactory
The overall realization idea is
UserServiceImp calls the createObject ("UserDao") method in BeanFactory-"parse the xml file in BeanFactory, get the node where id is the same as UserDao in xml, and get the class file on that node -" create an object using the reflection mechanism and return it.
UserServiceImp.java
Package cn.itcast.store.service.serviceImp; import java.sql.SQLException; import cn.itcast.store.dao.UserDao;import cn.itcast.store.dao.daoImp.UserDaoImp;import cn.itcast.store.domain.User;import cn.itcast.store.service.UserService;import cn.itcast.store.utils.BeanFactory; public class UserServiceImp implements UserService {UserDao UserDao= (UserDao) BeanFactory.createObject ("UserDao"); @ Override public void userRegist (User user) throws SQLException {/ / implement the registration function UserDao.userRegist (user) } @ Override public boolean userActive (String code) throws SQLException {/ / implement registration function / send select * from user where code=? to a pair of DB User user=UserDao.userActive (code); if (nullroomroomuser) {/ / you can query the status of a user / / modify the user according to the activation code, clear the activation code user.setState (1); user.setCode (null) / / perform a real update operation to the database update user set state=1, code=null where uid=? / / update user set username=?, password=?, name=?, email=?, telephone =?, birthday =?, sex=?, state=?, code=? Where uid=? UserDao.updateUser (user); return true;} else {/ / cannot query a user according to the activation code return false;}} @ Override public User userLogin (User user) throws SQLException {/ / here: exceptions can be used to pass data between modules / / select * from user where username=? And password=? User uu=UserDao.userLogin (user); if (null==uu) {throw new RuntimeException ("incorrect password!");} else if (uu.getState () = = 0) {throw new RuntimeException ("user not activated!");} else {return uu;}
BeanFactory.java
Package cn.itcast.store.utils; import java.io.InputStream;import java.sql.SQLException;import java.util.List; import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader; import cn.itcast.store.dao.UserDao;import cn.itcast.store.domain.User Public class BeanFactory {/ / parse XML public static Object createObject (String name) {try {/ / get the class value corresponding to name in application.xml through the passed name / / get the Document object SAXReader reader=new SAXReader (); / / if you get the input stream of the application.xml file (application.xml must be under src) InputStream is=BeanFactory.class.getClassLoader (). GetResourceAsStream ("application.xml"); Document doc=reader.read (is) / / get the root node beans Element rootElement = doc.getRootElement () through the Document object; / / get all the child nodes bean under the root node through the root node, and return the collection List list = rootElement.elements () / / iterate through the collection to determine whether the id value on each element is consistent with the current name for (Element ele: list) {/ / ele is equivalent to each bean / / under the beans node to get the id attribute value of the current node / / if it is the same, get the class attribute value String id=ele.attributeValue ("id") on the current element; if (id.equals (name)) {String str=ele.attributeValue ("class") / / create an object by reflection and return Class clazz=Class.forName (str); / / use the class value to create an object to return return clazz.newInstance ();}} catch (Exception e) {e.printStackTrace ();} return null;} public static void main (String [] args) throws SQLException {UserDao ud= (UserDao) BeanFactory.createObject ("UserDao"); User user=new User () User.setUsername ("aaa"); user.setPassword ("aaa"); User uu = ud.userLogin (user); System.out.println (uu);}}
Application.xml
The DAO layer implements the class CreatFactory.javapublic class CreatFactory {/ / singleton mode private static SqlSessionFactory factory=null; public static SqlSessionFactory creatFactory () {if (factory==null) {/ / load the configuration file String resource = "mybatis/config.xml"; / / the configuration file address / / reads the file resource and becomes the character stream Reader reader; try {reader = Resources.getResourceAsReader (resource); / / the character stream is equivalent to Connection factory= new SqlSessionFactoryBuilder () .build (reader) } catch (IOException e) {e.printStackTrace ();}} return factory;}} these are all the contents of the article "how java uses BeanFactory to decouple service and dao layers". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.