博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 自动依赖注入
阅读量:4049 次
发布时间:2019-05-25

本文共 2637 字,大约阅读时间需要 8 分钟。

– 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());    }}
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();	}}

除此之外,我们还可以全局设定自动注入方式。

基于注解的自动依赖注入

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();    }}

– 声 明:转载请注明出处
– Last Updated on 2017-05-23
– Written by ShangBo on 2017-05-23
– End

你可能感兴趣的文章
Love Your Life》—— 热爱生活
查看>>
一个高速交警的忠告
查看>>
新车装饰的中国特色
查看>>
没看过这么NB的自驾游,笑的我眼泪都出来了
查看>>
李涯的哭
查看>>
和机器学习和计算机视觉相关的数学
查看>>
论文MICO for MRI bias field estimation and tissue segmentation品讲
查看>>
后现代
查看>>
VMware6关机后出现is not a valid virtual machine configuration file的解决办法
查看>>
通过ASP实现flash对数据库的访问
查看>>
“==”和equals方法究竟有什么区别?
查看>>
哈佛图书馆墙上的20条训言
查看>>
交流引发深入思考
查看>>
保持我们母语的纯洁
查看>>
免费的互联网时代如何盈利?
查看>>
可怕的宣传力量
查看>>
症状:可以上网,可以上QQ,不能登陆360安全卫士,360浏览器无法同步,有道词典等无法登陆,无法查询。
查看>>
重读《触龙说赵太后》
查看>>
2010的第一次思想触动
查看>>
文学大师做菜艺术20个"须知"
查看>>