上一篇文章 我用了xml形式 实现自己简单版的spring ioc依赖注入 这节主要利用注解形式实现 和上集内容一样 只是加了读取注解的方法。
DAO接口
public interface PersonDao {
public void add();
}
实现类
package cn.leam.dao.impl;
import cn.leam.dao.PersonDao;
public class PersonDaoBean implements PersonDao {
public void add(){
System.out.println("执行add()方法");
}
}
服务接口
public interface PersonService {
public void save();
}
服务实现类
public class PersonServiceBean implements PersonService {
@LeamResource private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void save(){
personDao.add();
}
}
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
<!--需要加上注解-->
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--需要加上注解标签-->
<context:annotation-config/>
<bean id="personDao" class="cn.leam.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.leam.service.impl.PersonServiceBean">
<!--这里把xml定义的属性去掉 我们使用注解获取
<property name="personDao" ref="personDao"></property> -->
</bean>
</beans>
下面模拟spring对xml配置的类进行实例化
存放属性的对象
public class prosDefinition {
private String name;
private String ref;
public ProsDefinition(String name, String ref) {
this.name = name;
this.ref = ref;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
}
存放bean的 对象
public class Definition {
private String id;
private String className;
private List<ProsDefinition> propertys = new ArrayList<ProsDefinition>();
public Definition(String id, String className) {
this.id = id;
this.className = className;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List<PropertyDefinition> getPropertys() {
return propertys;
}
public void setPropertys(List<PropertyDefinition> propertys) {
this.propertys = propertys;
}
}
加上注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface LeamResource {
public String name() default "";
}
这里是关键点 所有代码都在这里 使用dom4j 解析xml文件中的bean 并获取id和class 再判断元素中是否有引用元素对其一并获取出来存放才Map中 利用java反射一个一个进行实例化
/**
* 学习版容器
*
*/
public class LeamClassPathXMLApplicationContext {
private List<Definition> beanDefines = new ArrayList<Definition>();
private Map<String, Object> sigletons = new HashMap<String, Object>();
public LeamClassPathXMLApplicationContext(String filename){
this.readXML(filename);
this.instanceBeans();
this.annotationInject();
this.injectObject();
}
/**
* 为bean对象的属性注入值
*/
private void injectObject() {
for(Definition beanDefinition : beanDefines){
Object bean = sigletons.get(beanDefinition.getId());
if(bean!=null){
try {
PropertyDescriptor[] ps =
Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(ProsDefinition propertyDefinition : beanDefinition.getPropertys()){
for(PropertyDescriptor properdesc : ps){
if(propertyDefinition.getName().equals(properdesc.getName())){
Method setter = properdesc.getWriteMethod();//获取属性的setter方法
if(setter!=null){
Object value = sigletons.get(propertyDefinition.getRef());
setter.setAccessible(true);
setter.invoke(bean, value);//把引用对象注入到属性
}
break;
}
}
}
} catch (Exception e) {
}
}
}
}
/**
* 完成bean的实例化
*/
private void instanceBeans() {
for(Definition beanDefinition : beanDefines){
try {
if(beanDefinition.getClassName()!=null && !""
.equals(beanDefinition.getClassName().trim()))
sigletons.put(beanDefinition.getId(),
Class.forName(beanDefinition.getClassName()).newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 读取xml配置文件
* @param filename
*/
private void readXML(String filename) {
SAXReader saxReader = new SAXReader();
Document document=null;
try{
URL xmlpath = this.getClass().getClassLoader().getResource(filename);
document = saxReader.read(xmlpath);
Map<String,String> nsMap = new HashMap<String,String>();
nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
xsub.setNamespaceURIs(nsMap);//设置命名空间
List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点
for(Element element: beans){
String id = element.attributeValue("id");//获取id属性值
String clazz = element.attributeValue("class"); //获取class属性值
Definition beanDefine = new Definition(id, clazz);
XPath propertysub = element.createXPath("ns:property");
propertysub.setNamespaceURIs(nsMap);//设置命名空间
List<Element> propertys = propertysub.selectNodes(element);
for(Element property : propertys){
String propertyName = property.attributeValue("name");//元素内部引用的属性也获取
String propertyref = property.attributeValue("ref");
ProsDefinition propertyDefinition = new ProsDefinition(propertyName, propertyref);
beanDefine.getPropertys().add(propertyDefinition);
}
beanDefines.add(beanDefine);
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 获取bean实例
* @param beanName
* @return
*/
public Object getBean(String beanName){
return this.sigletons.get(beanName);
}
}
/**
* 通过注解实现注入依赖对象
*/
private void annotationInject() {
for(String beanName : sigletons.keySet()){
Object bean = sigletons.get(beanName);
if(bean!=null){
try {
PropertyDescriptor[] ps =
Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyDescriptor properdesc : ps){
Method setter = properdesc.getWriteMethod();//获取属性的setter方法
//如果是用了注解 将在set方法注入进去
if(setter!=null && setter.isAnnotationPresent(LeamResource.class)){
LeamResource resource = setter.getAnnotation(LeamResource.class);
Object value = null;
if(resource.name()!=null && !"".equals(resource.name())){
value = sigletons.get(resource.name());
}else{
value = sigletons.get(properdesc.getName());
if(value==null){
for(String key : sigletons.keySet()){
if(properdesc.getPropertyType().
isAssignableFrom(sigletons.get(key).getClass())){
value = sigletons.get(key);
break;
}
}
}
}
setter.setAccessible(true);允许访问私有字段
setter.invoke(bean, value);//把引用对象注入到属性
}
}
Field[] fields = bean.getClass().getDeclaredFields();
for(Field field : fields){
if(field.isAnnotationPresent(LeamResource.class)){
LeamResource resource = field.getAnnotation(LeamResource.class);
Object value = null;
if(resource.name()!=null && !"".equals(resource.name())){
value = sigletons.get(resource.name());
}else{
value = sigletons.get(field.getName());
if(value==null){
for(String key : sigletons.keySet()){
if(field.getType().
isAssignableFrom(sigletons.get(key).getClass())){
value = sigletons.get(key);
break;
}
}
}
}
field.setAccessible(true);//允许访问private字段
field.set(bean, value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
LeamClassPathXMLApplicationContext ctx = new
LeamClassPathXMLApplicationContext("beans.xml");
PersonService personService = (PersonService)ctx.getBean("personService");
personService.save();
}
}
最终输出
"执行add()方法" 说明实例化成功 否则 出现空指针异常
分享到:
相关推荐
4、Spring源码下载 二阶段 1、什么是IOC/DI 2、SpringIOC体系结构 3、源码分析-IOC容器的初始化 4、源码分析-IOC容器的依赖注入 5、源码分析-IOC容器的高级特性 三阶段 Spring AOP的涉及原理及具体实践 SpringJDBC...
Spring Ioc源码分析系列--@Autowired注解的实现原理.doc
Spring IoC 入门实例,包括了多种依赖注入方式,适合Spring初学者学习使用。实例里有详细的注释,一看就懂。
2、 需要注解定义两个注解:@Service、@Resource; 3、 使用反射的手段读取指定目录下的class信息,解析Class信息; 4、 对Class信息的注解做处理; 5、 以键值对的方式创建beanID,bean对象,并且放到容器中; 6、 ...
Spring5-完全注解开发【之】Spring5介绍、实初始化项目、实现IOC【源码】 文章地址:https://blog.csdn.net/m0_37969197/article/details/124331225
资源名称:拓薪教育-Spring内幕深入剖析和实战精讲资源目录:【】01.拓薪教育-spring3.2-序【】02.拓薪教育-Spring3.2-介绍IOC上【】03.拓薪教育-spring3.2-介绍IOC下【】04.拓薪教育-spring3.2-AOP和其他功能介绍...
spring学习资料集合,源码+文档 注释详细 完全超值。 jdk代理,springAOP,IOC,注入全部 分工程详细介绍,这是我个人总结的想了好久决定拿出来跟大家分享,希望更多的人能够掌握它,谢谢
主要详细记载了对Spring注解开发的源码解析,从IOC创建到bean的注册等一系列配置说明
Spring Boot中AOP注解方式源码分析。 Spring Boot 1.x版本和2.x版本AOP默认配置的移动。Spring AOP多种代理机制相关核心类介绍先介绍一些Spring Aop中一些核心类,大致分为三类: advisorCreator ,从spring ioc的...
仿照Spring写的一个Ioc容器,注释很详细!
第2章:通过一个简单的例子展现开发Spring Web应用的整体过程,通过这个实例,读者可以快速跨入Spring Web应用的世界。 第3章:讲解Spring IoC容器的知识,通过具体的实例详细地讲解IoC概念。同时,对Spring框架...
Spring注解驱动开发第41讲——Spring IOC容器创建源码解析(一)之BeanFactory的创建以及预准备工作(合起来整个过程)
看完spring源码后,感触颇深。于是经过一番研究手动实现一个类似Spring的IOC和AOP功能的演示,本demo成功实现了容器的依赖注入和切面的功能,aop使用CGLIB实现。 说明 首先代码的测试运行demo见 应用程序 public ...
包含分析Spring IOC容器,lazy-init,循环依赖处理,AOP实现,事务管理实现源码分析详情注解,可以参考博客对应查看
Spring本身里面包含了两大核心IOC和AOP。IOC负责降低我们代码间的依赖关系,使我们的项目灵活度更高,可复用性更强。AOP是让方法间的各个部分更加独立,达到统一调用执行,使后期维护更加的方便。 SpringMVC本身是...
这是一本专注讲解Spring IOC最核心的知识的电子书,从简单的使用案例到深度解析源码,只讲Spring IOC最核心的知识,让你知其然,更知其所以然。涵盖:AnnotationConfigApplicationContext、组件添加、组件赋值、组件...
spring ioc, spring aop 源码注释,源码解析,有需要学习spring源码的可以看看,里面有详细的方法注释,以及解析
Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好。简单来说就是视情况而定。 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种策略更适合...
NULL 博文链接:https://sunbin.iteye.com/blog/2373014
Spring能有以上这么优秀的离不开Spring核心当中的两大特性,IOC和AOP,Spring能完成这么复杂的生态的构建离不开这两大伟大的特性。 SpringMVC本质上就是由Spring+MVC这两大部分组成,任何一个MVC框架都离不开MVC的...