(1)创建一个接口
package com.min.dao; public interface UserDao { public void save(String uname, String pwd); }
(2)创建一个实现类将用户信息保存到mysql数据库中
package com.min.dao.impl; import com.min.dao.UserDao; public class UserDaoMysqlImpl implements UserDao { @Override public void save(String uname, String pwd) { System.out.println("----------UserDaoMysqlImpl----------------"); } }
(3)创建一个实现类将用户信息保存到oracle数据库中
package com.min.dao.impl; import com.min.dao.UserDao; public class UserDaoOracleImpl implements UserDao{ @Override public void save(String uname, String pwd) { System.out.println("----------UserDaoOracleImpl----------------"); } }
(4)创建一个管理类,将接口对象作为其属性
package com.min.manager; import com.min.dao.UserDao; public class UserManager { private UserDao userDao; //将接口对象作为属性 public void save (String uname, String pwd) { userDao.save(uname, pwd); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
(5)在Spring配置文件applicationContext.xml将javaBean由Spring管理
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean id="hellword" class="com.min.Hellword"> <property name="message" value="heel"></property> </bean> <bean id="mysqlimpl" class="com.min.dao.impl.UserDaoMysqlImpl"></bean> <bean id="oracleimpl" class="com.min.dao.impl.UserDaoOracleImpl"></bean> <!-- manager的userDao属性依赖注入Spring来注入,可以在程序中无需修改代码,就可以注入不同实例,本例中向mysql保存数据就注入mysqlimpl,如果后来向Oracle中保存数据只需修改注入实例--> <bean id="usermanager" class="com.min.manager.UserManager"> <property name="userDao" ref="mysqlimpl"></property> </bean> </beans>
(6)编写测试类
package com.min.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.min.manager.UserManager; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager manager = (UserManager)context.getBean("usermanager"); manager.save("user", "pwd "); } }
执行结果
四月 21, 2019 10:46:19 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org[email protected]5f4da5c3: startup date ; root of context hierarchy 四月 21, 2019 10:46:19 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] ----------UserDaoMysqlImpl----------------pwd
原文地址:https://www.cnblogs.com/bigsiji/p/10744258.html
时间: 2024-10-30 22:49:49