sprig-ioc之注解注入

  1. spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 注解注入 -->
	<!-- 开启组件扫描  component 组件 scan 扫描-->
	<context:component-scan base-package="com.aaa.ioc"></context:component-scan>
	<!-- spring支持的注解 
		@Controller 扫描web层(表现层)
		@Service 扫描服务层(业务层)
		@Repository 扫描持久层(数据访问层)
		@Component 如果分层不明确
		
		@Autowired 注解依赖注入 按类型注入 spring框架提供
		@Resource 注解依赖注入 按名称注入 jvm提供(java扩展包)
	-->
	
</beans>
  1. Dao接口
package com.aaa.ioc.dao;

import com.aaa.ioc.entity.Dept;

public interface DeptDao {

	Dept getById(int DeptId);
}
  1. Dao实现类
package com.aaa.ioc.dao;

import org.springframework.stereotype.Repository;

import com.aaa.ioc.entity.Dept;

@Repository
public class DeptDaoImpl implements DeptDao {

	@Override
	public Dept getById(int DeptId) {
		
		return new Dept(1,"人事部","东");
	}
}
  1. Service接口
package com.aaa.ioc.service;

import com.aaa.ioc.entity.Dept;

public interface DeptService {

	Dept getById(int DeptId);
}
  1. Service实现类
package com.aaa.ioc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aaa.ioc.dao.DeptDao;
import com.aaa.ioc.entity.Dept;

@Service("deptS")
public class DeptServiceImpl implements DeptService {

	@Autowired
	private DeptDao deptDao;
	public Dept getById(int DeptId) {
		
		return deptDao.getById(DeptId);
	}
	
}
  1. Controller层
package com.aaa.ioc.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.aaa.ioc.entity.Dept;
import com.aaa.ioc.service.DeptService;

/**
 *压制警告
 */
@SuppressWarnings("all")

@Controller("deptC")
public class DeptController {

	//依赖注入
	@Autowired
	//如果一个接口有多个实现类,不知道依赖注入哪个时,使用该注解指定名称默认情况下实现类名称的首字母小写/如果有别名用别名
	//@Qualifier(value="deptServiceImpl") @Qualifier(value="deptS")

	//依赖注入
	//如果使用resource按名称注入, @Resource(name="xxx")相当于上面两个注解
	//@Resource(name="deptServiceImpl")
	private DeptService deptService;

	
	public void showDept(){
		Dept d = deptService.getById(1);
		System.out.println("部门名:"+d.getDeptName()+"位置:"+d.getIoc());
	}
	
}
  1. 实体类
package com.aaa.ioc.entity;

public class Dept {

	private int deptId;
	private String deptName;
	private String Ioc;
	public int getDeptId() {
		return deptId;
	}
	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public String getIoc() {
		return Ioc;
	}
	public void setIoc(String ioc) {
		Ioc = ioc;
	}
	public Dept(int deptId, String deptName, String ioc) {
		super();
		this.deptId = deptId;
		this.deptName = deptName;
		Ioc = ioc;
	}
}
  1. 测试类
package com.aaa.ioc.test;

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

import com.aaa.ioc.controller.DeptController;

public class DeptTest {

	public static void main(String[] args) {
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		DeptController deptController = (DeptController)applicationContext.getBean("deptC");
		deptController.showDept();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值