spring依赖注入小结

spring依赖注入两大方式:
1.基于xml配置文件实现依赖注入(记得提供get/set方法 )
1.1构造方法注入
1.2set方法注入
1.3Field注入
步骤:
1.加入spring所需要的jar包
2.在spring配置文件中进行相关配置
3.通过getBean()获取bean
示例:



  




  
  		
  

  
  	  
  	  
  	  
  
   
 
  
    
    
    
  
  
  
  
  	
  	   
  	   	 [email protected]
  	   	 [email protected]
  	   	 [email protected]
  	   
  	
  	
  		
  			05937732324
  			24324234332
  			32432432432
  		
  	
  	
  		
  			
  			
  				
  				   武当
  				  
  				张三丰				
  			  			
  		
  	
  	
  		
  			张梓兵
  			伍德的
  		
  	
  
  




java代码:
import com.xiaogang.dao.BaseDao;
import com.xiaogang.menu.service.IMenuService;
import com.xiaogang.pojo.Menu;
//业务层
public class MenuService implements IMenuService {


	private BaseDao menuDao;	
	//需要提供set方法
	public void setMenuDao(BaseDao menuDao) {
		this.menuDao = menuDao;
	}


	@Override
	public boolean save(Menu menu) {
		this.menuDao.save("");
		return false;
	}


}


java的测试代码:


import java.util.Set;
import java.util.Map.Entry;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.demo.Demo;
import com.demo.Person;
import com.xiaogang.menu.service.IMenuService;


public class Test01 {


	private static ApplicationContext ctx=null;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		//导入spring配置文件
		ctx = new ClassPathXmlApplicationContext("beans.xml");
	}
	
	@Test
	public void testSaveMenu(){
		//通过bean获取menuService对象
		IMenuService menuService=(IMenuService)ctx.getBean("menuService");
		menuService.save(null);
		
	}
	@Test
	public void testPersonValue(){
		Person person=(Person)ctx.getBean("person");
		System.out.println(person);
	}
	
	@Test
	public void testPersonContructor(){
		Person person2=(Person)ctx.getBean("person2");
		System.out.println(person2);
	}
	
	
	@Test
	public void testDemo(){
		////通过bean获取集合对象
		Demo demo=(Demo)ctx.getBean("demo_list");
		for(String temp:demo.getEmails()){
			System.out.println(temp);
		}
		System.out.println("**************");
		for(String m:demo.getMobiles()){
			System.out.println(m);
		}
		System.out.println("**************");
		for(Entry entry:demo.getContacts().entrySet()){
			System.out.println(entry);
		}
		System.out.println("**************");
		for(Object s:demo.getProps().keySet()){			
				System.out.println(s+"="+demo.getProps().get(s));
		}
	}


}




2.基于注解实现依赖注入,分为手动装配依赖注入(一般常用)和自动装配依赖注入(不需要在代码添加注解,通过配置文件中的autowire="byName",容易出错)。

注解流程解析:

当创建ServiceImpl类对象的时候,发现这个类中有一个属性daoImpl而且有一个resource的注解,故回到配置文件中去找有没有一个id叫“daoImpl”的bean,如果有,就创建它的对象,并赋值给类中的daoImpl.

spring配置文件:


	
	
	
	
	
		
	
		



java代码:
import com.xiaogang.dao.Dao;
import com.xiaogang.service.IService;
/*
 * 注解实现依赖注入(DI)
 * @Resource
 *   按属性名称注入,如按名称找不到,则按类型查找.若明确指定按属性名称查找,则只会按名称查找。
 * @Autowired
 *   只按类型查找
 */
public class ServiceImpl implements IService {
	//@Resource
	//@Resource(name="daoImpl")
	private Dao daoImpl;
	
	//@Resource	
	//@Resource(name="daoImpl")
	public void setDaoImpl(Dao daoImpl) {
		this.daoImpl = daoImpl;
	}
	
	
	@Override
	public void save() {
		daoImpl.save();
	}


}




java测试代码:
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.xiaogang.service.IService;


public class Test01 {


	static ApplicationContext ctx;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ctx = new ClassPathXmlApplicationContext("beans.xml");
	}
	
	@Test
	public void testSave(){
		IService service = (IService)ctx.getBean("serviceImpl");
		service.save();
	}


}


你可能感兴趣的:(ssh框架)