Spring 管理对象

– Start
点击此处观看本系列配套视频。


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

package shangbo.spring.core.example1;

public class OutPutService {
	
	public void outPut() {
        System.out.println("Hello World");
    }
	
}

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

<?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">

	<!-- 
		id 指定 bean 的标识符,必须唯一,如果没有设置,spring 帮我们生成一个
		name 也用来标识 bean,但可以起多个名字,不是必须要设置的
		class 指定类的全名
	-->
	<bean id="outPutService"
		name="outPutService1,outPutService2"
		class="shangbo.spring.core.example1.OutPutService"/>

</beans>

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

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 配置元数据
@Configuration
public 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 自动管理
@Component
public class OutPutService {

	public void outPut() {
		System.out.println("Hello World");
	}

}

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

<?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">

	<!-- 
		需要告诉 Spring 扫描组件的位置
	 -->
    <context:component-scan base-package="shangbo.spring.core.example3"/>

</beans>

然后是测试类。

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;

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

更多参见:Spring Framework 精萃
– 声 明:转载请注明出处
– Last Updated on 2017-06-17
– Written by ShangBo on 2017-05-20
– End

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值