Помощь! Я использую Spring + Hibernate, и я пытаюсь использовать АОП как это:Spring Hibernate + AOP
package ua.i.pustovalov.table;
enter code here
@Aspect
public class Aoprad {
@Pointcut("execution(* *getAll(..))")
public void performance() {
}
public Aoprad() {
}
@Around("performance()")
public void AfterAndPriv(ProceedingJoinPoint joinPoint) {
try {
System.out.println("Open query");
joinPoint.proceed();
System.out.println("Close query");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
Когда я пытаюсь использовать мой метод, он возвращает нуль.
public class TestDao {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" }, true);
DaoInterface das = (DaoInterface) context.getBean("dataDao");
System.out.println(das.getAll());
}
<context:annotation-config />
<aop:aspectj-autoproxy />
<context:component-scan base-package="ua.i.pustovalov.*">
</context:component-scan>
<bean id="audience" class="ua.i.pustovalov.table.Aoprad" />
<aop:config>
<aop:pointcut id="myPointcut"
expression="execution(* ua.i.pustovalov.maven.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- <tx:method name="get*" propagation="REQUIRED" read-only="true" /> -->
<tx:method name="getAll*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/homebase</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
</bean>
<bean id="dataDao" class="ua.i.putsovalov.dao.DaoStudent">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Мой DAO класс
package ua.i.putsovalov.dao;
public class DaoStudent extends HibernateDaoSupport implements DaoInterface {
public DaoStudent() {
}
@Override
protected HibernateTemplate createHibernateTemplate(
SessionFactory sessionFactory) {
HibernateTemplate result = super
.createHibernateTemplate(sessionFactory);
result.setAllowCreate(true);
return result;
}
@Override
public Student get(int id) {
return (Student) getSession().get(Student.class, id);
}
@Override
public Student get(String text) {
return (Student) getSession().get(Student.class, text);
}
@SuppressWarnings("unchecked")
@Override
public Collection<Student> getAll() {
return getSession().createCriteria(Student.class).list();
}
}
И интерфейс
public interface DaoInterface {
Student get(int id);
Student get(String text);
Collection<Student> getAll();
}
Какие шаги можно предпринять, чтобы отладить эту проблему?
Добро пожаловать в переполнение стека! Неясно, в чем проблема, которую вы пытаетесь решить. 'das.getAll()' возвращает 'null'? Любые ошибки? –
Любая ошибка! А имеют <- Открыть quary Hibernate: выберите this_.id в id0_0_, this_.mark как mark0_0_, this_.name как name0_0_, this_.surname как surname0_0_ от listofstudent this_ Закрыть quary нуль -> –
Ошибка говорит, что функция getAll() в классе DaoStudent возвращает значение null, проверка данных есть в таблице liststudent. – Basavaraj