首页 > 代码库 > spring入门篇-学习笔记

spring入门篇-学习笔记

1.spring IOC的作用就是用加载配置文件的方式代替在代码中new 对象的方式来实例化对象。

2.IOC 全称:Inversion  of Control,中文意思:控制反转

3.依赖注入有两种方式:

  1. 设值注入-> 通过添加set方法,并在配置文件的bean中添加property标签(property引用另一个bean)的方式注入
  2.  构造注入-》通过构造方法,并在配置文件的bean中添加constructor-arg标签的方式注入
例子项目结构:



以下是各文件代码:

InjectDao.java  接口

package com.dingli.dao;

public interface InjectDao {
	public void save(String args);
}

InjectDaoImpl.java  实现类
package com.dingli.dao;

public class InjectDaoImpl implements InjectDao{

	@Override
	public void save(String args) {
		System.out.println("保存数据->"+args);
	}

}

InjectService.java 接口
package com.dingli.service;

public interface InjectService {
	
	public void save(String args);
	
}

InjectServiceImpl.java 实现类
package com.dingli.service;

import com.dingli.dao.InjectDao;

public class InjectServiceImpl implements InjectService{

	private InjectDao injectDao;
	
	public InjectServiceImpl(InjectDao injectDao){
		this.injectDao  = injectDao;
	}
	
	@Override
	public void save(String args) {
		System.out.println("service接收到的数据->"+args);
		args +="."+this.hashCode();
		injectDao.save(args);
	}

	public void setInjectDao(InjectDao injectDao) {
		this.injectDao = injectDao;
	}
	
	
}

BaseTest.java Junit测试基类
package test;

import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BaseTest {
	
	private ClassPathXmlApplicationContext context;
	
	private String springXmlpath;
	
	public BaseTest() {}
	
	public BaseTest(String springXmlpath) {
		this.springXmlpath = springXmlpath;
	}
	
	@Before
	public void before() {
		if (StringUtils.isEmpty(springXmlpath)) {
			springXmlpath = "classpath*:spring-*.xml";
		}
		try {
			context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
			context.start();
		} catch (BeansException e) {
			e.printStackTrace();
		}
	}
	
	@After
	public void after() {
		context.destroy();
	}
	
	@SuppressWarnings("unchecked")
	protected <T extends Object> T getBean(String beanId) {
		try {
			return (T)context.getBean(beanId);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	protected <T extends Object> T getBean(Class<T> clazz) {
		try {
			return context.getBean(clazz);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}

}

TestDemo.java Junit测试实例

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.dingli.service.InjectService;


@RunWith(BlockJUnit4ClassRunner.class)
public class TestDemo extends BaseTest{

	public TestDemo() {
		super("classpath*:spring-ioc.xml");
	}
	
//	@Test  //测试设值注入
//	public void testSetter() {
//		InjectService service = super.getBean("injectService");
//		service.save("这是要保存的数据");
//	}
	
	@Test //测试构造注入
	public void testCons() {
		InjectService service = super.getBean("injectService");
		service.save("这是要保存的数据");
	}
}

spring-ioc.xml spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
  <!--  <bean id="injectService" class="com.dingli.service.InjectServiceImpl">
    		<property name="injectDao" ref="injectDao"> </property>
    </bean>    --> 
    
    <bean id="injectService" class="com.dingli.service.InjectServiceImpl">
        	<constructor-arg name="injectDao" ref="injectDao"></constructor-arg>
        </bean>  
    <bean id="injectDao" class="com.dingli.dao.InjectDaoImpl"></bean>
        
</beans>
此次学习内容来源,慕课网spring入门

本文代码可以点击 此处下载 







spring入门篇-学习笔记