每当我在spring-hibernate项目中使用aspect并放入@EnableJAutoproxy时,sessionfactory为null。否则,外翻工作正常。
我用sessionfactory获取空指针异常。只要我删除它,应用程序就可以工作,但Aspect不起作用。
请帮帮我!!!!
@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