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入门

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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值