new不能执行autowired_@Autowired在可运行内部无法正常工作

I have a runnable task where i am trying to Autowire field but when i do it the task doesn't run .

When i Autowire the field outside the runnable it works fine . Why is this happening ? Also is there any other cleaner way to get new instances of a autowired field inside runnable other than Autowiring it inside ?

Here's my runnable method `

Runnable task = new Runnable() {

@Autowired

ICruxPanelClientService CruxPanelClientService;

public void run (){

CruxPanelClientService.setCruxWebServiceBaseURL("http://10.41.181.23:8080");

CronCruxModel m = new CronCruxModel();

m = model1.get(model_var);

System.out.println("EXECUTING");

System.out.println(m.getService_status() + " ---------EXEexecution");

System.out.println(m.getCat_name() + "Executing Name ");

// time = m.getService_time();

UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest();

CategoryModel categoryModel = new CategoryModel();

categoryModel.setColor(m.getCat_color());

categoryModel.setIcon(m.getCat_icon());

categoryModel.setIconWhite(m.getCat_icon_white());

categoryModel.setName(m.getCat_name());

categoryModel.setId(m.getCat_id());

categoryModel.setKey(m.getCat_catkey());

categoryModel.setIndexOrder(m.getCat_indexOrder());

updateCategoryRequest.setCategory(categoryModel);

CruxPanelClientService.updateCategory(updateCategoryRequest);

GetServiceDataIdByCategoryIdRequest request1 = new GetServiceDataIdByCategoryIdRequest();

request1.setId(m.getCat_id());

GetServiceDataIdByCategoryIdResponse response1 = CruxPanelClientService.getServiceDataIdByCategoryId(request1);

ArrayList service = new ArrayList();

service = response1.getServiceModels();

JSONArray json = new JSONArray();

if(m.getService_order_succ_msg()==null)

{

json = new JSONArray();

}

else {

json = new JSONArray(m.getService_order_succ_msg());

}

String message = m.getService_order_succ_msg();

for (int j=0;j

{

UpdateServiceMasterRequest req = new UpdateServiceMasterRequest();

ServiceModel s = new ServiceModel();

s=service.get(j);

;

JSONObject obj = new JSONObject();

if(json.length()==0 )

{

String ms = null;

s.setOrderSuccessMessage(ms);

req.setServiceModel(s);

}

else {

String message1 = json.get(j).toString();

if(message1.equals(null) || message1.equals("")) {

String ms = null;

s.setOrderSuccessMessage(ms);

req.setServiceModel(s);

}

else {

s.setOrderSuccessMessage(message1);

req.setServiceModel(s);

}

}

CruxPanelClientService.updateServiceMaster(req);

}

m.setService_status("executed");

UpdateCronCruxRequest q = new UpdateCronCruxRequest();

q.setCronCruxModel(m);

CruxPanelClientService.updateCronCrux(q);

}

};`

解决方案

The problem is spring doesn't control creation of your runnable. There are couple possible solutions:

Put you runnable creation in some service, repository, controller, component or what ever handled by spring:

Example:

@Service

public class SomeService {

@Autowired

private ICruxPanelClientService cruxPanelClientService;

public Runnable newRunnable() {

return new Runnable() {

public void run() {

cruxPanelClientService

}

}

}

}

Create runnable as bean with prototype scope

Example:

@Configuration

public class Runnableconfiguration {

@Bean

@Scope("prototype")

public Runnable newRunnbale(final ICruxPanelClientService cruxPanelClientService) {

return new Runnable() {

public void run() {

cruxPanelClientService

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Java 中,使用 @Transactional 注解可以实现事务管理。但是在某些情况下,@Transactional 注解可能不会生效,比如注解在事务类的内部方法上。 这是因为 Spring AOP 默认只拦截通过 Spring 容器管理的 Bean 的方法调用,而事务类通常是使用自调用的方式,因此不会被 Spring AOP 拦截。 解决这个问题的方法是使用 AspectJ。AspectJ 是一种基于语言的 AOP 实现方式,可以在编译期或者运行期织入切面。 具体实现如下: 1. 添加 AspectJ 依赖 ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> ``` 2. 配置 Spring,启用 AspectJ 自动代理 ```xml <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <aop:aspectj-autoproxy /> ``` 3. 在事务类上添加 @Aspect 注解,并实现一个切面类 ```java @Aspect @Component public class TransactionAspect { @Autowired private PlatformTransactionManager transactionManager; @Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)") public void transactionalMethod() {} @Around("transactionalMethod()") public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable { TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); Object result; try { result = joinPoint.proceed(); transactionManager.commit(status); } catch (Exception e) { transactionManager.rollback(status); throw e; } return result; } } ``` 这个切面类会拦截所有被 @Transactional 注解标记的方法,并使用事务管理器执行事务。 以上就是解决注解@Transactional事务类内调用不生效问题的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值