sping learning

Spring is in 2003 a rise of lightweight Java development framework is an open source inversion of control (Inversion of Control, IoC ) and aspect-oriented (AOP) of the container frame. Its main purpose was to simplify enterprise open source link to download: test the source code .

  1. To learn sping framework of knowledge, first create a maven project, intended for test purposes (Note: webapp project; the default directory; create a directory structure for src / main / java java code as stored, and wherein the output directory and resource consistent; import sping beans and two core packages will be automatically downloaded core package after introducing the two log dependencies com.springsource.org.apache.commons.logging-1.1).
  2. The second step test first to add the following two labels in the application, which will be reported when doing parsing the following error. (Unable to locate the xml namespace, so it is necessary to introduce context class)

 

package come.beichende.test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import come.beichende.pojo.Student;

public class TestSping {
	
	@Test
	public void testSping(){//beanfactory 默认使用的是直接延迟加载,调用的时候才会去加载
		//创建sping容器
		Resource iResource = new ClassPathResource("applicationContext.xml");
		BeanFactory iBeanFactory = new XmlBeanFactory(iResource);
		//取出对象
		Student stu = (Student) iBeanFactory.getBean("studentPojo");
		stu.stuFun();
	}
	//模拟spring创建对象  反射
	@Test
	public void moNiSping() throws Exception{
		Class<?> cl = Class.forName("applicationContext.xml");
		Object obj = cl.newInstance();
		Field[] fidlds =  cl.getFields();//取得反射对象的所有属性
		for (Field field : fidlds) {
			//如果该参数有如下两个参数,则给对应的参数调用set方法,实现注入
			if( field.getName().equals("name")){
				Method setName = cl.getDeclaredMethod("setName", null);
				setName.invoke(obj, null);
			}
			if(field.equals("age")){
				Method setName = cl.getDeclaredMethod("setAge", null);
				setName.invoke(obj, null);
			}
		}
		Student stu = (Student)obj;
		stu.stuFun();
	}
	@Test
	public void testFactory(){//beanfactory 默认使用的是直接延迟加载,调用的时候才会去加载  桌面应用建议
		//创建sping容器
		Resource iResource = new ClassPathResource("applicationContext.xml");
		BeanFactory iBeanFactory = new XmlBeanFactory(iResource);
	}
	@Test
	public void testApplication(){//applicationContext接口 默认使用的是直接加载,所以在加载的时候会加载全部的bean  web应用建议
		//创建sping容器
		ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
}

	
	
	

Inversion of Control Ioc: Student is created and maintained within the application, so-called inversion of control is dependent on the application itself is not responsible for creation and maintenance of objects, dependent on creating and maintaining object is responsible for the exterior of the container, so that control goes by the application transferred to a transfer outside of the container, the so-called inversion of control, not the technology but an idea. (Will create objects of all-trans to spring container)

Dependency injection Di: at runtime, the outer container dynamically dependent objects is injected into the assembly (during object creation, the configuration-dependent properties of the object by injection).

 

Guess you like

Origin blog.csdn.net/qq_40826106/article/details/87358508