Spring系列之新注解配置+Spring集成junit+注解注入

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"你本来要写一段很长的代码来构造一个Beam对象,但是如果使用注解的话只要使用一个注解符号即可"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200921212449632.png#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4a/4afa474a01541dbbbce1388e95017f3c.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200921212449632.png#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"下面我们来讲讲一些经常使用的注解符号"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@Component 使用类上用于实例化Bean"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@Controller 使用web层类上用于实例化Bean"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@Service 使用在Service层类上用于实例化service"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@Repository 使用在dao层类上用于实例化Bean"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@Autorwired 使用在字段上用于根据类型依赖注入"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@Qualifier 结合结合@Autowired一起使用根据名称进行依赖注入"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@Resource 相当于@Autowired+@Qualifier一起使用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@Scope 标注bean的范围"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@PostConstruct 使用该在方法上标注该方法是Bean的初始化方法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@PostDestory 使用在方法上标注该方法是Bean的销毁方法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"这三个没有什么区别,不过是为了在后期读代码的时候更加利于我们区分,比如看到@Controller就知道这是web层,看到@Service就知道这是service层"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@Component 使用类上用于实例化Bean\n@Controller 使用web层类上用于实例化Bean\n@Service 使用在Service层类上用于实例化service\n@Repository 使用在dao层类上用于实例化Bean\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"所以我们只需讲一个即可,其他的使用方法均相同,这里我们使用@Component来讲解"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"定义一个userDaolmp类"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.dao;\nimport com.pjh.dao.Imp.userdao;\nimport org.springframework.stereotype.Component;\n@Component(\"userDaoImp\")\npublic class userDaoImp implements userdao {\n public void save() {\n System.out.println(\"save\");\n }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"定义一个测试类测试"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@Test\n public void test(){\n ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(\"applicationContext.xml\");\n userdao userDaoImp =(userdao) classPathXmlApplicationContext.getBean(\"userDaoImp\");\n userDaoImp.save();\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"然后我们就发现了如下报错"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由蓝色的划线部分我们可以看出问题是:没有一个名叫userDaoImp的bean"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922081316893.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2f/2f391ac247af5b2de0cc415a967754b9.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922081316893.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"这是为什呢?因为我们虽然写了注解,但是我们要让applicationContext.xml知道我们使用了注解,要告诉他哪里有个bean。所以我们在applicationContext中还需要加入以下配置"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"命名空间"},{"type":"text","text":":xmlns:context=\""},{"type":"link","attrs":{"href":"http://www.springframework.org/schema/context","title":null},"content":[{"type":"text","text":"http://www.springframework.org/schema/context"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"约束路径"},{"type":"text","text":":"},{"type":"link","attrs":{"href":"http://www.springframework.org/schema/context","title":null},"content":[{"type":"text","text":"http://www.springframework.org/schema/context"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"http://www.springframework.org/schema/context/spring-context.xsd","title":null},"content":[{"type":"text","text":"http://www.springframework.org/schema/context/spring-context.xsd"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"注解的组件扫描:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"成功运行"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922082045847.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9d/9d269cf91fcc828673e3be4e4df46dd3.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922082045847.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"2"},{"type":"text","text":"|"},{"type":"text","marks":[{"type":"italic"}],"text":"0"},{"type":"text","text":"接下来我们再来讲讲如何使用注解进行注入"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"方式一:使用@Autowired+@Qualifier"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"目的"},{"type":"text","text":":创建一个userdao类将其注入带service类中"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"userdao类代码"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.dao;\nimport com.pjh.dao.Imp.userdao;\nimport org.springframework.stereotype.Component;\n@Component(\"userDaoImp\")\npublic class userDaoImp implements userdao {\n public void save() {\n System.out.println(\"save\");\n }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"service类代码"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.service.Imp;\nimport com.pjh.dao.Imp.userdao;\nimport com.pjh.dao.userDaoImp;\nimport com.pjh.service.service;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.stereotype.Repository;\nimport javax.annotation.Resource;\nimport javax.xml.bind.SchemaOutputResolver;\n\n@Repository(\"serviceImp\")\npublic class serviceImp implements service {\n @Autowired\n @Qualifier(\"userDaoImp\")\n \n private userDaoImp userDaoImp;\n public void save() {\n System.out.println(\"sssssss\");\n userDaoImp.save();\n }\n\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"测试类"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@Test\n public void test(){\n ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(\"applicationContext.xml\");\n service service =(service) classPathXmlApplicationContext.getBean(\"serviceImp\");\n service.save();\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922082716201.png#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c6/c63a8cc0e2a5199cf8900464a99cb905.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922082716201.png#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"方式二:使用@Resource()"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就是把 @Autowired + @Qualifier(\"userDaoImp\")换成@Resource(name=\"userDaoImp\"),其余代码均与方式一相同"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.service.Imp;\nimport com.pjh.dao.Imp.userdao;\nimport com.pjh.dao.userDaoImp;\nimport com.pjh.service.service;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.stereotype.Repository;\nimport javax.annotation.Resource;\nimport javax.xml.bind.SchemaOutputResolver;\n@Repository(\"serviceImp\")\npublic class serviceImp implements service {\n /* @Autowired\n @Qualifier(\"userDaoImp\")*/\n @Resource(name=\"userDaoImp\")\n private userDaoImp userDaoImp;\n public void save() {\n System.out.println(\"sssssss\");\n userDaoImp.save();\n }\n\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922084023247.png#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/de/de42135e12cab5f5bac5c56f62a41cba.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922084023247.png#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"令人蛋碎的事情来了,报错了,意思是不支持版本5"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922084125530.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e1/e10d5579449d95ac3fec3818c0f4805f.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922084125530.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解决方案:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"File->setting下设置项目的jdk版本"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/2020092208475646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/34/3404b432d527e8985a7e3a6214f7ac58.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/2020092208475646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"File->ProjectStruct下设置版本"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922084858890.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b6/b651579cc5a80fdc8053f47d3200e069.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922084858890.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/2020092208495370.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0f/0fb1f3cc67344cf6ea81f73edc801c30.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/2020092208495370.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"又报错了,这。。。。。我tm心态崩了呀"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922085106310.png#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5f/5f629e79119223b1ab50831363ec8d18.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922085106310.png#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"根据报错的内容是报了个空指针异常,这是为什么呢?为什么使用@Autowired + @Qualifier(\"userDaoImp\")不报错换成@Resource(name=\"userDaoImp\")就报空指针呢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原因"},{"type":"text","text":":jdk版本不支持"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解决方案:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1.更换本地的jdk版本,最好jdk1.8以上,jdk9不支持有bug"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2.再maven.pom文件中引入如下依赖"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\n org.apache.tomcat\n tomcat-annotations-api\n 7.0.47\n \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"成功运行"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922085638916.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/70/70695aa602fdfd1034c2e29dbe76646a.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922085638916.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}},{"type":"text","marks":[{"type":"strong"}],"text":"终于解决完了"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922085725833.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ac/acba3a23582e3a32ff9af548bac052c8.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922085725833.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"使用@Scope注解标注Bean的范围"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"使用@Scope(\"prototype\")"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.dao;\nimport com.pjh.dao.Imp.userdao;\nimport org.springframework.context.annotation.Scope;\nimport org.springframework.stereotype.Component;\n@Component(\"userDaoImp\")\n@Scope(\"prototype\")\npublic class userDaoImp implements userdao {\n public void save() {\n System.out.println(\"save\");\n }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"测试代码"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@Test\n public void test(){\n ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(\"applicationContext.xml\");\n userdao userdao =(userdao) classPathXmlApplicationContext.getBean(\"userDaoImp\");\n userdao userdao1 =(userdao) classPathXmlApplicationContext.getBean(\"userDaoImp\");\n System.out.println(userdao);\n System.out.println(userdao1);\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922090603570.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2f/2f2605e42da1fe777964669b71d2613c.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922090603570.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"使用@Scope(\"singleton\")"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.dao;\nimport com.pjh.dao.Imp.userdao;\nimport org.springframework.context.annotation.Scope;\nimport org.springframework.stereotype.Component;\n@Component(\"userDaoImp\")\n@Scope(\"singleton\")\npublic class userDaoImp implements userdao {\n public void save() {\n System.out.println(\"save\");\n }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/2020092209070120.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/86/86a8fd20a31c47e131eed76ad8faa144.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/2020092209070120.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"在这里顺便给大家复习复习singleton与prototype的区别吧"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"singleton"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bean的实例化个数:1个"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bean的实例化时机:当Spring核心配置文件被加载时"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bean的生命周期:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对象创建:当应用加载时对象创建"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对象运行:只要容器在,对象就一直活着"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对象销毁:当应用卸载,容器销毁时"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"prototype:在使用getBean方法的时候创建bean"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"prototype"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bean的实例化格式:多个"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bean的实例化时机:当调用getBean()方法时,实例化Bean"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对象创建:当使用对象时,创建新的对象实例"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对象运行:只要对象在使用中,对象就一直存在"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对象销毁:对象长时间不使用,就会被java的垃圾回收机制回收"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"初始化方法和销毁方法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"初始化方法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@PostConstract"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@PostConstruct\n public void constract(){\n System.out.println(\"初始化方法\");\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"销毁方法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"@PostDestroy"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" @PreDestroy\n public void destroy(){\n System.out.println(\"在对象销毁前执行\");\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"@Value()进行注入"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.dao;\nimport com.pjh.dao.Imp.userdao;\nimport com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.context.annotation.Scope;\nimport org.springframework.stereotype.Component;\nimport javax.annotation.PostConstruct;\nimport javax.annotation.PreDestroy;\n@Component(\"userDaoImp\")\n@Scope(\"singleton\")\npublic class userDaoImp implements userdao {\n public void save() {\n System.out.println(\"save\");\n System.out.println(\"读取配置文件:\"+one);\n System.out.println(\"普通类型:\"+a);\n }\n @Value(\"${one.one}\")\n private String one;\n @Value(\"4\")\n private String a;\n\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"配置文件信息"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"one.one=1\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"applicationContext中添加的内容"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"测试代码"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@Test\n public void test(){\n ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(\"applicationContext.xml\");\n userdao userdao =(userdao) classPathXmlApplicationContext.getBean(\"userDaoImp\");\n userdao.save();\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922094336949.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/22/220f980bc044d6ad02a97ea2a80b8fe4.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922094336949.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用上面的注解还不可以全部替代xml配置文件,还需要使用注解替代的配置如下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"非自定义的Bean的配置:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"加载properties文件的配置:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注解扫描的配置:context:component-scan"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"引入其他文件:"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"3"},{"type":"text","text":"|"},{"type":"text","marks":[{"type":"italic"}],"text":"0"},{"type":"text","text":"Spring新注解"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"@Configuration"},{"type":"text","text":" 用于指定当前类是一个Spring配置类,创建容器时会从该类上加载注解,该类不能是匿名类与final类"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"@ComponentScan"},{"type":"text","text":" 用于指定Spring在初始化容器的时候要扫描包"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作用与Spring中的此代码相同:context:component-scan"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"@Bean"},{"type":"text","text":" 使用方法,将该方法的返回值返回到容器中"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"@Import"},{"type":"text","text":" 用于导入其他配置类"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"@PropertySource"},{"type":"text","text":" 用于加载properties文件中的配置"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"案例"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"核心配置类"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.config;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.context.annotation.Import;\nimport org.springframework.stereotype.Component;\n/*标志该类是核心配置类*/\n@Configuration\n/*扫描包*/\n@Component(\"com/pjh\")\n/*导入其他配置类*/\n@Import(DataSourceConfig.class)\npublic class Springconfig {\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"副配置类"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"package com.pjh.config;\nimport com.mchange.v2.c3p0.ComboPooledDataSource;\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.PropertySource;\nimport javax.sql.DataSource;\nimport java.beans.PropertyVetoException;\n/*加载配置文件*/\n@PropertySource(\"classpath:jdbc.properties\")\npublic class DataSourceConfig {\n @Value(\"${jdbc.driver}\")\n private String driver;\n @Value(\"${jdbc.url}\")\n private String url;\n @Value(\"${jdbc.username}\")\n private String username;\n @Value(\"${jdbc.password}\")\n private String password;\n /*Spring会将当前方法的返回值以指定名称存储到Spring容器中*/\n @Bean(\"dataSource\") \n public DataSource getDataSource() throws PropertyVetoException {\n ComboPooledDataSource dataSource = new ComboPooledDataSource();\n dataSource.setDriverClass(driver);\n dataSource.setJdbcUrl(url);\n dataSource.setUser(username);\n dataSource.setPassword(password);\n return dataSource;\n }\n}\n\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"测试函数"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@Test\n public void test() throws SQLException {\n ApplicationContext applicationContext = new\n AnnotationConfigApplicationContext(Springconfig.class);\n DataSource bean = (DataSource)applicationContext.getBean(\"dataSource\");\n Connection connection = bean.getConnection();\n System.out.println(connection);\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"结果"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"成功创建connection连接"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922184238237.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/df/df84f9d152aad9b47a8c98d815714b0b.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922184238237.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BqaDg4,size_16,color_FFFFFF,t_70#pic_center","title":null}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"4"},{"type":"text","text":"|"},{"type":"text","marks":[{"type":"italic"}],"text":"0"},{"type":"text","text":"Spring集成junit"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为什么使用Spring集成junit?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在测试类中每个类都要写下面的两行代码"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"ApplicationContext applicationContext = new\n AnnotationConfigApplicationContext(Springconfig.class);\n DataSource bean = (DataSource)applicationContext.getBean(\"dataSource\");\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"这两行代码的作用是获取容器,不写的话报空指针异常"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为了让我们测试的时候不用进行反复的写上述两行的操作,我们使用Spring来集成junit,用springjunit来创建spring容器,"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们只需将配置文件的名称告诉他们即可,将需要的bean直接在容器中进行注入"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Spring集成junit的步骤"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要导入的jar包"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\n org.springframework\n spring-test\n 5.0.2.RELEASE\n \n \n junit\n junit\n 4.13\n test\n \n"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"5"},{"type":"text","text":"|"},{"type":"text","marks":[{"type":"italic"}],"text":"0"},{"type":"text","text":"这是我spring系列的学习文章,我也会不断的学习,文章如有错误还请批评指正,如有帮助大家可以点点关注,我们一同学习"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922201913237.png#pic_center","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6f/6f10ce548c53cceed4767e1a571f6562.png","alt":"在这里插入图片描述","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://img-blog.csdnimg.cn/20200922201913237.png#pic_center","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章