Spring数据注入(xml)

首先引用所需的jar包:


基本结构:


Person.class:

package com.yc.bean;

public class Person {
    private String name;
    private int age;

    public Person() {
        super();
    }

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}

PersonDao.class:


package com.yc.dao.impl;

public interface PersonDao {
    public void addPerson();
}


PersonDaoImpl.class:


package com.yc.dao.impl;


public class PersonDaoImpl implements PersonDao{

    public void addPerson() {
        System.out.println("add Person name mybatis");
        
    }

}


PersonBiz.class:

package com.yc.Biz;

public interface PersonBiz {
    public void addPerson();
}


PersonBizImpl.class:

package com.yc.Biz.Impl;

import com.yc.Biz.PersonBiz;
import com.yc.dao.impl.PersonDao;

public class PersonBizImpl implements PersonBiz {
    private PersonDao personDao;

    public void addPerson() {
        this.personDao.addPerson();
    }

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

    public PersonBizImpl(PersonDao personDao) {
        super();
        this.personDao = personDao;
    }

    public PersonBizImpl() {
        super();
    }
    

}

Spring.xml:

<?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="person" class="com.yc.spring.Person" scope="prototype">
    <!-- 由spring完成了基本类型参数的依赖注入  通过set方法注入 -->
        <property name="name"  value="王五"></property>
        <property name="age"  value="40"></property>
    </bean>
    
    <bean id="person02" class="com.yc.spring.Person" scope="prototype">
    <!-- 由spring完成了基本类型参数的依赖注入  通过构造方法注入 -->
        <constructor-arg name="name" value="赵六"></constructor-arg>
        <constructor-arg name="age" value="50"></constructor-arg>
    </bean>
    
    
    <!-- 由spring完成了引用类型参数的依赖注入 -->
    <!--先使用ioc的方式创建各种bean  -->
    <bean id="personDao1" class="com.yc.dao.impl.PersonDaoImpl">
    </bean>
    
    <bean id="personBiz" class="com.yc.Biz.Impl.PersonBizImpl">
        <!--再使用di的方式来给personBiz注入依赖(personDao)  -->
        <!-- 注入方式:使用set注入 -->
        <property name="personDao" ref="personDao1" />
    </bean>
    
    <bean id="personBiz02" class="com.yc.Biz.Impl.PersonBizImpl">
        <!--再使用di的方式来给personBiz注入依赖(personDao)  -->
        <!-- 注入方式:构造注入 -->
        <constructor-arg name="personDao" ref="personDao1" />
    </bean>
    
</beans>



测试类:

package spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;






import com.yc.Biz.PersonBiz;
import com.yc.Biz.Impl.PersonBizImpl;
import com.yc.bean.Person;

import junit.framework.TestCase;

public class Test extends TestCase {
        public void testApp(){//传统方式
            Person p=new Person();
            p.setName("张三");
            p.setAge(30);
            
            p=new Person("李四",40);
            System.out.println(p);
        }
        
        public void testApp02(){//普通spring依赖注入(set方法注入)
            ApplicationContext con=new ClassPathXmlApplicationContext("spring.xml");
            Person p=(Person) con.getBean("person");
            System.out.println(p);
        }
        
        public void testApp03(){//普通spring依赖注入(构造方法注入)
            ApplicationContext con=new ClassPathXmlApplicationContext("spring.xml");
            Person p=(Person) con.getBean("person02");
            System.out.println(p);
        }
        
        public void testApp04(){//引用类型数据注入方式   set
            ApplicationContext con=new ClassPathXmlApplicationContext("spring.xml");
            PersonBiz p=(PersonBiz) con.getBean("personBiz");
            p.addPerson();
        }
        
        public void testApp05(){//引用类型数据注入方式   构造方式
            ApplicationContext con=new ClassPathXmlApplicationContext("spring.xml");
            PersonBiz p=(PersonBiz) con.getBean("personBiz02");
            p.addPerson();
        }
        
        
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值