Spring dependency injection IOC (assignment of fields) and Spring testing

meaning:

IOC is a kind of idea, one of its focuses is to dynamically provide other objects it needs to an object during the operation of the system. This is achieved through DI (Dependency Injection, Dependency Injection); the assignment of attribute fields of objects in Spring is called Dependency Injection (Dependency Injection).

Classification of dependency injection:

XML injection : There must be a setter method, so this injection method is also called attribute injection or setter method injection;
1. The type of the field and the type of the object being injected
must be the same 2. The XML method must have the setter method (the bottom layer is Copied by reflection and calling setter method)

<bean id="otherBean" class="cn.itshow.bean.OtherBean"></bean>
<bean id="myBean" class="cn.itshow.bean.MyBean">
	<!-- 属性:bean属性,如果该属性是字符串或基本类型的值,直接写value -->
	<property name="name" value="易小川"></property>		<!-- 实际开发中:这种情况非常少 -->
	<!-- ref:因为注入的是一个对象 -->
	<property name="time" ref="date"></property>		<!-- 实际开发中:这种情况非常少 -->
	<!-- ref:从容器中去找一个类型匹配【当前属性的类型与被注入对象的类型(可以是其子类)】的对象赋值给当前字段 -->
	<property name="bean" ref="otherBean"></property>	<!-- 实际开发中:这种情况非常多 -->
</bean>

Annotation injection
Annotation is written on the setter method or on the field. If it is written on the field, the setter method is not required;
@Autowired: annotation provided by Spring
@Resource: provided by J2EE, the package javax.annotation needs to be imported. Resource, spring supports this annotation

	@Autowired
	private MyBean bean;
	@Resource
	private Tomato t;
	@Resource
	private IUserDao dao;
	@Resource 
	private IUserService service;

Spring test

  • On top of Junit, Spring did a further encapsulation. This integrated test module is called Spring-test, and the bottom layer is still Junit4 (so the junit4 package is still required)

  • Spring testing loads configuration files and instantiated container objects in an annotated manner, which simplifies code and improves test efficiency.
    Implement spring test:

  • 导包:spring-aop-4.1.2.RELEASE.jar,spring-test-4.1.2.RELEASE.jar

  • Write test method:

  • Note that using Spring testing requires loading the spring configuration file and starting Springtest

  • Load the context configuration-the configuration is
    @ContextConfiguration ("classpath: applicationContext.xml") written in the configuration file to
    start Springtest
    @RunWith (SpringJUnit4ClassRunner.class) // Start Springtest For junit4 package, you must import junit4 jar

//加载上下文配置   -- 配置是写在配置文件中的
@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)//启动Springtest  对junit4封装,必须导入junit4的jar包
public class SpringTest {
	// 去容器中查询MyBean类型的对象赋值给当前字段,如果没有找到报错
	@Autowired
	private MyBean mybean;
	@Resource
	private Tomato t;
	@Test
	public void testBean(){
		System.out.println(mybean);
		System.out.println(t);
	}
}
Published 23 original articles · Like1 · Visits 164

Guess you like

Origin blog.csdn.net/weixin_45528650/article/details/105559346