我们都知道,如果要在不同的类中使用同一个对象一般我们我们都需要在每一个类中都去new一个新的对象,也有的人会为这个对象写一个工具类,无论哪种方法都需要我们自己去创建,不但繁琐,而且相当耗损资源,所以才有了Spring使用的必要性,也就是说,Spring的众多功能中为我们创建对象就是其中之一。话不多说直接上代码:

 /*
*dao层接口
*/
public interface AccountDao {
void say();
}
 /*
* dao层实现类
*/
public class AccountDaoImpl implements AccountDao {
@Override
public void say() {
System.out.println("我是dao层>>>>>>>>>>>>>");
}
}
/**
* service层接口
*/
public interface AccountService {
void say();
}
 /**
* service实现类
*/
public class AccountServiceImpl implements AccountService { private AccountDao dao;//创建dao层成员,并写出它的set,get方法 @Override
public void say() {
       dao.say();//直接调用dao层的方法
System.out.println("我是service层>>>>>>>>>>>>>");
} //get方法
public AccountDao getDao() {
return dao;
}
//set方法
public void setDao(AccountDao dao) {
this.dao = dao;
}
}

第一种纯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">
<!--创建dao层的bean,其中class属性指向的dao接口的实现类,只能时实现类,不能是接口-->
<bean id="dao" class="com.lhf.dao.impl.AccountDaoImpl"/>
<!--创建service层的bean-->
<bean id="service" class="com.lhf.service.impl.AccountServiceImpl">
<!--向service层注入dao的bean,可以把它之间看成是在service层创建一个dao的对象,
其中的name属性是service层的成员变量,如果成员是基本类型,可以用value属性直接为之赋值-->
<property name="dao" ref="dao"/>
</bean>
</beans>

创建测试类

 public class App
{
public static void main( String[] args )
{
//spring读取xml配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从spring容器中获取service层的对象
AccountService bean = context.getBean(AccountService.class);
//调用service层的方法
bean.say();
}
}

第一种注解方式,需要一个xml配置。接口都不变,只是在实现类上有些变化

 /*
* dao层实现类
*/
@Repository("dao")//相当于xml中配置的dao层的bean节点
public class AccountDaoImpl implements AccountDao {
@Override
public void say() {
System.out.println("我是dao层>>>>>>>>>>>>>");
}
}
/**
* service实现类
*/
@Service("service")//相当于xml层中配置的service的bean节点
public class AccountServiceImpl implements AccountService {
@Autowired//默认按类型匹配
@Qualifier("dao")//与autowired连用可以按名字匹配
private AccountDao dao;//创建dao层成员,并写出它的set,get方法 @Override
public void say() {
dao.say();
System.out.println("我是service层>>>>>>>>>>>>>");
} //get方法
public AccountDao getDao() {
return dao;
}
//set方法
public void setDao(AccountDao dao) {
this.dao = dao;
}
}

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"
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.xsd">
<!--扫描包-->
<context:component-scan base-package="com.lhf"/>
<!--开启ioc注解-->
<context:annotation-config/>
</beans>

测试类不变,自行测试

接下来最后一种就是不需要xml但是需要一个核心配置类AppConfig,取代xml

 @Configuration//标识,代表本类为核心配置类
@ComponentScan("com.lhf")//需要扫描的包的位置
public class AppConfig {
//暂时什么都不用写,后续又其他操作
}

接口和实现类与上一种相同但是测试类略有变化

 public class App
{
public static void main( String[] args )
{
//spring读取核心配置类
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
//从spring容器中获取service层的对象
AccountService bean = context.getBean(AccountService.class);
//调用service层的方法
bean.say();
}
}

以上就是spring中对属性ioc对对象注入的几种方式。这里写的不全,有需要的可以查以下官方文档。

Spring_IOC的更多相关文章

  1. Spring_IOC&DI概述

  2. 分享知识-快乐自己:Spring_IOC(控制反转)详解

    IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...

  3. Spring_IoC注解开发和AOP的XML开发(学习笔记2)

    一:IoC注解开发 1,在applicationContext.xml中需要引入context约束 <beans xmlns="http://www.springframework.o ...

  4. coding++:Spring_IOC(控制反转)详解

    IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...

  5. 【转】Spring_IOC学习

    原文地址:http://github.thinkingbar.com/spring/ 一.XML文件语法的知识点 对于XML没有提示的话,在Eclipse中搜索XML catalog设置.对于XML文 ...

  6. Spring依赖注入 --- 简单使用说明

    Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...

  7. Maven使用教程

    一.Maven介绍 我们在开发项目的过程中,会使用一些开源框架.第三方的工具等等,这些都是以jar包的方式被项目所引用,并且有些jar包还会依赖其他的jar包,我们同样需要添加到项目中,所有这些相关的 ...

  8. Java Web系列:Spring依赖注入基础

    一.Spring简介 1.Spring简化Java开发 Spring Framework是一个应用框架,框架一般是半成品,我们在框架的基础上可以不用每个项目自己实现架构.基础设施和常用功能性组件,而是 ...

  9. 开放源代码的设计层面框架Spring——day02

    spring第二天     一.基于注解的IOC配置         1.1写在最前             学习基于注解的IOC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能是 ...

随机推荐

  1. Access修改窗体的名称,用于VBA代码的调用

  2. POJ 1979:Red and Black

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26058   Accepted: 14139 D ...

  3. Django2.0——请求与响应(下)

    上篇讲完了请求,这篇接着讲下响应,django响应类型大致有以下几种 HttpResponse:返回简单的字符串 render:渲染模板 redirect:重定向 JsonResponse:返回jso ...

  4. 深度学习在美团配送ETA预估中的探索与实践

    1.背景 ETA(Estimated Time of Arrival,“预计送达时间”),即用户下单后,配送人员在多长时间内将外卖送达到用户手中.送达时间预测的结果,将会以”预计送达时间”的形式,展现 ...

  5. java 面向对象概述, 函数解析

    函数:class FunctionDemo { public static void main(String[] args) { /* int x = 4; System.out.println(x* ...

  6. Android java项目中引用kotlin,混合开发工程配置

    https://www.jianshu.com/p/9220227cdfb3 buildscript { ext.kotlin_version = '1.2.71' repositories { go ...

  7. Sort - Merge Sort

    归并排序思想 归并排序时间复杂度分析 归并排序特点 归并排序实现 递归 #include <iostream> using namespace std; void merge(int *a ...

  8. 如何决定 Web 应用的线程池大小

    在部署 web 应用到生产环境,或者在对 web 应用进行性能测试的时候,经常会有人问:如何决定 web 应用线程池大小?决定一个 IO 阻塞型 web 应用的线程池大小是一项很艰巨的任务.通常是通过 ...

  9. 二、Shell脚本高级编程实战第二部

    一.什么是变量? 变量就是一个固定的字符串替代更多更复杂的内容,当然内容里面可能还有变量.路径.字符串等等内容,最大的特点就是方便,更好开展工作 1.变量有环境变量(全局变量)和局部变量 环境变量就是 ...

  10. python的稀疏矩阵计算

    尽量避免稀疏矩阵, 加快计算. 比如计算稀疏矩阵S的F范数 a = norm(S, 'fro'), 方法1效率比方法2高很多. 方法 1 import numpy as np a = np.linal ...