Java 使用@autowired to DI后获取空对象
我有一个utils类,它使用@Autowired使用springbootstarter数据jpa注入存储库。但是当我使用这个存储库访问数据库时,它说存储库为空。我在控制器中使用了相同的方法,效果很好。这是我的Utils.classJava 使用@autowired to DI后获取空对象,java,spring,dependency-injection,spring-data-jpa,Java,Spring,Dependency Injection,Spring Data Jpa,我有一个utils类,它使用@Autowired使用springbootstarter数据jpa注入存储库。但是当我使用这个存储库访问数据库时,它说存储库为空。我在控制器中使用了相同的方法,效果很好。这是我的Utils.class package com.example.controller; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.
package com.example.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.example.dao.RuleRepository;
import com.example.model.Project;
import com.example.model.Rule;
public class Judge {
@Autowired
RuleRepository ruleRepository;
public boolean ageJudge(Project project) {
try {
if (ruleRepository == null)
{
System.out.println("yes");
}else {
System.out.println("false");
}
return false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
这是我的应用程序。java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.model.Project;
import com.example.model.Rule;
public interface RuleRepository extends JpaRepository<Rule, Integer>{
Rule findById(Integer id);
Rule findByRuleName(String ruleName);
}
这是RuleRepository.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.model.Project;
import com.example.model.Rule;
public interface RuleRepository extends JpaRepository<Rule, Integer>{
Rule findById(Integer id);
Rule findByRuleName(String ruleName);
}
package com.example.dao;
导入org.springframework.data.jpa.repository.JpaRepository;
导入com.example.model.Project;
导入com.example.model.Rule;
公共接口规则库扩展了JpaRepository{
规则findById(整数id);
规则findByRuleName(字符串ruleName);
}
它是目录。
RuleRepository在controller中运行良好。那么,问题出在哪里呢?您的util类
Judge
是一个普通的POJO而不是SpringBean,您只能将SpringBean注入到另一个SpringBean而不是普通的POJO中
如果您希望在Judge
中使用ruleRepository
bean,请使用@component
注释将其作为Spring组件:
@Component
public class Judge {
@Autowired
RuleRepository ruleRepository;
..............................
}
用户
@Service
对Judge
类的注释充当业务逻辑实现类。您的util类Judge
是一个普通POJO而不是Spring bean,您只能在另一个Spring bean而不是普通POJO中注入Spring bean
如果您希望在Judge
中使用ruleRepository
bean,请使用@component
注释将其作为Spring组件:
@Component
public class Judge {
@Autowired
RuleRepository ruleRepository;
..............................
}
用户
@Service
注释Judge
类充当业务逻辑实现类。您的Judge
应该注释为@Component
@Component
public class Judge{
// ...
}
因此Spring将实例化一个Judge
bean,它将可用于注入。然后,您可以在任何托管bean(例如:控制器)中使用该判断bean
但如果你实例化你自己,像这样:
Judge judge2 = new Judge();
您的存储库将为空,因为Spring与judge2对象无关,它不是由Spring管理的。您的
Judge
应该在@Component中注释
@Component
public class Judge{
// ...
}
因此Spring将实例化一个Judge
bean,它将可用于注入。然后,您可以在任何托管bean(例如:控制器)中使用该判断bean
但如果你实例化你自己,像这样:
Judge judge2 = new Judge();
您的存储库将为空,因为Spring与judge2对象无关,它不是由Spring管理的。您需要使您的
Judge
类至少成为项目的@组件,这将使您的类由Spring管理,因此您的RuleRepository
将被实例化
如果第一次尝试时它不起作用,您将不得不在要扫描的包列表中添加您的com.example.controller
包,在@ComponentScan注释中,您需要让您的判断类至少是项目的@Component
,这将使您的类由Spring管理,因此,您的规则存储库将被实例化
如果第一次尝试时它不起作用,您必须首先将您的com.example.controller
包添加到要扫描的包列表中,添加到@ComponentScan注释中,因为每个人都提到您的类法官没有@Component
注释
另一件事是,也许我的弹簧有点生锈了。
但据我记忆所及,我认为您的存储库还需要有@Component
或@repository
注释首先,因为每个人都提到您的类法官没有@Component
注释
另一件事是,也许我的弹簧有点生锈了。
但据我记忆所及,我认为您的存储库还需要有@组件
或@存储库
注释我看不出判断
在哪里实例化,但它看起来不像spring管理的bean,因此,如果你问一个问题,自动连线将不起作用,请始终添加堆栈。我看不到Judge
的实例化位置,但它看起来不像spring管理的bean,因此自动连线将不起作用。如果你问一个问题,请始终添加堆栈。是的,我只使用Judge judge2=new Judge()代码>以创建Judge对象。关于@Controller的闲聊之后。我发现它已经扩展了组件,所以它们在我的控制器中工作得很好代码>以创建Judge对象。关于@Controller的闲聊之后。我发现它已经扩展了组件,所以它们在我的控制器中工作得很好。