博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 管理对象
阅读量:4050 次
发布时间:2019-05-25

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

– Start


当我们需要某个对象时,通常的做法是 new 一个对象,然后使用它。使用 Spring 后,套路完全变了,Spring 帮我们创建对象,然后缓存到它的容器中,当我们需要一个对象时,问 Spring 要即可,怎么样?想法是不是非常妙,我们来看个例子吧。

首先,定义一个类。

package shangbo.spring.core.example1;public class OutPutService {		public void outPut() {        System.out.println("Hello World");    }	}

然后定义一个 XML 配置文件,用来告诉 Spring 需要创建哪些对象以及如何创建对象。

最后定义一个客户端测试类。

package shangbo.spring.core.example1;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器,也可以一次读取多个配置文件		// ApplicationContext 有很多子类,可以根据你的需求选择		ApplicationContext context = new ClassPathXmlApplicationContext("shangbo/spring/core/example1/example.xml");		// 从容器中获得 Service 对象,传统方式是自己 new 对象		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut();	}}

从这个例子我们可以看到,我们需要以某种方式告诉 Spring,哪些对象需要它管理,以及创建对象的细节,Spring 称之为配置元数据(Configuration metadata), 这个例子使用 XML 配置元数据。下面我们看一个 Java 配置元数据的例子。

首先定义一个类。

package shangbo.spring.core.example2;public class OutPutService {		public void outPut() {        System.out.println("Hello World");    }	}

然后定义一个Java配置类, 用来告诉 Spring 需要创建哪些对象以及如何创建对象。

package shangbo.spring.core.example2;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;// @Configuration 用来标识该类是一个 Java 配置元数据@Configurationpublic class AppConfig {	// @Bean 用来标识该对象需要 Spring 帮我们管理	@Bean	public OutPutService outPutService() {		return new OutPutService();	}}

最后定义一个客户端测试类。

package shangbo.spring.core.example2;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);		// 从容器中获得 Service 对象,传统方式是自己 new 对象		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut();	}}

通过上面的例子可以看到,不管使用 XML 或 Java 配置元数据,我们仍然需要明确告诉 Spring 哪些对象需要它管理,以及创建对象的细节,有没有可能完全自动化呢?答案是肯定的,Spring 还支持基于注解的配置元数据,看看下面的例子吧。

首先还是定义一个类。

package shangbo.spring.core.example3;import org.springframework.stereotype.Component;// @Component 标识的类可以被 Spring 自动管理// @Service 标识的类也可以被 Spring 自动管理// @Controller 标识的类也可以被 Spring 自动管理// @Repository 标识的类也可以被 Spring 自动管理@Componentpublic class OutPutService {	public void outPut() {		System.out.println("Hello World");	}}

然后定义如下 XML 配置文件,注意我们需要引入context命名空间。

然后是测试类。

package shangbo.spring.core.example3;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器,也可以一次读取多个配置文件		// ApplicationContext 有很多子类,可以根据你的需求选择		ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", OutPutService.class);		// 从容器中获得 Service 对象,传统方式是自己 new 对象		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut();	}}

你也可以使用 Java 配置元数据。

首先定义一个类。

package shangbo.spring.core.example4;import org.springframework.stereotype.Component;@Componentpublic class OutPutService {	public void outPut() {		System.out.println("Hello World");	}}

然后定义一个Java配置类。

package shangbo.spring.core.example4;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration// 需要告诉 Spring 扫描组件的位置@ComponentScan(basePackages = "shangbo.spring.core.example4")public class AppConfig {	// 此处没有定义创建 bean 的细节}

最后定义一个测试类。

package shangbo.spring.core.example4;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);		// 从容器中获得 Service 对象,传统方式是自己 new 对象		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut();	}}

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

你可能感兴趣的文章
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt 创建异形窗体
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
输入设备节点自动生成
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
wpa_supplicant控制脚本
查看>>
gstreamer相关工具集合
查看>>
RS232 四入四出模块控制代码
查看>>
linux 驱动开发 头文件
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>