Spring 自动依赖注入
来源:互联网 发布:网络用语rz是什么意思 编辑:程序博客网 时间:2024/06/05 23:48
– Start
事实上,Spring 还支持自动依赖注入。
基于 XML 的自动依赖注入
package shangbo.spring.example31;public interface MessageService { String getMessage();}
package shangbo.spring.example31;public class MessageServiceImpl implements MessageService { public String getMessage() { return "Hello World"; }}
package shangbo.spring.example31;public class MessagePrinter { private MessageService service; public void setService(MessageService service) { this.service = service; } public void printMessage() { System.out.println(service.getMessage()); }}
<?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"> <bean class="shangbo.spring.example31.MessageServiceImpl"/> <!-- autowire="no" 表示手动注入 autowire="byType" 表示根据类型,通过 setter 方法自动注入 autowire="constructor" 表示根据类型,通过构造器方法自动注入 autowire="byName" 表示根据名字自动注入 --> <bean class="shangbo.spring.example31.MessagePrinter" autowire="byType"/></beans>
package shangbo.spring.example31;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App { public static void main(String[] args) { // 实例化 Spring IoC 容器 ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", MessagePrinter.class); // 从容器中获得 MessagePrinter 的实例 MessagePrinter printer = context.getBean(MessagePrinter.class); // 使用对象 printer.printMessage(); }}
除此之外,我们还可以全局设定自动注入方式。
<?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" default-autowire="byType"> <!-- 全局设定自动注入方式 --> <bean class="shangbo.spring.example32.MessageServiceImpl"/> <bean class="shangbo.spring.example32.MessagePrinter"/></beans>
基于注解的自动依赖注入
package shangbo.spring.example33;public interface MessageService { String getMessage();}
package shangbo.spring.example33;public class MessageServiceImpl implements MessageService { public String getMessage() { return "Hello World"; }}
package shangbo.spring.example33;import org.springframework.beans.factory.annotation.Autowired;public class MessagePrinter { private MessageService service; // 自动注入 @Autowired public void setService(MessageService service) { this.service = service; } public void printMessage() { System.out.println(service.getMessage()); }}
package shangbo.spring.example33;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class AppConfig { @Bean public MessageService messageService() { return new MessageServiceImpl(); } @Bean public MessagePrinter messagePrinter() { return new MessagePrinter(); }}
package shangbo.spring.example33;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class App { public static void main( String[] args ) { // 实例化 Spring IoC 容器,也可以一次读取多个Java配置文件 ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); // 从容器中获得 MessagePrinter 的实例 MessagePrinter printer = context.getBean(MessagePrinter.class); // 使用对象 printer.printMessage(); }}
– 更多参见:Spring Framework 精萃
– 声 明:转载请注明出处
– Last Updated on 2017-05-23
– Written by ShangBo on 2017-05-23
– End
阅读全文
0 0
- spring 自动依赖注入
- Spring 自动注入依赖
- Spring 自动依赖注入
- Spring依赖注入,自动扫描组件,自动装配,注解注入
- Spring如何实现自动依赖注入-------autowire
- Spring依赖注入 — 属性自动装配
- Spring依赖注入 — 属性自动装配
- Spring自动扫面和依赖注入
- Spring如何实现自动依赖注入-------autowire
- Spring如何实现自动依赖注入-------autowire
- Spring如何实现自动依赖注入-------autowire
- Spring如何实现自动依赖注入-------autowire
- Spring 自动依赖注入优化(primary)
- Spring 自动依赖注入优化(qualifier)
- Spring 自动依赖注入优化(autowire-candidate)
- Spring DI依赖注入,自动装配
- spring依赖注入,自动转配
- Spring 依赖注入:自动注入properties文件中的配置
- OpenGL ES零基础入门四矩形的绘制
- bzoj 1022
- Python 实践心得 —— 中英混杂的字符串对齐输出
- Unity中的持久化数据PlayerPrefs
- 学生学籍管理系统~~功能界面
- Spring 自动依赖注入
- python入门基础笔记
- 从Okhttp的建造者模式开始讲
- [实训]Base64学习
- Andorid实现垃圾桶开关动画
- 图片切换(二)(div+css+js)
- Redis之——Redis集群的高可用测试与集群重建
- 修改mysql5.6.24解压缩版编码格式
- OCP+Docker牛刀小试,轻松完成“百万并发”压力测试