为什么我的 Spring Boot @Autowired MyBatis 静态映射器为空?
Why is my Spring Boot @Autowired MyBatis static mapper null?
我有以下 class,但是 Spring 和 MyBatis-Spring-Boot-Starter 不会自动装配我的映射器。
当我 运行 请求时,我从 println()
得到输出
sourceMapper = null
型号
public class Source {
@Autowired
public static SourceMapper sourceMapper; #### Why isn't this set?
public static Source findOrCreate(String url) {
...
System.out.println("sourceMapper = " + sourceMapper);
source = sourceMapper.findByHost(host);
...
}
}
我尽可能地遵循了示例。
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
主 @Controller
class 中处理请求的另一个 @Autowired
Mappers
工作,即使它们是私有的。
这是映射器class
package ...mapper;
@Mapper
public interface SourceMapper {
...
我 运行 使用新模型和映射器再次解决这个问题。我试着关注Why is my Spring @Autowired field null? and the code sample,但是还是 null!我尝试了 @Configurable
、@Service
、@Component
。
Model
@Configurable
public class Domain {
@Autowired
private static DomainMapper domainMapper;
public static void incrementCounter(String host) {
...
Domain d = getDomainMapper().find(host, thisMonth);
public static DomainMapper getDomainMapper() {
return domainMapper;
public static void setDomainMapper(DomainMapper domainMapper) {
Domain.domainMapper = domainMapper;
Mapper
@Mapper
public interface DomainMapper {
MyBatis 3.4.5,MyBatis Spring1.3.1,MyBatis Spring 引导自动配置 1.3.1,MyBatis Spring 引导启动器 1.3.1
Spring 只会在另一个 bean 需要时尝试为您注入一个 bean。
你的 class Source
只是一个带有一堆静态方法的普通 class。
因此它不受 Spring 的创作控制。
如果你想将SourceMapper
注入你的Source
,你应该用@Component
或@Service
标记Source
,这样容器就会知道它应该为你创建一个 Source
类型的 bean 并给你一个 SourceMapper
.
的实例
此外,SourceMapper
应该声明为非静态的,以防止 class 在注入之前访问变量。而静态字段只有使用字段setter注入才能注入。
我用
修复了它
private static DomainMapper getDomainMapper() {
//
if (domainMapper == null)
domainMapper = MyApplication.getApplicationContext().getBean(DomainMapper.class);
return domainMapper;
和
MyApplication
@Autowired // for AWS
private static ApplicationContext context;
// I believe this only runs during an embedded Tomcat with `mvn spring-boot:run`.
// I don't believe it runs when deploying to Tomcat on AWS.
public static void main(String[] args) {
context = SpringApplication.run(MyApplication.class, args);
但是我不喜欢!
我有以下 class,但是 Spring 和 MyBatis-Spring-Boot-Starter 不会自动装配我的映射器。
当我 运行 请求时,我从 println()
sourceMapper = null
型号
public class Source {
@Autowired
public static SourceMapper sourceMapper; #### Why isn't this set?
public static Source findOrCreate(String url) {
...
System.out.println("sourceMapper = " + sourceMapper);
source = sourceMapper.findByHost(host);
...
}
}
我尽可能地遵循了示例。
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
主 @Controller
class 中处理请求的另一个 @Autowired
Mappers
工作,即使它们是私有的。
这是映射器class
package ...mapper;
@Mapper
public interface SourceMapper {
...
我 运行 使用新模型和映射器再次解决这个问题。我试着关注Why is my Spring @Autowired field null? and the code sample,但是还是 null!我尝试了 @Configurable
、@Service
、@Component
。
Model
@Configurable
public class Domain {
@Autowired
private static DomainMapper domainMapper;
public static void incrementCounter(String host) {
...
Domain d = getDomainMapper().find(host, thisMonth);
public static DomainMapper getDomainMapper() {
return domainMapper;
public static void setDomainMapper(DomainMapper domainMapper) {
Domain.domainMapper = domainMapper;
Mapper
@Mapper
public interface DomainMapper {
MyBatis 3.4.5,MyBatis Spring1.3.1,MyBatis Spring 引导自动配置 1.3.1,MyBatis Spring 引导启动器 1.3.1
Spring 只会在另一个 bean 需要时尝试为您注入一个 bean。
你的 class Source
只是一个带有一堆静态方法的普通 class。
因此它不受 Spring 的创作控制。
如果你想将SourceMapper
注入你的Source
,你应该用@Component
或@Service
标记Source
,这样容器就会知道它应该为你创建一个 Source
类型的 bean 并给你一个 SourceMapper
.
此外,SourceMapper
应该声明为非静态的,以防止 class 在注入之前访问变量。而静态字段只有使用字段setter注入才能注入。
我用
修复了它private static DomainMapper getDomainMapper() {
//
if (domainMapper == null)
domainMapper = MyApplication.getApplicationContext().getBean(DomainMapper.class);
return domainMapper;
和
MyApplication
@Autowired // for AWS
private static ApplicationContext context;
// I believe this only runs during an embedded Tomcat with `mvn spring-boot:run`.
// I don't believe it runs when deploying to Tomcat on AWS.
public static void main(String[] args) {
context = SpringApplication.run(MyApplication.class, args);
但是我不喜欢!