Spring的JavaConfig

spring曾经很多东西也是被诟病的,当然这也是说明有很多进步的空间,其中xml的配置是诟病最多的,后面spring就推出了JavaConfig来进行配置优化。这篇文章就由简入繁进行介绍。

一、@Configuration

<?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">
   
    <!-- bean定义 -->
</beans>
@Configuration
public class MockConfiguration{
    // bean定义
} 

二、@Bean

<bean id="mockService" class="..MockServiceImpl"> ...</bean>
@Configuration
public class MockConfiguration {
	/*给容器中注册一个bean,类型是方法返回值,id就是方法名称*/
    @Bean
    public MockService mockService() {
        return new MockServiceImpl();
    }
}

注意:
下面是xml和JavaConfig中bean与bean之间依赖的配置:

<bean id="mockService" class="..MockServiceImpl">
    <property name="dependencyService" ref="dependencyService" />
</bean>
<bean id="dependencyService" class="DependencyServiceImpl" /> 
@Configuration
public class MockConfiguration {
    @Bean
    public MockService mockService() {
        return new MockServiceImpl(dependencyService());
    }
    @Bean
    public DependencyService dependencyService() {
        return new DependencyServiceImpl();
    }
}

三、@ComponentScan

(1)xml配置

<context:component-scan base-package="..." use-default-filters="true">

1.base-package: 指定扫描路径。
spring会去自动扫描base-package对应的路径或者该路径的子包下面的带有@Service,@Component,@Repository,@Controller注解的java文件。

<context:componet-scan base-package="com.sff.app" use-default-filters="true">
        <!--不扫描@Controller注解的类-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:componet-scan>
<context:componet-scan base-package="com.sff.app" use-default-filters="false">
        <!--只扫描@Controller注解的类-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:componet-scan>

2.use-default-filters: 是否使用默认的扫描过滤器。
use-default-filters=true表示按照自定义规则扫描包路径下的类

(2)@ComponentScan的使用
1.该注解是使用在我们的配置类上的。

/**
 * 配置类等价于spring的配置文件
 */
@Configuration
@ComponentScan(value = "com.sff.app",
        includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)},
        useDefaultFilters = false)
public class AppConfig {
    /*给容器中注册一个bean,类型是方法返回值,id就是方法名称*/
    @Bean
    public Person person() {
        return new Person("Kate", 12);
    }
}

2.属性说明
basePackages与value: 用于指定包的路径,进行扫描
basePackageClasses: 用于指定某个类的包的路径进行扫描
nameGenerator: bean的名称的生成器
useDefaultFilters: 是否开启对@Component,@Repository,@Service,@Controller的类进行检测
includeFilters: 包含的过滤条件
FilterType.ANNOTATION:按照注解过滤
FilterType.ASSIGNABLE_TYPE:按照给定的类型
FilterType.ASPECTJ:使用ASPECTJ表达式
FilterType.REGEX:正则
FilterType.CUSTOM:自定义规则
excludeFilters: 排除的过滤条件,用法和includeFilters一样

四、@EnableWebMvc

<mvc:annotation-driven/>

@EnableWebMvc出现自Spring3.1的版本;将@EnableWebMvc添加给@Configuration类来导入SpringMvc的配置;如果还想要自定义配置,移除@EnableWebMvc,并且继承WebMvcConfigurationSupport或DelegatingWebMvcConfiguration。
注意:
当使用@EnableWebMvc时,加载的是WebMvcConfigurationSupport中的配置项。
当不使用@EnableWebMvc时,使用的是WebMvcAutoConfiguration引入的配置项。
这个注解的使用会有很多坑点,具体可参考:
《Spring注解@EnableWebMvc使用坑点解析》

五、@ImportResource

<import resource="applicationContext-redis.xml"/>

将多个分开的容器配置合到一个配置中,在 JavaConfig 形式的配置中,我们则使用 @Import 这个 Annotation 完成同样目的。

@Configuration
@Import(MockConfiguration.class)
public class XConfiguration {
    ...
}

@Import 只负责引入 JavaConfig 形式定义的 IoC 容器配置,如果有一些遗留的配置或者遗留系统需要以 XML 形式来配置(比如 dubbo 框架),我们依然可以通过 @ImportResource 将它们一起合并到当前 JavaConfig 配置的容器中。

六、@PropertySource

<context:property-placeholder location="classpath:*.properties"/>

@PropertySource 用于从某些地方加载 *.properties 文件内容,并将其中的属性加载到 IoC 容器中,便于填充一些 bean 定义属性的占位符(placeholder)。
使用 Java 8 或者更高版本开发,那么,我们可以并行声明多个 @PropertySource:

@Configuration
@PropertySource("classpath:1.properties")
@PropertySource("classpath:2.properties")
@PropertySource("...")
public class XConfiguration{
    ...
}

使用低于 Java 8 版本的 Java 开发 Spring 应用,又想声明多个 @PropertySource,则需要借助**@PropertySources**:

@PropertySources({ @PropertySource("classpath:1.properties"), @PropertySource("classpath:2.properties"), ...})
public class XConfiguration{
    ...
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值