Spring 带有 Activiti BPMN 的 Bean
Spring Beans with Activiti BPMN
我目前正在研究 Spring Activiti 的 bean 实现。我需要将一个 bean 注入多个服务 类.
这是我试过的。
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.test.spring.HelloWorld" scope="singleton" autowire="byName">
<property name="message" value="Hello World!"/>
</bean>
</beans>
豆子Class
package com.test.spring;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
Activiti 服务任务
public class ServiceTask1 implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("Begin Trans : Execute:");
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
obj.setMessage("This is second message");
obj.getMessage();
}
}
public class ServiceTask2 implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("Begin Trans : Execute:");
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
尽管 ServiceTask1 为消息设置了新值,但它并未反映在 ServiceTask2 中。原因可能是我也在 ServiceTask2 中创建了新的 ApplicationContext。有人可以[让我知道如何在多个 Activiti 服务任务中使用同一个单例 Bean。
据我所知,您无法将 (@Autowire) bean 自动装配到 Java 委托中,因此为了访问您在应用程序配置中定义的 bean,您需要访问 applicationContext 并调用 getBean () 方法。
我通常会创建一个 ApplicationContext 提供程序 class 来简化此操作,因为您倾向于重复使用它。
这个 Whosebug post 展示了如何创建这样一个野兽:
Spring get current ApplicationContext
我目前正在研究 Spring Activiti 的 bean 实现。我需要将一个 bean 注入多个服务 类.
这是我试过的。
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.test.spring.HelloWorld" scope="singleton" autowire="byName">
<property name="message" value="Hello World!"/>
</bean>
</beans>
豆子Class
package com.test.spring;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
Activiti 服务任务
public class ServiceTask1 implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("Begin Trans : Execute:");
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
obj.setMessage("This is second message");
obj.getMessage();
}
}
public class ServiceTask2 implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("Begin Trans : Execute:");
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
尽管 ServiceTask1 为消息设置了新值,但它并未反映在 ServiceTask2 中。原因可能是我也在 ServiceTask2 中创建了新的 ApplicationContext。有人可以[让我知道如何在多个 Activiti 服务任务中使用同一个单例 Bean。
据我所知,您无法将 (@Autowire) bean 自动装配到 Java 委托中,因此为了访问您在应用程序配置中定义的 bean,您需要访问 applicationContext 并调用 getBean () 方法。
我通常会创建一个 ApplicationContext 提供程序 class 来简化此操作,因为您倾向于重复使用它。
这个 Whosebug post 展示了如何创建这样一个野兽:
Spring get current ApplicationContext