一、通过get/set方法依赖注入
1、dao层
点击查看接口代码
public interface IDAO {
public int save();
public int remove();
public int modify();
public List findAll();
}
点击查看实现类代码
public class DeptDAO implements IDAO{
@Override
public int save() {
System.out.println("保存了一条 dept 的数据");
return 0;
}
@Override
public int remove() {
return 0;
}
@Override
public int modify() {
return 0;
}
@Override
public List findAll() {
List resultList = new ArrayList<>();
resultList.add("dept01");
resultList.add("dept02");
resultList.add("dept03");
return resultList;
}
}
点击查看实现类代码
public class EmpDAO implements IDAO{
@Override
public int save() {
System.out.println("保存了一条 emp 的数据");
return 0;
}
@Override
public int remove() {
return 0;
}
@Override
public int modify() {
return 0;
}
@Override
public List findAll() {
return null;
}
}
点击查看代码
public class DeptService {
private DeptDAO deptDAO;
private EmpDAO empDAO;
public DeptDAO getDeptDAO() {
return deptDAO;
}
public void setDeptDAO(DeptDAO deptDAO) {
this.deptDAO = deptDAO;
}
public EmpDAO getEmpDAO() {
return empDAO;
}
public void setEmpDAO(EmpDAO empDAO) {
this.empDAO = empDAO;
}
public List findAll(){
List all = deptDAO.findAll();
return all;
}
public int save(){
deptDAO.save();
empDAO.save();
return 1;
}
}
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!--通过get/set依赖注入-->
<bean id="deptdao" class="com.bh.dao.DeptDAO"></bean>
<bean id="empdao" class="com.bh.dao.EmpDAO"></bean>
<bean id="deptservice" class="com.bh.service.DeptService">
<property name="deptDAO" ref="deptdao"/><!--传值方式:应用传递用ref,值传递用value-->
<property name="empDAO" ref="empdao"/>
</bean>
<bean class="com.bh.service.DeptService2"></bean>
<!--通过构造器方式依赖注入-->
<bean id="print" class="com.bh.service.PrintService">
<constructor-arg index="0" value="5"></constructor-arg>
<constructor-arg index="1" value="class08"></constructor-arg>
</bean>
<!--通过注解依赖注入-->
<context:annotation-config></context:annotation-config>
<!--使用spring管理.properties配置文件-->
<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<bean id="user" class="com.bh.po.User">
<property name="userName" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!--p属性-->
<bean id="dept" class="com.bh.po.Dept"
p:deptno="100"
p:dname="class009"
p:loc="1406">
<!-- <property name="dname" value="class009" />-->
<!-- <property name="loc" value="1406" />-->
<!-- <property name="deptno" value="100" />-->
</bean>
</beans>
点击查看代码
public class Test {
public static void main(String[] args) {
System.out.println("start==========");
//读取配置文件,并且根据配置文件初始化spring容器
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据配置文件的id创建实例对象
IDAO deptdao = (IDAO) ac.getBean("deptdao");
//deptdao.save();
DeptService deptService = (DeptService) ac.getBean("deptservice");
// DeptService deptService = ac.getBean(DeptService.class);//如果配置文件没有id就通过类对象创建实例对象
List all = deptService.findAll();
System.out.println(all);
System.out.println("end=============");
}
}
1、创建一个类
点击查看代码
package com.bh.service;
public class PrintService {
private String name;
private int count;
//创建构造器
public PrintService(int count,String name){
this.count = count;
this.name = name;
}
public void print(){
for (int i = 0; i < count; i++) {
System.out.println(name + "-" + i);
}
}
}
点击查看代码
<!--通过构造器方式依赖注入-->
<bean id="print" class="com.bh.service.PrintService">
<constructor-arg index="0" value="5"></constructor-arg><!--index为第几个参数,value为值传递-->
<constructor-arg index="1" value="class08"></constructor-arg>
</bean>
点击查看代码
package com.bh.test;
import com.bh.po.User;
import com.bh.service.DeptService2;
import com.bh.service.PrintService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
public static void main(String[] args) {
System.out.println("start============");
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过构造器依赖注入
PrintService print = (PrintService) ac.getBean("print");
print.print();
System.out.println("end ===============");
}
}
点击查看代码
class08-0
class08-1
class08-2
class08-3
class08-4