Spring的几个有用的小功能

一、 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

猜你喜欢

转载自garyjiao.iteye.com/blog/2246488