为什么会报空指针异常呢
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | <? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.wyh</ groupId > < artifactId >asperctJJ</ artifactId > < version >1.0-SNAPSHOT</ version > < packaging >war</ packaging > < name >asperctJJ Maven Webapp</ name > <!-- FIXME change it to the project's website --> < url >http://www.example.com</ url > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >1.7</ maven.compiler.source > < maven.compiler.target >1.7</ maven.compiler.target > </ properties > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-core</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-beans</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-expression</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > < dependency > < groupId >aopalliance</ groupId > < artifactId >aopalliance</ artifactId > < version >1.0</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-aop</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > < dependency > < groupId >org.aspectj</ groupId > < artifactId >aspectjweaver</ artifactId > < version >1.8.9</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-aspects</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-test</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > </ dependencies > < build > < finalName >asperctJJ</ finalName > < pluginManagement > <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> < plugins > < plugin > < artifactId >maven-clean-plugin</ artifactId > < version >3.0.0</ version > </ plugin > <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> < plugin > < artifactId >maven-resources-plugin</ artifactId > < version >3.0.2</ version > </ plugin > < plugin > < artifactId >maven-compiler-plugin</ artifactId > < version >3.7.0</ version > </ plugin > < plugin > < artifactId >maven-surefire-plugin</ artifactId > < version >2.20.1</ version > </ plugin > < plugin > < artifactId >maven-war-plugin</ artifactId > < version >3.2.0</ version > </ plugin > < plugin > < artifactId >maven-install-plugin</ artifactId > < version >2.5.2</ version > </ plugin > < plugin > < artifactId >maven-deploy-plugin</ artifactId > < version >2.8.2</ version > </ plugin > </ plugins > </ pluginManagement > </ build > </ project > |
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <? xml version = "1.0" encoding = "UTF-8" ?> < 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" 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"> <!--开启aspectJ的自动代理--> < aop:aspectj-autoproxy /> <!--目标类--> < bean id = "productDao" class = "com.wyh.aspectJ.demo1.ProductDao" /> <!--定义切面--> < bean class = "com.wyh.aspectJ.demo1.MyAspectAnno" /> </ beans > |
Spring.demo1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.wyh.aspectJ.demo1; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith (SpringJUnit4ClassRunner. class ) @ContextConfiguration ( "classpath:applicationContext.xml" ) public class SpringDemo1 { @Resource (name= "productDao" ) private ProductDao productDao; @Test public void demo1(){ productDao.update(); productDao.delete(); productDao.findOne(); productDao.save(); } } |
ProductDao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.wyh.aspectJ.demo1; public class ProductDao { public void save(){ System.out.println( "保存商品。。。" ); } public void update(){ System.out.println( "更新商品。。。" ); } public void delete(){ System.out.println( "删除商品。。" ); } public void findOne(){ System.out.println( "查询一个商品。" ); } } |
MyAspectAnno
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.wyh.aspectJ.demo1; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; //注解方式 //切面类 @Aspect public class MyAspectAnno { @Before (value = "execution(* com.wyh.aspectJ.demo1.ProductDao.*(..))" ) public void before(){ System.out.println( "前置通知" ); } } |
51
收起
正在回答
4回答
我用你的代码替换老师代码相关的内容,是可以运行的,你看一下错误提示中发生空指针异常的SpringDemo1的19行是哪一行代码
祝学习愉快!
SSM主流框架入门与综合项目实战2018版
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧