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配置

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	>
		
	<!-- action暂未用注解 -->
	<bean id="NoticeAction" class="cn.life.routine.action.NoticeAction" scope="prototype" ></bean>
	
	<!-- 注解测试,routine -->
	<context:component-scan base-package="cn.life.routine"></context:component-scan>
</beans>


现在依次贴出service,dao

service接口

/**
 * 注解
 * @author Francis.Hu
 * @createDate Oct 21, 2012
 */
public interface TestService {

	/**
	 * 注解测试
	 * @return
	 */
	public String getTestAnnotation();
}

service实现类


 

package cn.life.routine.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.life.routine.dao.TestDao;

/**
 * 注解测试
 * @author Francis.Hu
 * @createDate Oct 21, 2012
 */
@Service("testService")
public class TestServiceImp implements TestService{

	/**
	 * 自动装配
	 */
	@Autowired
	@Qualifier("testDao")
	//@Resource(name="testDao"), 等价于<property ………… ref="testDao" />
	private TestDao testDao;
	
	public String getTestAnnotation() {
		return testDao.getTestDaoAnnotation();
	}

	public TestDao getTestDao() {
		return testDao;
	}

	public void setTestDao(TestDao testDao) {
		this.testDao = testDao;
	}

}


 dao层接口

/**
 * 测试注解
 * @author Francis.Hu
 * @createDate Oct 21, 2012
 */
public interface TestDao {

	/**
	 * 得到dao层注解
	 * @return
	 */
	public String getTestDaoAnnotation();
}

dao层实现类       

/**
 * 测试注解
 * @author Francis.Hu
 * @createDate Oct 21, 2012
 */
@Repository("testDao")
public class TestDaoImpl implements TestDao {

	public String getTestDaoAnnotation() {
		return "This is testDao Annotation";
	}

}


下面是action中的调用

	/**
	 * 测试注解
	 * @return
	 */
	@Resource(name="testService")
	private TestService testService;
	public String testAnnotation(){
		String result = testService.getTestAnnotation();
		System.out.println(result);
		return SUCCESS;
	}
	public TestService getTestService() {
		return testService;
	}

	public void setTestService(TestService testService) {
		this.testService = testService;
	}
	/*********************测试结束******************************/


 

开发中没有绝对的xml配置或者绝对的零配置 ,只有合理的相互结合才能保证项目的可读性,高效性,个人语言组织能力有限,如果有什么问题或者建议请发邮件至franci.hu.0115@gmail.com      

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值