package com.example.ui; import com.example.factory.BeanFactory; import com.example.service.IAccountService; import com.example.service.impl.AccountServiceImpl; /** * 模拟一个表现层,用于调用业务层 */ public class Client { public static void main(String[] args) { //IAccountService as=new AccountServiceImpl(); IAccountService as= (IAccountService) BeanFactory.getBean("accountService"); as.saveAccount(); } }
package com.example.factory; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * 一个创建Bean对象的工厂 * * Bean:在计算机英语中,有可重用组件的含义。 * JavaBean:用java语言编写的可重用组件。 * * javabean > 实体类 * * 他就是创建我们的service和dao对象的 * * 第一个:需要一个配置文件来配置我们的service和dao * 配置的内容:唯一标识=全限定类名(key=value) * 第二个:通过读取配置文件中的内容,反射创建对象 * * 我的配置文件可以是xml,也可以是properties */ public class BeanFactory { //定义一个properties对象 private static Properties props; //使用静态代码块为Properties对象赋值 static{ //实例化对象 props=new Properties(); //获取properties对象的流对象 InputStream in= BeanFactory.class.getClassLoader().getResourceAsStream("Bean.properties"); try { props.load(in); } catch (IOException e) { throw new ExceptionInInitializerError("初始化properties失败"); } } /** * 根据bean的名称获取bean对象 * @param beanName * @return */ public static Object getBean(String beanName){ Object bean = null; String beanPath = props.getProperty(beanName); System.out.println(beanPath); try { bean=Class.forName(beanPath).newInstance(); } catch (InstantiationException|IllegalAccessException|ClassNotFoundException e) { e.printStackTrace(); } return bean; } }
package com.example.service; /** * 账户业务层接口 */ public interface IAccountService { /** * 模拟保存账户 */ void saveAccount(); }
package com.example.service.impl; import com.example.factory.BeanFactory; import com.example.service.IAccountService; import com.example.dao.IAccountDao; import com.example.dao.impl.AccountDaoImpl; /** * 账户的业务层实现类 */ public class AccountServiceImpl implements IAccountService { //private IAccountDao accountDao=new AccountDaoImpl(); private IAccountDao accountDao= (IAccountDao) BeanFactory.getBean("accountDao"); @Override public void saveAccount() { accountDao.saveAccount(); } }
package com.example.dao; /** * 账户的持久层接口 */ public interface IAccountDao { /** * 模拟保存账户 */ void saveAccount(); }
package com.example.dao.impl; import com.example.dao.IAccountDao; /** * 账户的持久层实现类 */ public class AccountDaoImpl implements IAccountDao { @Override public void saveAccount(){ System.out.println("保存了账户"); } }
#Bean.properties accountService=com.example.service.impl.AccountServiceImpl accountDao=com.example.dao.impl.AccountDaoImpl
<!--pom.xm--> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>learn</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency> </dependencies> <repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>14</java.version> <maven.compiler.source>14</maven.compiler.source> <maven.compiler.target>14</maven.compiler.target> <encoding>UTF-8</encoding> </properties> </project>
原文:https://www.cnblogs.com/abuduri/p/13283127.html