spring零配置Annotation札记

spring零配置Annotation笔记

    spring提供相关的几个Annotation来标注bean先列出来

 

@Component:标注一个普通的spring bean

@Controller:标注一个控制器组建类如action

@Service:标注一个逻辑控制类 service层

@Repository:标识一个持久化层Dao组件类

 

再列几个

@Scope:表示bean作用域使用方式:Scope("prototype")

@Resource:

@Autowired:自动装配默认按照type装配,如果需要按照名称装配则需要和下面相结合

@Qualifier  针对自动装配下面展示两种写法分别表示属性修饰和set方式修饰:

  @Autowired

  @Qualifier("namexxx")

  private XXXX xxxx;

-------------------------------------------------

  @Autowired

  public void setXxx(@Qualifier("namexxx") Xxx xxx)

 

用一个项目工程的小例子

包结构:

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 <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>"
	>	
	<bean id="NoticeAction" class="cn.life.routine.action.NoticeAction" scope="prototype" />
       <!--注解测试,routine-->
      <context:component-scan base-package="cn.life.routine" />
</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配置或者绝对的零配置 ,只有合理的相互结合才能保证项目的可读性,高效性