say-event-provider.xmljava
<!-- lang: xml --> <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" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <bean id="eventService" class="com.say.event.provider.EventServiceImpl" /> <dubbo:service interface="com.say.event.EventService" ref="eventService" /> <bean id="eventMomentService" class="com.say.event.provider.EventMomentServiceImpl" /> <dubbo:service interface="com.say.event.EventMomentService" ref="eventMomentService" /> <dubbo:reference id="pushService" interface="com.say.user.PushService" /> <context:component-scan base-package="com.say.event"/> <task:annotation-driven/>.....
xxx.javaspring
<!-- lang: java --> @Component @Transactional public class EventServiceImpl implements EventService { final static Logger logger = Logger.getLogger(EventServiceImpl.class); .... @Scheduled(cron="0 0/1 * * * ?") //每分鐘觸發一次 public void test(){ System.err.println(this +" === "+ new Date() +" === "+ Thread.currentThread().getId() +" === "+ Thread.currentThread().getName() ); }
執行結果:ide
上圖中看到在同一時間點執行了兩次。 同時,查以經過劃紅色下劃紅的結果: com.say.event.provider.EventServiceImpl**@20fc40ae** com.say.event.provider.EventServiceImpl**@19aa5882** 能夠看出他們不是同一個實例,因爲能夠看出是EventServiceImpl 的兩個不一樣實例的*test()*方法分別被執行了一次,加來共爲兩次。this
爲何執行了兩次, 或者說什麼會產兩個實例?,Spring Bean的scope 默承認是singleton的哦...code
緣由就出在say-event-provider.xml的component
<!-- lang: xml --> <bean id="eventService" class="com.say.event.provider.EventServiceImpl" />
和 xxx.java的第一行代碼 @Component註解。它們兩個會使用Spring 在初始化的過程當中分別產生一個EventServiceImpl的實例。因此致使以上的結果。xml