Code Monkey home page Code Monkey logo

springiocmock's Introduction

SpringIoC管理bean的原理:

1、通过 Resource 对象加载配置文件

2、解析配置文件,得到指定名称的 bean

3、解析 bean 元素,id 作为 bean 的名字,class 用于反射得到 bean 的实例:

注意:此时,bean 类必须存在一个无参数构造器(和访问权限无关); ​

4、调用 getBean 方法的时候,从容器中返回对象实例;

结论:就是把代码从JAVA文件中转移到了XML中。

//模拟Spring IoC容器操作
	@Test
	void testIoCMock() throws Exception {

		String className = "cn.wolfcode.hello.HelloWorld";

		HelloWorld world = null;

		//------------------------------------------------------

		//使用反射创建对象

		Class clzz  = Class.forName(className);

		Constructor con = clzz.getConstructor();

		con.setAccessible(true);//设置构造器的可访问性

		Object obj =con.newInstance();

		//使用内省机制设置属性值

		BeanInfo beanInfo = Introspector.getBeanInfo(clzz,Object.class);

		PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

		for (PropertyDescriptor pd : pds) {

			//HelloWorld类中所有的属性名称

			String  propertyName = pd.getName();

			if("username".equals(propertyName)) {

				//调用username的setter方法

				pd.getWriteMethod().invoke(obj, "Lucy");

			}else if("age".equals(propertyName)) {

				//调用age的setter方法

				pd.getWriteMethod().invoke(obj, 18);

			}

		}

		world = (HelloWorld)obj;

		//------------------------------------------------------

		world.sayHello();

	}

springiocmock's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.