编程语言
首页 > 编程语言> > java-如何为独立应用程序调用存储库方法?

java-如何为独立应用程序调用存储库方法?

作者:互联网

我有以下扩展JPA Repositroy的存储库,也有一个实现类,在其中我已自动连接了它.

@Repository
public interface ProjectDAO extends CrudRepository<Project, Integer> {}

@Service
public class ProjectServiceImpl {

@Autowired private ProjectDAO pDAO;

public void save(Project p) { pDAO.save(p); } }

现在我有一个Application.java类

Class Application{
public static void main(String..s){
// I need a way to call a method of repository
}
}

配置文件

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
@PropertySource("file:/Users/abc/Documents/application.properties")
public class PersistenceContext {
    @Autowired
    Environment environment;

因此,在不使用任何基于Web的控制器的情况下,我们如何称呼它为main?

解决方法:

这是一种方法:

class Application {
    public static void main(String[] s){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersistenceContext.class);
        ProjectDAO dao = applicationContext.getBean(ProjectDAO.class);
    }
}

编辑:

class Application {
    public static void main(String[] s){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersistenceContext.class);
        ProjectServiceImpl service = applicationContext.getBean(ProjectServiceImpl.class);
    }
}

标签:spring-data-jpa,spring,java
来源: https://codeday.me/bug/20191111/2017553.html