首先引用所需的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();
}
}