0

I am trying to set up a Spring environment but I can't properly inject/autowire anything because it always returns null.

I am NOT using any xml, so my configuration is as follow (nothing related to Spring on web.xml as well):

public class SpringConfig implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.scan("br.com.cemiterio");

    // Manage the lifecycle of the root application context
    container.addListener(new ContextLoaderListener(rootContext));

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
}
}

Then this class is called withing another package:

@Configuration
@ComponentScan("br.com.xxx")
@EnableTransactionManagement
public class FinanceiroBeanConfig {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws IOException, NamingException {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource());
    emf.setPersistenceProviderClass(HibernatePersistenceProvider.class);
    emf.setPackagesToScan("br.com.xxx.financeiro.model.bean");
    emf.setJpaProperties(readJpaProperties());
    emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    return emf;
}

private Properties readJpaProperties() throws IOException {
    Properties prop = new Properties();
    prop.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
    prop.setProperty("hibernate.hbm2ddl.auto", "update");
    prop.setProperty("hibernate.show_sql", "false");
    prop.setProperty("hibernate.format_sql", "false");
    return prop;
}

@Bean
public DataSource dataSource() throws IOException, NamingException {
    JndiTemplate template = new JndiTemplate();
    return (DataSource) template.lookup("java:comp/env/jdbc/financeiroDS");
}

@Bean
public LoadTimeWeaver loadTimeWeaver() {
    return new InstrumentationLoadTimeWeaver();
}

@Bean
public PlatformTransactionManager transactionManager() throws IOException, NamingException {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}

@Bean
public UsuarioService usuarioService() {
    return new UsuarioService();
}
}

My Service:

@Service
@Transactional
public class UsuarioService {

@PersistenceContext
private EntityManager em;

public Usuario salvar(Usuario usuario) {
    return em.merge(usuario);
}
}

Now when I try to inject it, it is always null:

@ManagedBean
@SessionScoped
public class ExemploBean {

private String nome;

@Autowired
private UsuarioService usuarioService;

public void salvar() {
    Usuario usuario = new Usuario();
    usuario.setNome(nome);
    usuarioService.salvar(usuario);
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

}

Am I missing something?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
dambros
  • 4,252
  • 1
  • 23
  • 39

0 Answers0