public class HelloService {
public void test(){
System.out.println("HelloService.......successful!!!!!");
}
}
1…application.java 接口
public interface ApplicationContext {
public Object getBean(String id);
}
2.接口实现类
public class lcyApplicationContext implements ApplicationContext {
private String xmlPath;
private List<LcyBean> lcyBeans = new ArrayList<>();//存放解析结果
private Map<String,Object> beanMap = new HashMap<>();//存放反射生成的bean
public lcyApplicationContext(String xmlPath){
this.xmlPath=xmlPath;
readXml();
initBean();
}
private void initBean() {
//开始反射生成bean
if(lcyBeans.size()>0){
for(LcyBean lcyBean:lcyBeans){
String id=lcyBean.getId();
String clazz=lcyBean.getClazz();
try {
beanMap.put(id,Class.forName(clazz).newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
private void readXml() {
if(null!=xmlPath&&""!=xmlPath){
//创建解析对象
SAXReader reader = new SAXReader();
try {
URL url = this.getClass().getClassLoader().getResource(xmlPath);
Document document = reader.read(url);
List<Node> list=document.selectNodes("//beans/bean");
for(Iterator<Node> iter =list.iterator();iter.hasNext();)
{
Element e = (Element)iter.next();
System.out.println(e.attributeValue("id") + e.attributeValue("class"));
//存入集合
LcyBean lcyBean= new LcyBean();
lcyBean.setId(e.attributeValue("id"));
lcyBean.setClazz(e.attributeValue("class"));
lcyBeans.add(lcyBean);
}
} catch (DocumentException e) {
e.printStackTrace();
System.err.println("文件找不到");
}
}
else{
System.err.println("文件为空");
}
}
@Override
public Object getBean(String id) {
return beanMap.get(id);
}
}
3.bean 存放xml解析的信息
public class LcyBean {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
private String clazz;
}
public class lcyApplicationContextTest {
@Test
public void test01(){
ApplicationContext context = new lcyApplicationContext("spring.xml");
context.getBean("hellowService");
HelloService helloService =(HelloService) context.getBean("hellowService");
helloService.test();
context.getBean("AccountService");
AccountService accountService = (AccountService) context.getBean("AccountService");
accountService.test();
}
}
当前版本不再支持直接Element然后遍历取值
需要一个迭代器
List<Node> list=document.selectNodes("//beans/bean");
for(Iterator<Node> iter =list.iterator();iter.hasNext();)
{
Element e = (Element)iter.next();
System.out.println(e.attributeValue("id") + e.attributeValue("class"));
}
## 结局:总结+分享
看完美团、字节、腾讯这三家的一二三面试问题,是不是感觉问的特别多,可能咱们真的又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。
开篇有提及我可是足足背下了**Java互联网工程师面试1000题**,多少还是有点用的呢,换汤不换药,不管面试官怎么问你,抓住本质即可!能读到此处的都是真爱,所以福利也为你准备,这份1000题免费送你!**[点击这里免费领取](https://codechina.csdn.net/m0_60958482/java-p7)**
* **Java互联网工程师面试1000题**
![image.png](https://img-blog.csdnimg.cn/img_convert/971a908076810329926683e37761d89d.png)
而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的 《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。
* **程序员代码面试指南--IT名企算法与数据结构题目最优解**
![image.png](https://img-blog.csdnimg.cn/img_convert/33e77993980f0a89f08cd88b23b8510f.png)
* 其余像设计模式,建议可以看看下面这4份PDF(已经整理)
![image.png](https://img-blog.csdnimg.cn/img_convert/f3b1778bb27cb5ea091bebfee9e491f1.png)
* 更多的Java面试学习笔记如下,关于面试这一块,我额外细分出Java基础-中级-高级开发的面试+解析,以及调优笔记等等等。。。
![image.png](https://img-blog.csdnimg.cn/img_convert/698d7b2db1562429f45795d13d4b90e9.png)
码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。
* **程序员代码面试指南--IT名企算法与数据结构题目最优解**
[外链图片转存中...(img-5embx0qh-1629254004752)]
* 其余像设计模式,建议可以看看下面这4份PDF(已经整理)
[外链图片转存中...(img-ODMs2Iyg-1629254004752)]
* 更多的Java面试学习笔记如下,关于面试这一块,我额外细分出Java基础-中级-高级开发的面试+解析,以及调优笔记等等等。。。
[外链图片转存中...(img-y7hRaGEr-1629254004754)]
以上所提及的全部Java面试学习的PDF及笔记,如若皆是你所需要的,那么都可发送给你!