Skip to content

Commit

Permalink
增加ioc代码的抽象,为写aop做准备
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui committed Aug 22, 2019
1 parent 0195ca1 commit 8023b05
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* 用于测试的服务器接口
*/
@Controller
public class TestController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.xiaohui.ioc.beans.factory.annotation.Component;

/**
* 用于测试的service服务
*/
@Component(value = "testService")
public class TestService {
public void say(String str) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.xiaohui.ioc.beans.aware;

import context.app.ApplicationContext;

import com.xiaohui.ioc.context.ApplicationContext;

/**
* @author ly
* @create 2019-01-12 16:06
**/
* 实现该接口的类可以直接获取spring配置文件中的bean对象。
*/
public interface ApplicationContextAware {
void setApplicationContext(ApplicationContext applicationContext);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xiaohui.ioc.beans.aware;

/**
* Aware接口 用于注入一些需要用到的对象
* 让bean获取spring容器的服务
*/
public interface Aware {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.xiaohui.ioc.beans.factory.BeanDefinitionRegistry;

public interface BeanFactoryAware extends Aware{
/**
* 让Bean获取配置他们的BeanFactory的引用
*/
public interface BeanFactoryAware extends Aware {

void setBeanFactory(BeanDefinitionRegistry beanDefinitionRegistry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,6 @@
import java.util.Map;

public interface BeanDefinitionRegistry {
/**
* 根据id获取bean
*
* @param id
* @return
*/
Object getBean(String id);

/**
* 根据id获取特定类型的bean,完成强转
*
* @param id
* @param clazz
* @param <T>
* @return
*/
<T> T getBean(String id, Class<T> clazz);

/**
* 获取工厂内的所有bean集合
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.xiaohui.ioc.beans.factory;



/**
* 提供IOC容器最基本的形式,为IOC容器的实现提供了规范
**/
public interface BeanFactory {

Object getBean(String beanName) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.xiaohui.ioc.context;


import com.xiaohui.ioc.beans.factory.BeanFactory;

/**
* 应用上下文
* 1. 以通用的方式加载文件资源的能力。2. 将事件发布到注册监听器的功能。3. 解析消息的能力,支持国际化。4. 从父上下文继承的特性
*/
public interface ApplicationContext extends BeanFactory {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.xiaohui.ioc.support;



import com.xiaohui.ioc.beans.factory.BeanFactory;
import com.xiaohui.ioc.context.ApplicationContext;


public abstract class AbstractApplicationContext implements ApplicationContext {

protected BeanFactory beanFactory;

public AbstractApplicationContext(String location) {
this.beanFactory = new AnnotationBeanFactory(location);
}

@Override
public Object getBean(String beanName) throws Exception {
return beanFactory.getBean(beanName);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xiaohui.ioc.support;

import com.xiaohui.ioc.beans.factory.BeanDefinitionRegistry;
import com.xiaohui.ioc.beans.factory.BeanFactory;
import com.xiaohui.ioc.beans.factory.BeanRegister;
import com.xiaohui.ioc.beans.factory.config.BeanDefinition;
import com.xiaohui.ioc.beans.factory.config.BeanDefinitionParser;
Expand All @@ -13,13 +14,13 @@
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public class AnnotationBeanFactory implements BeanDefinitionRegistry, BeanRegister {
public class AnnotationBeanFactory implements BeanFactory, BeanDefinitionRegistry, BeanRegister {
private Map<String, Object> instanceMapping = new ConcurrentHashMap<>();

// 保存所有bean的信息,主要包含bean的类型 id等信息(初始化时,遍历该list,实例化所有bean)
private List<BeanDefinition> beanDefinitions = new ArrayList<>();
// 配置文件我们使用properties文件,相比使用xml节省了很多解析xml的代码
private Properties config = new Properties();
private Properties config = new Properties();

public AnnotationBeanFactory(String location) {
InputStream is = null;
Expand Down Expand Up @@ -75,6 +76,7 @@ private void register() {
parser.parse(config);
}

@Override
public Object getBean(String id) {
return instanceMapping.get(id);
}
Expand All @@ -83,18 +85,17 @@ public Properties getConfig() {
return this.config;
}

public <T> T getBean(String id, Class<T> clazz) {
return (T) instanceMapping.get(id);
}

@Override
public Map<String, Object> getBeans() {
return instanceMapping;
}

@Override
public void registBeanDefinition(List<BeanDefinition> bds) {
beanDefinitions.addAll(bds);
}

@Override
public void registInstanceMapping(String id, Object instance) {
instanceMapping.put(id, instance);
}
Expand Down

0 comments on commit 8023b05

Please sign in to comment.