当前位置: 动力学知识库 > 问答 > 编程问答 >

java - AspectJ causing sessionfactory null?

问题描述:

Whenever I use aspect with spring-hibernate project and put @EnableJAutoproxy, sessionfactory is null. Otherwise everthing works fine.

I get null pointer exception with sessionfactory. As soon as I remove it the app works but Aspect doesn't work .

Please help me !!!!

 @Aspect

@Component

public class AspectHandler {

@Pointcut("within(com.x.y..*)")

private void selectAll() {

}

@Before("selectAll()")

public void beforeAdvice() throws Throwable {

System.out.println("before method exceution");

}

@Around("selectAll()")

public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable{

System.out.println("firing aspect");

Object retVal = joinPoint.proceed();

System.out.println("retval" +retVal);

}


 @Configuration

@EnableWebMvc

@EnableTransactionManagement

> **@EnableAspectJAutoProxy**

@ComponentScan(basePackages = { "com.x.y.controller","com.x.y.resources.*.controller"})

public class DispatcherContext extends WebMvcConfigurerAdapter {

@Bean

@Autowired

public HibernateTransactionManager transactionManager(SessionFactory sessionFactory)

{

HibernateTransactionManager transactionManager = new HibernateTransactionManager();

transactionManager.setSessionFactory(sessionFactory);

return transactionManager;

}

@Bean

public LocalSessionFactoryBean getSessionFactory()throws PropertyVetoException

{

LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

sessionFactory.setDataSource(getDataSource());

sessionFactory.setHibernateProperties(getHibernateProperties());

sessionFactory.setPackagesToScan(new String[] { "com.x.y" });

return sessionFactory;

}

}


StudentDAOImpl

@Repository

public class StudentDAOImpl implements StudentDAO{

@Autowired

private SessionFactory sessionFactory;

@Override

public Student getStudent(int rollNo) {

String query = "from Student where rollNo = "+rollNo;

Session session = sessionFactory.getCurrentSession();

List<Student> students = session.createQuery(query).list();

if(students != null && students.size()==1)

{

return students.get(0);

}

return null;

}

@Override

public List<Student> getStudents() {

String query = "from Student";

Session session = sessionFactory.getCurrentSession();

List<Student> students = session.createQuery(query).list();

return students;

}

}


StackTrace :

HTTP ERROR 500

Server Error</pre></p><h3>Caused by:</h3><pre>java.lang.NullPointerException

at com.x.y.resources.school.dao.SchoolDAOImpl.getSchoolDetails(SchoolDAOImpl.java:30)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:483)

at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocati on.java:190)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 157)

at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedin gJoinPoint.java:85)

at com.x.y.config.aop.AspectHandler.aroundAdvice(AspectHandler.java:40)

at

分享给朋友:
您可能感兴趣的文章:
随机阅读: