码迷,mamicode.com
首页 > 编程语言 > 详细

spring依赖注入

时间:2015-04-13 22:48:09      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

一、属性注入

1,PersonDaoBean.java

package cn.itcast.dao.impl;

import cn.itcast.dao.PersonDao;

public class PersonDaoBean implements PersonDao {
	/* (non-Javadoc)
	 * @see cn.itcast.dao.impl.PersonDao#add()
	 */
	@Override
	public void add(){
		System.out.println("执行PersonDaoBean中的add方法!");
	}

}

PersonDao接口:

package cn.itcast.dao;

public interface PersonDao {

	public abstract void add();

}

  

setter属性注入:

package cn.itcast.service.impl;

import cn.itcast.dao.PersonDao;
import cn.itcast.service.IPersonService;

public class PersonServiceBean implements IPersonService {
	
	private PersonDao personDao;	
	
	
	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	@Override
	public void save(){
		personDao.add();
	}	

}

  文件配置

<?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-2.5.xsd">
    <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
        <property name="personDao" ref="personDao"></property>
    </bean>
    
</beans>

测试:

package org.test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.IPersonService;

public class SpringTest {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
     //此处虽然personDao没有显示的初始化,但是在beans.xml已经实现依赖注入了,所以不会出现空指针的问题。 IPersonService personservice
= (IPersonService) ctx.getBean("personService"); personservice.save(); } }

 二、内部bean注入:

<?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-2.5.xsd">
    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
        <property name="personDao">
            <bean class="cn.itcast.dao.impl.PersonDaoBean"></bean>
        </property>
    </bean>
    
</beans>

第一种方法的好处是可以被多个业务bean使用。

为基本属性注入:

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
        <property name="personDao">
            <bean class="cn.itcast.dao.impl.PersonDaoBean"></bean>
        </property>
        <property name="name" value="liushaocun"></property>
    </bean>

 

spring依赖注入

标签:

原文地址:http://www.cnblogs.com/liusc0424/p/4423361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!