简体   繁体   中英

Can't connect with MongoDB

i have some problem with Java project, using Spring and MongoDB, code below.

I tried connect with database by jiraConfig.properties using jira.path and spring.data.mongodb.port, but it doesn't work and i can't find solution. Did i forgot about something?

public class JiraProjectBriefControllerImpl implements JiraProjectBriefController {



  @Autowired
    JiraProjectBriefRepository jiraProjectBriefRepository;

    @Override
    public void update() {
        JiraController jiraController = new JiraControllerImpl();

        List<ProjectBrief> projectBriefs = jiraController.getAllProjectsBrief();

         jiraProjectBriefRepository.save(projectBriefs);
    }

    @Override
    public List<ProjectBrief> getProjectsBrief() {
        return jiraProjectBriefRepository.findAll();
    }

    @Override
    public ProjectBrief findById(int id) {
        return jiraProjectBriefRepository.findById(id);
    }}

// second class

@Configuration
@EnableAutoConfiguration
public interface JiraIntegrationService {

    static void main(String[] args) {

        SpringApplication.run(JiraIntegrationService.class, args);

        JiraProjectBriefController jiraProjectBriefController = new JiraProjectBriefControllerImpl();

        jiraProjectBriefController.update();

        System.out.print(jiraProjectBriefController.getProjectsBrief());
    }

}

// Errors

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at pl.ie.service.JiraProjectBriefControllerImpl.update(JiraProjectBriefControllerImpl.java:23)
    at pl.ie.JiraIntegrationService.main(JiraIntegrationService.java:26)
    ... 6 more

When you call new on an object, like you do in your main, that means Spring is out of the picture. The creation of the object and satisfying its dependencies are up to you.

You should not be calling new to create that object. Better to instantiate the Spring Bean factory and ask it to give you the instance you need with all its dependencies wired in.

This is a common misunderstanding by new Spring users. They call new and cannot understand why their Spring dependencies aren't wired.

jiraProjectBriefRepository未初始化。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM