1、pom.xml引入struts2-spring-plugin

  1. <dependency>
  2. <groupId>org.apache.struts</groupId>
  3. <artifactId>struts2-spring-plugin</artifactId>
  4. <version>2.5.18</version>
  5. </dependency>

2、struts.xml添加常量struts.objectFactory.spring.autoWire.alwaysRespect,使Spring的自动注入总是有效

  1. <constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />

通过上述两个配置,即可在Action中实现自动注入。测试例子:

在application.properties配置:

  1. destPath = application.properties

新建util文件夹,新建工具类StrutsConfig

  1. import java.util.Properties;
  2.  
  3. @Component
  4. public class StrutsConfig {
  5.  
  6. @Value("${destPath}")
  7. private String destPath;
  8.  
  9. public String getDestPath() {
  10. System.out.println(destPath);
  11. return destPath;
  12. }
  13.  
  14. }

新建action类UploadFileAction

  1. package com.struts2demo.demo.action;
  2.  
  3. import com.struts2demo.demo.util.StrutsConfig;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5.  
  6. import java.io.File;
  7.  
  8. public class UploadFileAction {
  9.  
  10. @Autowired
  11. private StrutsConfig strutsConfig;
  12.  
  13. public String excute() {
  14. System.out.println(strutsConfig.getDestPath());
  15. return "error";
  16. }
  17.  
  18. }

struts.xml和jsp的编写就不写了,测试效果:

Struts2学习:Action使用@Autowired注入为null的解决方案的更多相关文章

  1. struts2的action中@Autowired注入为null的解决方案

    今天遇到类似问题,记录下来以便以后查阅: @Aspect作用于action,致使action中的@Autowired注入为null的解决方案,以下三种任选一种: 1.去掉@Autowired,改用se ...

  2. Java 各级注解及@Autowired注入为null解决办法

    1.@controller 控制器 用于标注控制层,相当于struts中的action层. 2.@service 服务层 用于标注服务层,主要用来进行业务的逻辑处理. 3.@repository DA ...

  3. 关于工具类中@Autowired注入为NULL的问题记录

      记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. @Component //把普通pojo实例化到spring容器中 0 public clas ...

  4. 于工具类中@Autowired注入为NULL的问题记录

      记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  5. @Autowired注入为null问题分析

    题说明 最近看到Spring事务,在学习过程中遇到一个很苦恼问题 搭建好Spring的启动环境后出现了一点小问题 在启动时候却出现[java.lang.NullPointerException] 不过 ...

  6. spring@Autowired注入为null的问题,2017年9月14日21点41分记录

    这个小问题纠结了三个小时..发出来留个纪念 这是启动项目的时候 这是请求控制器的时候   图1注入的时候是null,图2请求控制器的时候是有的,这是因为图1debug的地方是构造器..autowire ...

  7. Spring @Autowired 注入为 null

    原因 配置缺失,比如为开启注解扫描驱动.注入组件为注册: 使用 new 关键字创建的对象不受spring容器管理,无法注入: 注入静态变量, 静态变量/类变量不是对象的属性,而是一个类的属性,spri ...

  8. struts2视频学习笔记 07-08(为Action的属性注入值,指定需要Struts 2处理的请求后缀,常用常量)

    课时7 为Action的属性注入值(增加灵活性,适用于经常更改的参数) Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入 ...

  9. 【Struts2学习笔记(3)】至Action注入属性值

    (1)有属性注入? 当一些属性不适合固定写入时适合使用这样的方法,对于一些使用特频繁的类或者方法,非常多类都会用到,那么使用属性注入会节省很多其它的力气.并且在设计的时候就能够提早的把该属性给定义出来 ...

随机推荐

  1. LOJ 2339 「WC2018」通道——边分治+虚树

    题目:https://loj.ac/problem/2339 两棵树的话,可以用 CTSC2018 暴力写挂的方法,边分治+虚树.O(nlogn). 考虑怎么在这个方法上再加一棵树.发现很难弄. 看了 ...

  2. JQuery 240中插件

    http://www.cnblogs.com/Terrylee/archive/2007/12/09/the-ultimate-jquery-plugin-list.html

  3. Remote error: VAR and OUT arguments must match parameter type exactly'

    在XE10中 downloadfile(filename: string; out FileStream: TStream; out FileSize: int64)是没有问题的,升级到delphi ...

  4. RedHat6.5安装单机flume1.6

    版本号: RedHat6.5   JDK1.8   apache-flume-1.6.0 1.apache-flume-1.6.0-bin.tar.gz 下载 官网下载地址:http://archiv ...

  5. 【java】浅谈while 和do-while

    while语法格式:while(布尔表达式){//语句}先判断布尔表达式,如果为true就会执行循环体中的语句,然后再判断布尔表达式,一直到布尔表达式为false,然后循环结束.通常用算术运算符(++ ...

  6. Micro QR 和QR码

    QRcode.com 什么是QR码 QR码的种类 如何导入 QR码的成功之路 常见问题解答 咨询 Language   Micro QR码 返回 QR码的种类 首页 二维码上只有一个定位图案,这就是M ...

  7. ELK Stack 笔记

    ELK Stack ELK Stack ELK Stack ELK 介绍 架构 Elasticsearch 安装 常见问题 关闭 Elasticsearch Elasticsearch-head Ki ...

  8. 让SQL SERVER自动清理掉处于SLEEPING状态超过30分钟的进程(转)

    原文地址:http://www.itpub.net/thread-809758-1-1.html use master go ) drop procedure [dbo].[p_killspid] G ...

  9. 【Spring学习笔记-3】国际化支持

    [Spring]国际化支持 一.总体结构: 两个国际化资源中的内容: 二.程序 2.1  配置Spring上下文 beans.xml文件 <?xml version="1.0" ...

  10. bzoj5045: 打砖块

    Description 小Q最近沉迷于一款新型<打砖块>游戏.在每局游戏中,呈现在屏幕上的是一堵无限大小的墙壁.墙壁上镶嵌着 无数长度为2.宽度为1的砖块.墙壁被分成若干行,每行宽度都为1 ...