整理spring零配置(Annotation)
2016-03-14 21:25
363 查看
spring零配置(Annotation)学习笔记
有关spring的注解,今天了解了下,现在一方面做下学习总结,另一方面给学习的筒子做个借鉴。
spring提供相关的几个Annotation来标注bean先列出来
@Component:标注一个普通的spring bean
@Controller:标注一个控制器组件类如action
@Service:标注一个逻辑控制类如Service层
@Repository:标注一个持久层Dao组件类
再列几个
@Scope:相信大家对这个不陌生吧,表示bean的作用域,使用方式:Scope("prototype")
@Resource:配合依赖,使用方式:Resource(name="XXXX")等同于xml中的配置<property …… ref="XXXX" />
@Autowired:自动装配,默认按照type装配,如果需要按照name装配就需要和下面的相结合了
@Qualifier
针对自动装配下面展示两种写法分别表示属性修饰和set方式修饰:
@Autowried
@Qualifier("XXXX")
private XXXX xxxx;
-----------------------------------------------------
@Autowried
public void setXXX(@Qualifier("xxxx") XXX xxx){}
基本常用的注解也就上面的了,现在贴上代码:
要想让注解生效,首先要在配置文件中指明扫描那些包下的Bean类,
包结构:
cn.life.routine
-action
-dao
-service
引入ContextSchema,spring配置
[html] view plaincopy
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:context="http://www.springframework.org/schema/context"
5. xsi:schemaLocation="
6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7. http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>"
8. >
9.
10. <!-- action暂未用注解 -->
11. <bean id="NoticeAction" class="cn.life.routine.action.NoticeAction" scope="prototype" ></bean>
12.
13. <!-- 注解测试,routine -->
14. <context:component-scan base-package="cn.life.routine"></context:component-scan>
15. </beans>
现在依次贴出service,dao
service接口
[java] view plaincopy
1. /**
2. * 注解
3. * @author Francis.Hu
4. * @createDate Oct 21, 2012
5. */
6. public interface TestService {
7.
8. /**
9. * 注解测试
10. * @return
11. */
12. public String getTestAnnotation();
13. }
service实现类
[java] view plaincopy
1. package cn.life.routine.service;
2.
3. import org.springframework.beans.factory.annotation.Autowired;
4. import org.springframework.beans.factory.annotation.Qualifier;
5. import org.springframework.stereotype.Service;
6.
7. import cn.life.routine.dao.TestDao;
8.
9. /**
10. * 注解测试
11. * @author Francis.Hu
12. * @createDate Oct 21, 2012
13. */
14. @Service("testService")
15. public class TestServiceImp implements TestService{
16.
17. /**
18. * 自动装配
19. */
20. @Autowired
21. @Qualifier("testDao")
22. //@Resource(name="testDao"), 等价于<property ………… ref="testDao" />
23. private TestDao testDao;
24.
25. public String getTestAnnotation() {
26. return testDao.getTestDaoAnnotation();
27. }
28.
29. public TestDao getTestDao() {
30. return testDao;
31. }
32.
33. public void setTestDao(TestDao testDao) {
34. this.testDao = testDao;
35. }
36.
37. }
dao层接口
[java] view plaincopy
1. /**
2. * 测试注解
3. * @author Francis.Hu
4. * @createDate Oct 21, 2012
5. */
6. public interface TestDao {
7.
8. /**
9. * 得到dao层注解
10. * @return
11. */
12. public String getTestDaoAnnotation();
13. }
dao层实现类
[java] view plaincopy
1. /**
2. * 测试注解
3. * @author Francis.Hu
4. * @createDate Oct 21, 2012
5. */
6. @Repository("testDao")
7. public class TestDaoImpl implements TestDao {
8.
9. public String getTestDaoAnnotation() {
10. return "This is testDao Annotation";
11. }
12.
13. }
下面是action中的调用
[java] view plaincopy
1. /**
2. * 测试注解
3. * @return
4. */
5. @Resource(name="testService")
6. private TestService testService;
7. public String testAnnotation(){
8. String result = testService.getTestAnnotation();
9. System.out.println(result);
10. return SUCCESS;
11. }
12. public TestService getTestService() {
13. return testService;
14. }
15.
16. public void setTestService(TestService testService) {
17. this.testService = testService;
18. }
19. /*********************测试结束******************************/
开发中没有绝对的xml配置或者绝对的零配置 ,只有合理的相互结合才能保证项目的可读性,高效性,个人语言组织能力有限
有关spring的注解,今天了解了下,现在一方面做下学习总结,另一方面给学习的筒子做个借鉴。
spring提供相关的几个Annotation来标注bean先列出来
@Component:标注一个普通的spring bean
@Controller:标注一个控制器组件类如action
@Service:标注一个逻辑控制类如Service层
@Repository:标注一个持久层Dao组件类
再列几个
@Scope:相信大家对这个不陌生吧,表示bean的作用域,使用方式:Scope("prototype")
@Resource:配合依赖,使用方式:Resource(name="XXXX")等同于xml中的配置<property …… ref="XXXX" />
@Autowired:自动装配,默认按照type装配,如果需要按照name装配就需要和下面的相结合了
@Qualifier
针对自动装配下面展示两种写法分别表示属性修饰和set方式修饰:
@Autowried
@Qualifier("XXXX")
private XXXX xxxx;
-----------------------------------------------------
@Autowried
public void setXXX(@Qualifier("xxxx") XXX xxx){}
基本常用的注解也就上面的了,现在贴上代码:
要想让注解生效,首先要在配置文件中指明扫描那些包下的Bean类,
包结构:
cn.life.routine
-action
-dao
-service
引入ContextSchema,spring配置
[html] view plaincopy
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:context="http://www.springframework.org/schema/context"
5. xsi:schemaLocation="
6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7. http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>"
8. >
9.
10. <!-- action暂未用注解 -->
11. <bean id="NoticeAction" class="cn.life.routine.action.NoticeAction" scope="prototype" ></bean>
12.
13. <!-- 注解测试,routine -->
14. <context:component-scan base-package="cn.life.routine"></context:component-scan>
15. </beans>
现在依次贴出service,dao
service接口
[java] view plaincopy
1. /**
2. * 注解
3. * @author Francis.Hu
4. * @createDate Oct 21, 2012
5. */
6. public interface TestService {
7.
8. /**
9. * 注解测试
10. * @return
11. */
12. public String getTestAnnotation();
13. }
service实现类
[java] view plaincopy
1. package cn.life.routine.service;
2.
3. import org.springframework.beans.factory.annotation.Autowired;
4. import org.springframework.beans.factory.annotation.Qualifier;
5. import org.springframework.stereotype.Service;
6.
7. import cn.life.routine.dao.TestDao;
8.
9. /**
10. * 注解测试
11. * @author Francis.Hu
12. * @createDate Oct 21, 2012
13. */
14. @Service("testService")
15. public class TestServiceImp implements TestService{
16.
17. /**
18. * 自动装配
19. */
20. @Autowired
21. @Qualifier("testDao")
22. //@Resource(name="testDao"), 等价于<property ………… ref="testDao" />
23. private TestDao testDao;
24.
25. public String getTestAnnotation() {
26. return testDao.getTestDaoAnnotation();
27. }
28.
29. public TestDao getTestDao() {
30. return testDao;
31. }
32.
33. public void setTestDao(TestDao testDao) {
34. this.testDao = testDao;
35. }
36.
37. }
dao层接口
[java] view plaincopy
1. /**
2. * 测试注解
3. * @author Francis.Hu
4. * @createDate Oct 21, 2012
5. */
6. public interface TestDao {
7.
8. /**
9. * 得到dao层注解
10. * @return
11. */
12. public String getTestDaoAnnotation();
13. }
dao层实现类
[java] view plaincopy
1. /**
2. * 测试注解
3. * @author Francis.Hu
4. * @createDate Oct 21, 2012
5. */
6. @Repository("testDao")
7. public class TestDaoImpl implements TestDao {
8.
9. public String getTestDaoAnnotation() {
10. return "This is testDao Annotation";
11. }
12.
13. }
下面是action中的调用
[java] view plaincopy
1. /**
2. * 测试注解
3. * @return
4. */
5. @Resource(name="testService")
6. private TestService testService;
7. public String testAnnotation(){
8. String result = testService.getTestAnnotation();
9. System.out.println(result);
10. return SUCCESS;
11. }
12. public TestService getTestService() {
13. return testService;
14. }
15.
16. public void setTestService(TestService testService) {
17. this.testService = testService;
18. }
19. /*********************测试结束******************************/
开发中没有绝对的xml配置或者绝对的零配置 ,只有合理的相互结合才能保证项目的可读性,高效性,个人语言组织能力有限
相关文章推荐
- spring-hibernate修改数据报错
- Java内部类详解
- java知识盲区
- Java集合类
- android 在eclipse中使用RecyclerView
- 二叉树java版
- 静态内部类,成员内部类,外部类之间的一些知识总结
- eclipse中搭建svn开发管理环境
- SpringMVC——hello SpringMVC
- java实现走迷宫
- java path classpath JAVA_HOME
- Java实现发送邮件功能(可带附件)
- java设计模式之建造者模式
- Java —内部类
- 修改 JVM 输出数组
- struts2上传下载文件
- Java设计模式之单例(Singleton)模式
- [Java开发之路](21)Comparator与Comparable
- eclipse启动无响应,停留在Loading workbench状态,或老是加载不了revert resources
- JAVA算法---最大公约数和最小公倍数