Spring_IOC
我们都知道,如果要在不同的类中使用同一个对象一般我们我们都需要在每一个类中都去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的更多相关文章
- Spring_IOC&DI概述
- 分享知识-快乐自己:Spring_IOC(控制反转)详解
IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...
- Spring_IoC注解开发和AOP的XML开发(学习笔记2)
一:IoC注解开发 1,在applicationContext.xml中需要引入context约束 <beans xmlns="http://www.springframework.o ...
- coding++:Spring_IOC(控制反转)详解
IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...
- 【转】Spring_IOC学习
原文地址:http://github.thinkingbar.com/spring/ 一.XML文件语法的知识点 对于XML没有提示的话,在Eclipse中搜索XML catalog设置.对于XML文 ...
- Spring依赖注入 --- 简单使用说明
Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...
- Maven使用教程
一.Maven介绍 我们在开发项目的过程中,会使用一些开源框架.第三方的工具等等,这些都是以jar包的方式被项目所引用,并且有些jar包还会依赖其他的jar包,我们同样需要添加到项目中,所有这些相关的 ...
- Java Web系列:Spring依赖注入基础
一.Spring简介 1.Spring简化Java开发 Spring Framework是一个应用框架,框架一般是半成品,我们在框架的基础上可以不用每个项目自己实现架构.基础设施和常用功能性组件,而是 ...
- 开放源代码的设计层面框架Spring——day02
spring第二天 一.基于注解的IOC配置 1.1写在最前 学习基于注解的IOC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能是 ...
随机推荐
- Python—数据结构——链表
数据结构——链表 一.简介 链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性存储结构.由一系列节点组成的元素集合.每个节点包含两部分,数据域item和指向下一个节 ...
- 我的第一次JAVA实训——校园公用房管理系统
老铁们,昨天电脑没电了.这是上周答应的大项目. 别打脸. 详细内容我之后再写,下面是代码地址. Github地址 附:一个寂寞 以上
- symmetry methods for differential equations,exercise 1.4
tex文档: \documentclass[a4paper, 12pt]{article} % Font size (can be 10pt, 11pt or 12pt) and paper size ...
- Python笔记_第一篇_面向过程_第一部分_0.开场白
*什么是Python? Python是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido(吉多) van Rossum于1989年发明,第一个公开版本发行于1991年.在国外应用非常的广泛,国 ...
- Java线程(一)——创建线程的两种方法
Thread 和 Runnable Java程序是通过线程执行的,线程在程序中具有独立的执行路径.当多条线程执行时,它们之间的路径可以不同,例如,一条线程可能在执行switch的一个case语句,另一 ...
- 内存管理-MRC
MRC内存管理 环境:先关闭arc模式,选中项目->build Settings
- oracle误删scott文件如何恢复
找到oracle的路径,一般是 某盘:\app\用户名\product\11.2.0\dbhome_1\RDBMS\ADMIN\scott.sql 这样找到scott.sql ,其中有恢复所有内容的S ...
- oracle学习笔记(六)——函数&存储过程的异同
我看的书上除了能看出来函数有返回值,存储过程没有,其他啥也看不出来... 网上大大的总结
- Linux之程序的开始和结束
1.main函数由谁来调用 (1).编译链接时的引导代码. 操作系统下的应用程序其实是在main函数执行前也需要先执行一段引导代码才能去执行main函数,我们写应用程序时不用考虑引导代码的问题,编译链 ...
- python3 subprocess 内存操作视频转换流格式
import subprocessout = open('./tmp/sss.mp4','rb').read()p = subprocess.Popen(["./ffmpeg",& ...