Spring的几个有用的小功能

3 篇文章 0 订阅

一、 Spring的Profile功能

这个功能的使用方法其实和Maven的profile是一样的,可以通过不同的参数来激活某些配置,这样便于我们在开发、调试以及生产模式下进行切换。

对于类的配置,如果需要启用Profile功能,有以下几种方式:

    1. 对于在配置类上使用注解方式的配置

 

@Configuration
@Profile(value="productprofile")
public class ProductConfig {
...
}

 

    2. 对于使用注解方式定义类

 

@Bean
@Profile("dev")
public Person getPerson() {
}

 

 

    3. 在XML里使用

 

	<beans>
		<bean id="person" class="com.test.spring.profile.ProfilePerson">
			<constructor-arg value="The message comes from default enviorment."/>
		</bean>
		
		<bean id="person2" class="com.test.spring.profile.ProfilePerson">
			<constructor-arg value="The message comes from default2 enviorment."/>
		</bean>
	</beans>
	
	<beans profile="dev">
		<bean id="person" class="com.test.spring.profile.ProfilePerson">
			<constructor-arg value="The message comes from development enviorment."/>
		</bean>
	</beans>
	
	<beans profile="prd">
		<bean id="person" class="com.test.spring.profile.ProfilePerson">
			<constructor-arg value="The message comes from production enviorment."/>
		</bean>
	</beans>

     4. 也可以将特定的配置集中在某些文件里,然后通过参数来激活   

<import resource="spring/spring-profile-${spring.profiles.active}.xml"/>

 

总结一下,就是以下这些:

1.ENV方式: ConfigurableEnvironment.setActiveProfiles("unittest")
2.代码方式:System.setProperty("spring.profiles.active", "dev")
3.JVM参数方式: -Dspring.profiles.active="unittest"
4.web.xml方式:
<init-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>production</param-value>
</init-param>
5.标注方式(junit单元测试非常实用):
@ActiveProfiles({"unittest","productprofile"})

 

 

 

 

二、Spring里的事件发布功能

之前看过Guava的事件发布,回过头发现Spring也已经提供了,对于项目里简单的事件触发功能,肯定是够用了。以下内容转载自:http://wiselyman.iteye.com/blog/2212013

       应按照如下部分实现bean之间的消息通讯

  • 继承ApplicationEvent类实现自己的事件
  • 实现继承ApplicationListener接口实现监听事件
  • 使用ApplicationContext发布消息

 

//事件类
import org.springframework.context.ApplicationEvent;

public class DemoEvent extends ApplicationEvent{
    private static final long serialVersionUID = 1L;
    private String msg;

    public DemoEvent(Object source,String msg) {
        super(source);
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

//监听类
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {

    public void onApplicationEvent(DemoEvent event) {
            String msg = ((DemoEvent) event).getMsg();
            System.out.println("我监听到了pulisher发布的message为:"+msg);

    }

}

//测试类
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext("com.event");
        Main main =context.getBean(Main.class);
        main.pulish(context);
        context.close();

    }
    public void pulish(AnnotationConfigApplicationContext context){
        context.publishEvent(new DemoEvent(this, "22"));
    }

}

 

 

三、Spring的Conditional功能

其实这个功能感觉上跟Profile是类似的,主要是Conditional允许自定义规则,更加灵活一些。

这里有一篇文章,对两者有一个示例性的说明:

http://blog.csdn.net/yangxt/article/details/19970323

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架是一个非常流行的开发框架,它提供了众多的特性和功能,如依赖注入、AOP、事务管理等等。如果要深入理解Spring框架,最好的方法就是阅读它的源代码。在这里,我将简单介绍一下Spring源码的结构和阅读方法,希望对你有所帮助。 Spring源码的结构 Spring源码的结构非常清晰,主要分为以下几个模块: 1. Spring Core:这是Spring框架的核心模块,提供了依赖注入和AOP功能等基本特性。 2. Spring Context:这个模块构建在Spring Core之上,提供了更高级的特性,如国际化、事件传播等。 3. Spring Web:这个模块提供了与Web相关的特性,如MVC框架、RestTemplate等。 4. Spring Data:这个模块提供了与数据访问相关的特性,如JPA、MongoDB等支持。 5. Spring Security:这个模块提供了安全相关的特性,如认证、授权等。 阅读Spring源码的方法 1. 熟悉设计模式 Spring框架使用了很多设计模式,如工厂模式、单例模式、装饰器模式等等。因此,在阅读Spring源码之前,你需要熟悉这些设计模式的基本原理。 2. 了解Spring的工作原理 在阅读Spring源码之前,你需要了解Spring框架的基本原理,如依赖注入、AOP、Bean生命周期等等。这些知识将帮助你更好地理解Spring源码。 3. 从Spring的入口开始 阅读Spring源码的最好方法是从Spring的入口开始,这通常是一个ApplicationContext对象。你可以通过调试工具进入ApplicationContext的构造函数,从而了解Spring框架是如何初始化的。 4. 跟踪代码流程 在阅读Spring源码时,你需要跟踪代码的流程,了解每个方法的作用和调用关系。这将帮助你更好地理解Spring框架的内部实现。 5. 阅读注释和文档 Spring源码中有很多注释和文档,这些都是非常有用的参考资料。在阅读源码时,你应该仔细阅读这些注释和文档,以便更好地理解代码的含义和作用。 总结 阅读Spring源码是一个非常有挑战性的任务,但也是一个非常有价值的学习过程。通过阅读Spring源码,你将深入理解Spring框架的内部实现,从而更好地应用它的特性和功能

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值