1

I'm using Hibernate, Spring and Primefaces (and Maven) and I'm trying to run

@PostConstruct
init() {}

to initialize a location list inside a bean. But the init() method is never called. The projects structure is:

com.xxx
com.xxx.hibernate.dao
com.xxx.hibernate.dao.impl
com.xxx.hibernate.data
com.xxx.prime.faces.bean
com.xxx.spring.service
com.xxx.spring.service.impl

application context:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<context:property-placeholder location="classpath*:META-INF/*.properties"/>
<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.xxx"></context:component-scan>

<!-- Create Data Source bean -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://host:3306/db" />
    <property name="username" value="user" />
    <property name="password" value="password" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property value="persistenceUnit" name="persistenceUnitName" />
    <property name="dataSource" ref="dataSource" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!-- Detect @Transactional Annotation -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

The LocationView Bean:

@ManagedBean
@ViewScoped
public class LocationView implements Serializable{

    private static final long serialVersionUID = 1L;

    @ManagedProperty("#{locationService}")
    private LocationService locationService;

    private Location location = new Location();

    private List<Location> locations = new ArrayList<Location>();

    @PostConstruct
    public void init() {
        this.locations = locationService.getAllLocations();
    }

I run this on Glassfish4 in debug mode and the init() Method is never called. but I don't no why. It should be scanned for spring annotations, but I don't know how I could be sure about that.

Also I'm not sure what

<context:property-placeholder location="classpath*:META-INF/*.properties"/>

does.

Any ideas what I could check?

user2158143
  • 123
  • 1
  • 2
  • 10
  • JSF is not managing beans in your case. It is the responsibility of Spring to manage as specified by `` in which case, JSF related annotations like `@ManagedBean`, `@ManagedProperty` will be ignored. The post construct method should nevertheless be invoked but these changes are necessary (you are supposed to use Spring related annotations correctly at first, if you want Spring to manage beans). – Tiny Nov 21 '15 at 16:47
  • Why should JSF not manage my beans? Another Bean with [@]ManagedBean, [@]essionScoped and [@]ManagedProperty("#{otherService}")) works just fine. (only [@]PostConstruct is missing) – user2158143 Nov 21 '15 at 17:30
  • Because they are delegated to Spring. Other beans using `@ManagedBean` and `@SessionScoped` will no longer be session scoped JSF beans as both annotations are simply ignored and the Spring default "singleton" scope comes into play i.e. those beans are like application scoped beans as default - they are Spring managed beans (in either case, `@ManagedProperty("#{otherService}")` in Spring managed beans can no longer work - it requires a Spring annotation `@Autowired`). – Tiny Nov 21 '15 at 17:46
  • Food for read: http://stackoverflow.com/questions/18387993/spring-jsf-integration-how-to-inject-a-spring-component-service-in-jsf-managed – BalusC Nov 21 '15 at 20:33
  • Ok so, basicaly I shouldn't mix jsf and spring annotations. except [@]ManagedProperty to inject a spring managed bean using the SpringBeanFacesELResolver. Is there a jsf equivalent for the spring PostConstruct annotation? @BalusC Thanks for the link! What I don't get: The last code example in the answere in your link is exactly what I try to achive. (except the autowired part). Why is this a working example and my code does not work? The spring component scan scans the bean class, is that the problem? – user2158143 Nov 22 '15 at 17:26
  • `@PostConstruct` is not from Spring. Look what package it's coming from. As to failing Spring part, sorry I don't do Spring. – BalusC Nov 22 '15 at 18:36
  • right... it's from javax. I forgot about that. I still don't know exactly what the reason is, postconstruct doesn't get called. – user2158143 Nov 22 '15 at 19:08
  • Also: now i only use javax annotations in my bean and . my beans are in com.xxx.prime so they should not be delegated to spring. but the javax annotations still don't work. – user2158143 Nov 22 '15 at 20:36

0 Answers0