我们都知道,如果要在不同的类中使用同一个对象一般我们我们都需要在每一个类中都去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. F - Moving Points树状数组

    题:https://codeforces.com/contest/1311/problem/F 题意:给定x轴上的点以及他们的速度v,只在x轴上运动,求最小的dis之和,注意,这里的时间是可随意的,比 ...

  2. UML-GRASP总结

    对象设计的核心 1).对象交互 2).职责分配

  3. 踩一踩win7安装neo4j的坑

    本文使用zip解压方式安装,下载社区版zip 解压到喜欢的文件夹,然后配置环境变量NEO4J_HOME=D:\neo4j-community-3.5.5(自己的解压目录) 配置Path=%NEO4J_ ...

  4. Hadoop的伪分布式安装和部署流程

    在opt目录创建install software test other四个目录 /opt/installed #安装包/opt/software #软件包/opt/other #其他/opt/test ...

  5. 多线程下,两个线程交替打印0 -100,使用wait()和notify()

    多线程下,两个线程交替打印0 -100,使用wait()和notify() public class ThreadTest{ private static final Object lock = ne ...

  6. Python笔记_第四篇_高阶编程_进程、线程、协程_5.GPU加速

    Numba:高性能计算的高生产率 在这篇文章中,笔者将向你介绍一个来自Anaconda的Python编译器Numba,它可以在CUDA-capable GPU或多核cpu上编译Python代码.Pyt ...

  7. ModernCNN

    # 深度卷积神经网络(AlexNet) LeNet: 在大的真实数据集上的表现并不尽如⼈意. 1.神经网络计算复杂. 2.还没有⼤量深⼊研究参数初始化和⾮凸优化算法等诸多领域. 机器学习的特征提取:手 ...

  8. iTOP-4412-Ubuntu系统源码-ubuntu没有声音的解决办法

    准备工作 1.下载 vim 在命令行上输入 apt-get install vim 下载 vim 2.输入 vim /etc/hosts 在所打开界面的第一行最后写上 iTOP4412-ubuntu- ...

  9. 撤销上一次的commit

    撤销上一次的commit git reset HEAD~ 如果是撤销所有的已经add的文件: git reset HEAD .

  10. PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...