Lucas,
consegui resolver o problema aqui.
O erro anterior estava acontecendo porque esqueci de incluir o listener do spring no web.xml, depois de incluído voltou a aparecer o erro:
No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2:
Após algumas debugadas percebi que o que estava faltando era uma anotação para indicar qual das implementações seria a primária. Fiz isso através da anotação @Primary.
Tive que mudar de ComponentFactory para FactoryBean. Também acabei trocando a anotação @Qualifer por @Named, mas isso não interfere nesse problema.
Vou postar aqui como ficou.
MysqlSessionFactory
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Named;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Primary
@Component
@SuppressWarnings("deprecation")
@Named("mysqlSessionFactory")
public class MysqlSessionFactoryCreator implements FactoryBean<SessionFactory> {
private SessionFactory factory;
@PostConstruct
public void create() {
factory = new AnnotationConfiguration().configure("hibernate.mysql.cfg.xml").buildSessionFactory();
}
@PreDestroy
public void destroy() {
factory.close();
}
@Override
public SessionFactory getObject() throws Exception {
return factory;
}
@Override
public Class<?> getObjectType() {
return SessionFactory.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
OracleSessionFactory
@Component
@SuppressWarnings("deprecation")
@Named("oracleSessionFactory")
public class OracleSessionFactoryCreator implements FactoryBean<SessionFactory> {
private SessionFactory factory;
@PostConstruct
public void create() {
factory = new AnnotationConfiguration().configure("hibernate.oracle.cfg.xml").buildSessionFactory();
}
@PreDestroy
public void destroy() {
factory.close();
}
@Override
public SessionFactory getObject() throws Exception {
return factory;
}
@Override
public Class<?> getObjectType() {
return SessionFactory.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
MysqlSessionCreator
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Named;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Primary
@Component
@Named("mysql")
@Scope(value="request")
public class MysqlSessionCreator implements FactoryBean<Session> {
@Autowired
@Named("mysqlSessionFactory")
private SessionFactory factory;
private Session session;
public MysqlSessionCreator(SessionFactory factory) {
this.factory = factory;
}
@PostConstruct
public void create() {
this.session = factory.openSession();
}
@PreDestroy
public void destroy() {
this.session.close();
}
@Override
public Session getObject() throws Exception {
return session;
}
@Override
public Class<?> getObjectType() {
return Session.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
OracleSessionCreator
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Named;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Named("oracle")
@Scope(value="request")
public class OracleSessionCreator implements FactoryBean<Session> {
@Autowired
@Named("oracleSessionFactory")
private final SessionFactory factory;
private Session session;
public OracleSessionCreator(SessionFactory factory) {
this.factory = factory;
}
@PostConstruct
public void create() {
this.session = factory.openSession();
}
@PreDestroy
public void destroy() {
this.session.close();
}
@Override
public Session getObject() throws Exception {
return session;
}
@Override
public Class<?> getObjectType() {
return Session.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
MysqlDao
import javax.inject.Named;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import br.com.caelum.vraptor.ioc.Component;
@Component
public class MysqlDao {
@Autowired
@Named("mysql")
private Session session;
public MysqlDao(Session session) {
this.session = session;
}
public void algumMetodo(){
return;
}
}
Configuração do listener web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="br.com.foo.hibernate"/>
<context:annotation-config/>
</beans>
Existem alguma possibilidade de futuramente o vraptor já trazer esse tipo de configuração?
Obrigado pela ajuda.