Struts2学习:Action使用@Autowired注入为null的解决方案
1、pom.xml引入struts2-spring-plugin
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.18</version>
</dependency>
2、struts.xml添加常量struts.objectFactory.spring.autoWire.alwaysRespect,使Spring的自动注入总是有效
<constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />
通过上述两个配置,即可在Action中实现自动注入。测试例子:
在application.properties配置:
destPath = application.properties
新建util文件夹,新建工具类StrutsConfig
import java.util.Properties;
@Component
public class StrutsConfig {
@Value("${destPath}")
private String destPath;
public String getDestPath() {
System.out.println(destPath);
return destPath;
}
}
新建action类UploadFileAction
package com.struts2demo.demo.action;
import com.struts2demo.demo.util.StrutsConfig;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
public class UploadFileAction {
@Autowired
private StrutsConfig strutsConfig;
public String excute() {
System.out.println(strutsConfig.getDestPath());
return "error";
}
}
struts.xml和jsp的编写就不写了,测试效果:

Struts2学习:Action使用@Autowired注入为null的解决方案的更多相关文章
- struts2的action中@Autowired注入为null的解决方案
今天遇到类似问题,记录下来以便以后查阅: @Aspect作用于action,致使action中的@Autowired注入为null的解决方案,以下三种任选一种: 1.去掉@Autowired,改用se ...
- Java 各级注解及@Autowired注入为null解决办法
1.@controller 控制器 用于标注控制层,相当于struts中的action层. 2.@service 服务层 用于标注服务层,主要用来进行业务的逻辑处理. 3.@repository DA ...
- 关于工具类中@Autowired注入为NULL的问题记录
记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. @Component //把普通pojo实例化到spring容器中 0 public clas ...
- 于工具类中@Autowired注入为NULL的问题记录
记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- @Autowired注入为null问题分析
题说明 最近看到Spring事务,在学习过程中遇到一个很苦恼问题 搭建好Spring的启动环境后出现了一点小问题 在启动时候却出现[java.lang.NullPointerException] 不过 ...
- spring@Autowired注入为null的问题,2017年9月14日21点41分记录
这个小问题纠结了三个小时..发出来留个纪念 这是启动项目的时候 这是请求控制器的时候 图1注入的时候是null,图2请求控制器的时候是有的,这是因为图1debug的地方是构造器..autowire ...
- Spring @Autowired 注入为 null
原因 配置缺失,比如为开启注解扫描驱动.注入组件为注册: 使用 new 关键字创建的对象不受spring容器管理,无法注入: 注入静态变量, 静态变量/类变量不是对象的属性,而是一个类的属性,spri ...
- struts2视频学习笔记 07-08(为Action的属性注入值,指定需要Struts 2处理的请求后缀,常用常量)
课时7 为Action的属性注入值(增加灵活性,适用于经常更改的参数) Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入 ...
- 【Struts2学习笔记(3)】至Action注入属性值
(1)有属性注入? 当一些属性不适合固定写入时适合使用这样的方法,对于一些使用特频繁的类或者方法,非常多类都会用到,那么使用属性注入会节省很多其它的力气.并且在设计的时候就能够提早的把该属性给定义出来 ...
随机推荐
- 3个问题:MySQL 中 character set 与 collation 的理解;utf8_general_ci 与 utf8_unicode_ci 区别;uft8mb4 默认collation:utf8mb4_0900_ai_ci 的含义
MySQL 中 character set 与 collation 的理解 出处:https://www.cnblogs.com/EasonJim/p/8128196.html 推荐: 编码使用 uf ...
- activemq jms使用
activemq 是 基于 jms 协议的 消息队列 消息 流程: jsm 的 消息流程鼻尖简单 生产者发送消息到目的地,消费者 监听这个目的地,然后收到消息. 相比 amqp 的 消息流程简单很多 ...
- Jenkins进阶-部署Web项目到远程tomcat(7)
之前讲到的是如何构建一个项目,并且将代码进行编译.打包,那么打包完成最后的结果就需要发布到应用服务器,将项目部署成功.在之前的项目中我们采用的shell脚本来部署,下面讲解通过Jenkins部署web ...
- less命令详解
Linux less命令 less 与 more 类似,但使用 less 可以随意浏览文件,而 more 仅能向前移动,却不能向后移动,而且 less 在查看之前不会加载整个文件 语法: less [ ...
- PHP用ActiveMq 实现消息列队
1.各种安装 2.简单配置: jetty.xml localhost:8161 配置: activemq添加stomp的61613接口 conf/activemq.xml <transportC ...
- Flutter at Google I/O 2018
Flutter at Google I/O 2018 如何观看 https://flutterchina.club/ 极速构建漂亮的原生应用 Flutter是谷歌的移动UI框架,可以快速在iOS和 ...
- PHP $a='abcdef';请取出$a的值并打印第一个字母
echo $a[0];echo $a{0};echo chr(ord($a));echo substr( $a, 0,1);
- ALGO-152_蓝桥杯_算法训练_8-2求完数
记: 掌握完数的概念 AC代码: #include <stdio.h> int main(void) { int i,j,sum; ; i <= ; i ++) { sum = ; ...
- 【linux】安装docker
硬件Centos6.9 x86_64 1.查看centos内核,uname -r 目前,CentOS 仅发行版本中的内核支持 Docker. Docker 运行在 CentOS 7 上,要求系统为64 ...
- C++11--Tuple类<tuple>
#include "stdafx.h" #include <iomanip> #include <condition_variable> #include ...