如何在Spring Boot应用程序中不启动Spring服务器的情况下调用该方法
我创建了一个 Spring Boot 项目,但后来意识到我不想在服务器上运行该应用程序,只想在 main 方法中实现我的逻辑。 到目前为止我已经完成了,但我似乎无法弄清楚我错过了什么。
我有一个 MainLogic.java 类
@Configuration
public class MainLogic {
JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private String sqlQuery = "";
public String getID(@RequestParam String id, @RequestParam(required=false) String date){
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat.setLenient(false);
String path = "";
sqlQuery= "SELECT i.abc_id as id"
+ "FROM ABCDetail i \n"
+ "WHERE i.abc_id = "+id+"\n"
}
#46: List<ABC> ABCdetails = jdbcTemplate.query(sqlQuery, new BeanPropertyRowMapper<ABC>(ABC.class));
ABCdetails.toString();
final StringBuilder textData = new StringBuilder();
final String headers = "ID";
textData.append(headers).append("\n");
for (final ABC abcDetail : ABCdetails) {
final String line = ((abcDetail.getID()==null)?"": abcDetail.getID()) + "\t" + ((abcDetail.getEXT_TYPE()==null)?"";
textData.append(line).append("\n");
}
try {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File tempFile = File.createTempFile("output", ".tmp", tempDir);
FileWriter fileWriter = new FileWriter(tempFile, true);
System.out.println(tempFile.getAbsolutePath());
path = tempFile.getAbsolutePath();
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(textData.toString());
bw.close();
}catch (Exception e) {
// TODO: handle exception
}
return path;
}
和我的主类,
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(MainLogic.class);
MainLogic ml = ctx.getBean(MainLogic.class);
String newPath = "";
if(args.length > 0) {
try {
System.out.println("Passing the argument to the controller: "+ ml.getID(args[0], args[1]));
}
我在第 46 行 MainLogic 中遇到空指针异常,有人可以帮助我吗? 或者有人可以建议我更好地做到这一点吗? 我是JAVA新手。
I created a spring boot project, but then realized I dont want to run the app on the server, just wanted to implement my logic in the main method.
I have done so far, and I cant seem to figure out what am I missing.
I have a class MainLogic.java
@Configuration
public class MainLogic {
JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private String sqlQuery = "";
public String getID(@RequestParam String id, @RequestParam(required=false) String date){
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat.setLenient(false);
String path = "";
sqlQuery= "SELECT i.abc_id as id"
+ "FROM ABCDetail i \n"
+ "WHERE i.abc_id = "+id+"\n"
}
#46: List<ABC> ABCdetails = jdbcTemplate.query(sqlQuery, new BeanPropertyRowMapper<ABC>(ABC.class));
ABCdetails.toString();
final StringBuilder textData = new StringBuilder();
final String headers = "ID";
textData.append(headers).append("\n");
for (final ABC abcDetail : ABCdetails) {
final String line = ((abcDetail.getID()==null)?"": abcDetail.getID()) + "\t" + ((abcDetail.getEXT_TYPE()==null)?"";
textData.append(line).append("\n");
}
try {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File tempFile = File.createTempFile("output", ".tmp", tempDir);
FileWriter fileWriter = new FileWriter(tempFile, true);
System.out.println(tempFile.getAbsolutePath());
path = tempFile.getAbsolutePath();
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(textData.toString());
bw.close();
}catch (Exception e) {
// TODO: handle exception
}
return path;
}
and my main class
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(MainLogic.class);
MainLogic ml = ctx.getBean(MainLogic.class);
String newPath = "";
if(args.length > 0) {
try {
System.out.println("Passing the argument to the controller: "+ ml.getID(args[0], args[1]));
}
I am getting a null pointer exception in MainLogic at line #46, can someone please help me.
or can anyone suggest me a better to do this?
I am new to JAVA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring boot bean有一个生命周期过程。如果不启动 Spring Boot,您将获得依赖项的空指针。如
JdbcTemplate jdbcTemplate;
你必须手动创建bean。如果没有 Spring Boot 启动,它不会被application.properties
配置。Bean 的生命周期
您可以创建一个简单的
maven/Gradle
项目并像这样连接 jdbc。Spring boot bean has a life cycle process. Without starting spring boot, you will get null pointer for your dependencies. Such as
JdbcTemplate jdbcTemplate;
You have to create the bean manually. It won't be configured byapplication.properties
without spring boot start up.Life Cycle of Bean
You can create a simple
maven/Gradle
project and connect jdbc like this.