UserService接口
//UserService接口
public interface UserService {
public void login();
}
beans.properties配置文件:
beans.properties
userService=com.itheima.service.impl.Userserviceimpl
ResourceBundle.getBundle方法调用
package com.itheima.service;
//import com.itheima.service.impl.Userserviceimpl;
import java.util.ResourceBundle;
public class UserService1 {
public static void main(String[] args) throws Exception{
// Userserviceimpl userService=new Userserviceimpl() ;
//读取外部文件配置内容
ResourceBundle rb=ResourceBundle.getBundle("beans");
String userService1path = rb.getString("userService");
UserService userService=(UserService) Class.forName(userService1path).newInstance();
userService.login();
}
}
接口实现类
package com.itheima.service.impl;
import com.itheima.service.UserService;
public class Userserviceimpl implements UserService {
@Override
public void login() {
System.out.println("---");
}
}
如果要开发新的类,只需要更改配置文件和新的实现类即可