Skip to content

Instantly share code, notes, and snippets.

@hseritt
Created June 28, 2016 12:15
Show Gist options
  • Save hseritt/7f9d28cc279bf56fef66cfe49502afb2 to your computer and use it in GitHub Desktop.
Save hseritt/7f9d28cc279bf56fef66cfe49502afb2 to your computer and use it in GitHub Desktop.
Calling a bean that uses JPA Repository
import java.io.IOException;
import java.sql.SQLException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class AlfmonitorApplication {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException, InterruptedException {
ConfigurableApplicationContext app = SpringApplication.run(AlfmonitorApplication.class, args);
Controller agentController = (Controller) app.getBean("controller");
agentController.run();
}
}
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Controller implements Runnable, InitializingBean {
private static Logger logger = Logger.getLogger(Controller.class);
@Autowired
private AgentRepository agentRepository;
public void run() {
logger.info("Starting subsystem AgentController");
List<Agent> activeAgents = agentRepository.findByActive(true);
for (Agent agent : activeAgents) {
logger.info(agent.getName());
}
logger.info("Agent controller finished.");
}
@Override
public void afterPropertiesSet() throws Exception {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment