zoukankan      html  css  js  c++  java
  • spring 06-Spring框架基于Annotation的依赖注入配置

    1、定义一个Dept类

    package cn.liang.vo;
    import java.util.List;
    public class Dept {
    	private Integer deptno ;
    	private String dname ; 
    	private String loc ;
    	public Integer getDeptno() {
    		return deptno;
    	}
    
    	public void setDeptno(Integer deptno) {
    		this.deptno = deptno;
    	}
    
    	public String getDname() {
    		return dname;
    	}
    
    	public void setDname(String dname) {
    		this.dname = dname;
    	}
    
    	public String getLoc() {
    		return loc;
    	}
    
    	public void setLoc(String loc) {
    		this.loc = loc;
    	}
    
    	@Override
    	public String toString() {
    		return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
    	}
    	
    }
    

    2、定义一个IDeptDAO的操作接口

    package cn.liang.dap;
    import cn.liang.vo.Dept;
    public interface IDeptDAO {
        // 这个接口里面只提供有一个增加数据方法
    	public boolean doCreate(Dept vo);
    
    }
    

    3、定义业务层的处理类

    package cn.liang.service;
    import cn.liang.vo.Dept;
    public interface IDeptService {
    	public boolean add(Dept vo);
    }
    

    4、配置applicationContext.xml

    主要核心配置

    xmlns:context="http://www.springframework.org/schema/context"
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    <context:annotation-config/>
    <context:component-scan base-package="cn.liang"/>
    

    Annotation注解

    • 数据层:仓库配置类 @Repository(org.springframework.stereotype.Repository)
    • 业务层:业务配置类 @Service(org.springframework.stereotype.Service)
    • 工具组件:工具类配置 @Component (org.springframework.stereotype.Component)
    • 控制层:控制层配置 @Controller(org.springframework.stereotype.Controller)
    • 以上几个注解除了名字不一样,效果都一样
    • 写上次注解之后就相当于这个bean对象已经在applicationContext.xml文件里面配置上
    • 而它的引用名字就是首字母小写,例如deptDAOImpl

    5、定义DeptDAOImpl类:

    package cn.liang.dao.impl;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Repository;
    import cn.liang.dao.IDeptDAO;
    import cn.liang.vo.Dept;
    
    @Repository
    public class DeptDAOImpl implements IDeptDAO {
    	@Override
    	public boolean doCreate(Dept vo) {
    		LoggerFactory.getLogger(DeptDAOImpl.class).info(vo.toString());
    		return true;
    	}
    
    }
    

    6、定义DeptServiceImpl类:

    package cn.liang.service.impl;
    import javax.annotation.Resource;
    import org.springframework.stereotype.Service;
    import cn.liang.dao.IDeptDAO;
    import cn.liang.service.IDeptService;
    import cn.liang.vo.Dept;
    @Service
    public class DeptServiceImpl implements IDeptService{
    	@Resource
    	private IDeptDAO deptDAO;
    
    	@Override
    	public boolean add(Dept vo) {
    		return this.deptDAO.doCreate(vo);
    	}
    }
    

    7、测试程序:

    package cn.liang.test;
    import org.apache.log4j.Logger;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.liang.service.IDeptService;
    import cn.liang.service.impl.DeptServiceImpl;
    import cn.liang.vo.Dept;
    import junit.framework.TestCase;
    public class TestMessageService2 {
    	private static ApplicationContext ctx = null ;
    	static {
    		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    	}
    
    	@Test
    	public void testDeptConstructor2(){
    		Dept vo = new Dept();
    		vo.setDeptno(10);
    		vo.setDname("Development department");
    		vo.setLoc("Guangzhou");
    		IDeptService deptService = ctx.getBean("deptServiceImpl",IDeptService.class);
    		Logger.getLogger(TestMessageService2.class).info(deptService.add(vo));
    		
    	}
    }
    

    8、输出结果:

    2018-12-03 10:37:54,372 INFO [cn.liang.dao.impl.DeptDAOImpl] - Dept [deptno=10, dname=Development department, loc=Guangzhou]
    2018-12-03 10:37:54,372 INFO [cn.liang.test.TestMessageService2] - true
    
  • 相关阅读:
    FZU Problem 2150 Fire Game
    HTTP协议认识
    GitHub
    设计模式学习笔记(一)——面向对象设计模式与原则
    信息安全系统设计基础第九周学习总结
    信息安全系统设计基础第八周期中总结
    家庭作业二
    信息安全系统设计基础第七周学习总结
    家庭作业
    信息安全系统设计基础第六周学习总结
  • 原文地址:https://www.cnblogs.com/liangjingfu/p/10058723.html
Copyright © 2011-2022 走看看