【问题标题】:Problem with Spring auto wired declarationSpring自动连线声明的问题
【发布时间】:2018-09-01 20:33:53
【问题描述】:

我正在尝试按照 Petri Kainulainen 的“Spring Data”一书中的示例代码创建一个应用程序。我有一个服务 RepositoryContactService 包 com.packtpub.springdata.jpa.service;

@Service("service")
public class RepositoryContactService implements ContactService {

我的ApplicationContext类设置扫描服务的包

@Configuration
@ComponentScan(basePackages = { "com.packtpub.springdata.jpa.service" })
@EnableTransactionManagement
@EnableWebMvc
@EnableJpaRepositories("com.packtpub.springdata.jpa.repository")
@PropertySource("classpath:application.properties")
public class ApplicationContext extends WebMvcConfigurerAdapter {

我正在运行带有声明的类测试

@Autowired
private static RepositoryContactService service;

和main方法中的代码

Contact contact = new Contact("handro1104@gmail.com", "handro");
service.save(contact);

问题在于“service.save(contact);”行给我服务空。

【问题讨论】:

  • 请显示测试类的声明和所有注释。
  • Spring 不会 @Autowire 静态变量 - 所以你的整体设置是无效的。您需要创建一个包含 RepositoryContactService 的类,并且该 main 将通过 Spring 的应用程序上下文调用,以便 Spring 能够帮助您进行设置。

标签: java spring autowired


【解决方案1】:

从注解@Service 的类中只创建一个bean,因为@Service 的默认方式是Singleton,因此您不需要静态自动连接这些类bean。

变化:

@Autowired
private static RepositoryContactService service;

收件人:

@Autowired
private RepositoryContactService service;

【讨论】:

    【解决方案2】:

    感谢所有回复的人。我发现您无法自动装配静态字段并尝试了

    @Component
    public class Test {
        @Autowired
        private ContactService service;
    
        public static void main(String[] args) {
            Test test = new Test();
            Contact contact = new Contact("handro1104@gmail.com", "handro");
            ContactService service = test.service;
            service.save(contact);
        }
    

    但这也没有用。我也试过了

    public class Test {
        public static void main(String[] args) {
            ContactService service = new RepositoryContactService();
            Contact contact = new Contact("handro1104@gmail.com", "handro");
            service.save(contact);
        }
    

        @Resource // Also tried with @Autowired
        private ContactRepository repository;
    @Configurable
    public class RepositoryContactService implements ContactService {
        @Override
        public void save(Contact updated) {
            repository.save(updated);
        }
    

    但是这里的存储库是空的。

    为了更清楚,我有

    @Configuration
    @ComponentScan(basePackages = { "com.packtpub.springdata.jpa.service" })
    @EnableTransactionManagement
    @EnableWebMvc
    @EnableJpaRepositories("com.packtpub.springdata.jpa.repository")
    @PropertySource("classpath:application.properties")
    public class ApplicationContext extends WebMvcConfigurerAdapter {
    

    这样就可以扫描 RepositoryContactService 并且可以自动装配 com.packtpub.springdata.jpa.repository 中的 ContactRepository。在某些时候,我什至将 com.packtpub.springdata.jpa.repository 添加到 @ComponentScan。

    【讨论】:

      【解决方案3】:

      Spring 无法自动装配 RepositoryContactService 的原因有很多。

      1. RepositoryContactService 不在@ComponentScan 中声明的包中。为此,请尝试添加 RepositoryContactService 所在的包 并且 ContactService 出现在 @ComponentScan 的列表中。
      2. 你写过你写过Test类。如果它是一个单元测试类,则检查所有用于单元测试的注释是否存在。

      虽然这不能解决 null 问题,但我更喜欢对接口进行编程并使用 Qualifier 告诉 Spring 容器要注入的接口实现。

      【讨论】:

        猜你喜欢
        • 2021-08-03
        • 1970-01-01
        • 2017-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多