How do I call the method without starting the spring server in spring boot app

Viewed 220

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.

1 Answers

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 by application.properties without spring boot start up.

Life Cycle of Bean

Life Cyle of Bean

You can create a simple maven/Gradle project and connect jdbc like this.

import java.sql.*;  
class MysqlCon{  
public static void main(String args[]){  
try{  
  Class.forName("com.mysql.jdbc.Driver");  
  Connection con=DriverManager.getConnection(  
  "jdbc:mysql://localhost:3306/sonoo","root","root");  
  //here sonoo is database name, root is username and password  
  Statement stmt=con.createStatement();  
  ResultSet rs=stmt.executeQuery("select * from emp");  
  while(rs.next())  
  System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  
  "+rs.getString(3));  
  con.close();  
  }catch(Exception e){ System.out.println(e);}  
 }  
}  
Related