Spring(18)——使用Java类的形式定义bean(二)

18.2 @Configuration

@Configuration是标注在Class上的,当我们需要基于Java类的形式对Spring进行配置时,我们就需要在对应的配置类上使用@Configuration进行标注,这样Spring才会把对应的Class当做是一个配置用的Class。使用@Configuration进行标注的配置类默认也会被Spring作为一个bean进行注册。所以当我们的拥有多个@Configuration进行标注的配置类时,如果需要进行依赖注入时对应的依赖项处于不同的@Configuration配置类时也可以通过注入@Configuration配置类,然后通过注入的@Configuration配置类实例的对应方法产生目标依赖bean的实例进行注入。如下示例中我们拥有两个Java配置类,其中ServiceConfig中的UserService需要注入一个在DaoConfig中定义的UserDao,然后我们就在ServiceConfig中注入了一个DaoConfig,再通过调用DaoConfig的userDao()方法获取对应的UserDao实例注入给UserService的实例。

@Configuration
public class ServiceConfig {
	
	@Autowired
	private DaoConfig daoConfig;

	@Bean
	public UserService userService() {
		UserService userService = new UserServiceImpl(daoConfig.userDao());
		return userService;
	}
	
}

@Configuration
public class DaoConfig {

	@Bean
	public UserDao userDao() {
		UserDao userDao = new UserDaoImpl();
		return userDao;
	}
	
}

18.2.1 扫描类路径以注册bean

基于Java类的Spring配置也可以像基于XML配置一样让Spring扫描类路径以将标注了@Component等注解的Class注册为bean容器中的bean定义。这是通过在配置上使用@ComponentScan进行标注来进行定义的。

@Configuration
@ComponentScan(basePackages="com.app")
public class SpringConfig {

}

在上述代码中我们就通过@ComponentScan定义了将扫描类路径下com.app包及其子孙包下所有标注了@Component等注解的Class,并将它们作为一个bean定义注册到当前的bean容器中。其就相当于如下XML定义。

<context:component-scan base-package="com.app"/>

通过@ComponentScan的includeFilters和excludeFilters属性可以指定在扫描时需要使用的过滤器,对应的过滤器是通过@Filter进行指定的,可以通过其type属性指定过滤器的类型,默认为ANNOTATION。如下示例表示将排除@Service注解。

@Configuration
@ComponentScan(basePackages = "com.app", excludeFilters={@Filter(Service.class)})
public class ControllerConfig {

}

基本上使用<context:component-scan/>能定义的内容,@ComponentScan都有与之相对应的属性进行定义,更多信息请参考Spring API文档中ComponentScan的相关信息。

18.3 实例化bean容器

18.3.1 非Web环境

对于非Web环境的bean容器实例化,Spring为我们提供了一个对应的ApplicationContext实现类——AnnotationConfigApplicationContext。实例化AnnotationConfigApplicationContext时我们可以直接将Java配置类作为构造参数传入。

	public static void main(String args[]) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
		context.close();
	}

当有多个Java配置类时,我们可以给AnnotationConfigApplicationContext传递多个Java配置类。

	public static void main(String args[]) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				SpringConfig.class, ServiceConfig.class, DaoConfig.class);
		context.close();
	}

我们还可以先构造一个空的AnnotationConfigApplicationContext,然后通过对应的register()方法来注册用于配置的Java类。

	public static void main(String args[]) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(SpringConfig.class);//注册单个配置类
		context.register(ServiceConfig.class, DaoConfig.class);//注册多个配置类
		context.refresh();//注册完后一定要refresh()一次
		context.close();
	}

此外,需要注意的是AnnotationConfigApplicationContext不仅仅接收使用@Configuration进行标注的类作为配置类,其还可以接收使用@Component等用于标注为bean的注解进行标注的类作为配置类。所以,当我们直接使用AnnotationConfigApplicationContext时,我们可以选择在对应的配置类上使用@Configuration进行标注,也可以选择使用@Component等定义bean的注解进行标注。
关于AnnotationConfigApplicationContext的更多信息请参考Spring API文档。

18.3.2 Web环境

对于Web环境而言,Spring针对基于Java类配置提供了一个对应WebApplicationContext的实现类—AnnotationConfigWebApplicationContext。如果需要使用该WebApplicationContext,我们需要通过在web.xml文件中通过<context-param/><init-param/>进行指定。

我们知道通常在Web环境下使用Spring时我们都需要在web.xml文件中定义如下Listener,Spring将通过该Listener来实例化WebApplicationContext,默认会实例化一个基于XML配置的XmlWebApplicationContext。

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

如果我们希望ContextLoaderListener实例化的WebApplicationContext是一个AnnotationConfigWebApplicationContext,那么我们可以通过在web.xml中定义如下参数,该参数将会被ContextLoaderListener用来实例化对应的WebApplicationContext实现类。

<context-param>
	<param-name>contextClass</param-name>
	<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

我们知道基于XML配置的ContextLoaderListener默认取WEB-INF目录下的applicationContext.xml文件作为对应的配置文件,且对应的配置我们可以通过参数contextConfigLocation进行配置。那么当我们使用的WebApplicationContext是AnnotationConfigWebApplicationContext时对应的contextConfigLocation也需要变更为我们的Java配置类,且必须是全名的,即包含对应的包名称,多个配置类之间以逗号或分号或空格隔开。所以对于使用Spring的Web应用,我们应该如下定义对应的ContextLoaderListener。在如下示例中我们就通过contextClass参数指定了对应的contextClass为AnnotationConfigWebApplicationContext,然后通过contextConfigLocation参数指定了三个Java配置类。

<context-param>
	<param-name>contextClass</param-name>
	<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>com.app.SpringConfig,com. app.ServiceConfig,com.app.DaoConfig</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

而对于使用SpringMVC而言,如果我们是使用基于Java类的配置来配置对应的SpringMVC定义,则需要在定义DispatcherServlet时通过来定义需要使用的contextClass和contextConfigLocation。如下示例中我们就指定了SpringMVC将使用AnnotationConfigWebApplicationContext作为contextClass,对应的Java配置类为com.app.ControllerConfig。

<servlet>
	<servlet-name>spring</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextClass</param-name>
		<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
	</init-param>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>com.app.ControllerConfig</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>spring</servlet-name>
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

18.3.3 测试环境

对于使用Spring Test进行单元测试而言,我们可以直接通过@ContextConfiguration的classes属性来指定需要使用的Java配置类。如下示例中我们就指定了将使用SpringConfig、ServiceConfig和DaoConfig三个Java配置类来构建对应的ApplicationContext。

@ContextConfiguration(classes={SpringConfig.class, ServiceConfig.class, DaoConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class ConfigurationTest {
	
	@Autowired
	private ApplicationContext context;
	
	@Test
	public void test() {
		context.getBean("hello", Hello.class);
	}

}

猜你喜欢

转载自elim.iteye.com/blog/2389625