我们都知道,如果要在不同的类中使用同一个对象一般我们我们都需要在每一个类中都去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. UML-GRASP后4种模式

    1.多态 1).什么是多态 问题:if-else耦合度过高 解决: 方法1:接口 方法2:超类里需多态的方法前加上{abstract} 2).相关模式 防止异变 大量GoF,如适配器(Adapter) ...

  2. Codeforces Round #571 (Unrated for Div. 1+Div. 2)

    A 略 B 被删了,被这个假题搞自闭了,显然没做出来. C 开始莽了个NTT,后来发现会TLE,其实是个SB前缀和,对于这题,我无**说. #include<bits/stdc++.h> ...

  3. 33. docker swarm 集群服务通信 之 RoutingMesh - Ingress 网络

    1.作用 当在 任何 一个 swarm 节点去访问 端口服务的时候 会通过 本节点 的 IPVS ( ip virtual service ) 到 真正的 swarm 节点上 当访问 docker h ...

  4. Android开发—错误记录1:W/System.err: java.net.ConnectException: Connection refused

    W/System.err: java.net.ConnectException: Connection refused 前台访问后台时,出现访问被拒绝情况:W/System.err: java.net ...

  5. mysql安装完之后,登陆后发现只有两个数据库

    mysql安装完之后,登陆后发现只有两个数据库:mysql> show databases;+--------------------+| Database           |+------ ...

  6. 题解 P2738 【[USACO4.1]篱笆回路Fence Loops】

    这题是我期中测试的一题水题,然而英文题目太长了不想读...后面考完被同学提醒后20分钟切了(心塞) 切完看了波题解,发现貌似我的方法跟大家都不一样呢... 常规做法: \(Floyd\) 这个有三页的 ...

  7. 关于mysql一边查一边更新

    update test_table set user_id = 112 where id in (select id from ( select id from test_table where nu ...

  8. 题解-------CF372C Watching Fireworks is Fun

    传送门 一道有趣的DP 题目大意 城镇中有$n$个位置,有$m$个烟花要放.第$i$个烟花放出的时间记为$t_{i}$,放出的位置记为$a_{i}$.如果烟花放出的时候,你处在位置$x$,那么你将收获 ...

  9. Vue2--非父子组件通信笔记

    核心要点: var Event=new Vue(); Event.$emit(事件名称, 数据) Event.$on(事件名称,function(data){ //data }.bind(this)) ...

  10. HDU-6514 Monitor(二维前缀和+差分)

    http://acm.hdu.edu.cn/showproblem.php?pid=6514 Problem Description Xiaoteng has a large area of land ...