4
I am setting up a project using Spring boot for ioc and Data-Jpa along with JSF however I have a problem with @Autowired
my DAO is not carrying.
Does anyone know how to do that configuration and where I might be missing?
Follow my setup:
Class Applicationconfig
@SpringBootApplication
@ComponentScan
public class ApplicationConfig extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ApplicationConfig.class);
}
@Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new FacesServlet();
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.xhtml");
return servletRegistrationBean;
}
}
Class loginMbController
@ManagedBean(name = "login")
@Component
public class LoginMBController {
@Autowired
private CustomerDao customerDao;
public void test() {
System.out.println("Hello");
System.out.println(customerDao);
}
}
The customerDao
public interface CustomerDao extends JpaRepository<Customer, Long>{
}
The application.properties
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Datasource
spring.datasource.jndi-name=java:jboss/datasources/mysqlDS
Login.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<H2>
<h:outputText value="Login Page" />
</H2>
<h:form>
<p:button value="ok" onclick="#{login.test()}" />
</h:form>
</h:body>
</html>
and my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>CarlosSnackBar</display-name>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
</web-app>
when I click the "ok" button on the console appears "hello null"
Does the injection work on any other component? Have you tried including base package or base package class in the Component scan to make sure DAO is in context? Another possible reason is that MB is only in JSF context, and not in Spring
– Bruno César
Opa vlw Bruno, yes I will need to inject other components and I tried to put explicitly in @componentScan the base package but it gave the same problem. and as such MB is not in the Spring context?
– Carlos Eduardo Proença Silva