<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config />
<context:component-scan base-package=”com.eric.spring”>
</beans>

1.annotation-config是对标记了 Spring's @Required、@Autowired、JSR250's @PostConstruct、@PreDestroy、@Resource、JAX-WS's @WebServiceRef、EJB3's @EJB、JPA's @PersistenceContext、@PersistenceUnit等注解的类进行对应的操作使注解生效。
2.base-package为需要扫描的包(含所有子包),负责扫描那些类有注解。

注:前面讲要使用注解需要配置: <context:annotation-config />但如果使用了@Component就不需要加它了,因为:<context:component-scan base-package="com.zchen">里面默认了<context:annotation-config />。

而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。

@Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解,可以只使用单一组件)

事例

1.定义接口PersonDao

package com.qn.dao;

public interface PersonDao {
     void add();
}

2.定义接口PersonService

package com.qn.dao;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public interface PersonService {
 void save();

}

3.定义类PersonDaoBean实现PersonDao

package com.qn.service.impl;

import com.qn.dao.PersonDao;

public class PersonDaoBean implements PersonDao{

public void add() {
  System.out.println("PersonDaoBean的添加方法");
 }

}

4.定义类PersonServiceBean实现PersonService

package com.qn.service.impl;

import javax.annotation.Resource;

import com.qn.dao.PersonDao;
import com.qn.dao.PersonService;

public class PersonServiceBean implements PersonService {
    @Resource
    private PersonDao personDao;
    private String name;
    public PersonServiceBean(){}
 public PersonServiceBean(PersonDao personDao, String name) {
  this.personDao = personDao;
  this.name = name;
 }

public void save(){
  personDao.add();
  System.out.println(name);
 }
}

5.在bean中进行配置

<?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-2.5.xsd
           http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">
          <context:annotation-config/>
          <bean name="personDao" class="com.qn.service.impl.PersonDaoBean"></bean>
          <bean name="personService" class="com.qn.service.impl.PersonServiceBean"></bean>
</beans>

6.在test中进行测试

package com.qn.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qn.dao.PersonService;

public class test {

public static void main(String []args){
  AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
  PersonService personService=(PersonService) ctx.getBean("personService");
  personService.save();  
 }
}

@Resource

的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

  @Resource装配顺序 
  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常 
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 
  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常 
  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配

链接:http://www.cnblogs.com/pwc1996/p/4839131.html

http://www.cnblogs.com/loveweiwei/p/3901387.html

springmvc注解配置的更多相关文章

  1. spring-mvc注解配置小记

    Controller中注解Service时,Service的实现类需加@Service,dao的实现类需加@Repository. 另:配置文件中对应的包也需要扫描到!!! <context:a ...

  2. SpringInAction--Spring Web应用之SpringMvc 注解配置

    Spring MVC 是当前Web服务器中常用的结构,今天就来学习这相关的知识,首先上图——Spring请求的时候所经历的坎坷之路: (书上原话,算是解释..) 在请求离开浏览器时① ,会带有用户所请 ...

  3. SpringMVC 注解配置

    使用注解配置spring  mvc (1)spring mvc的配置文件 <?xml version="1.0" encoding="UTF-8"?> ...

  4. SpringMVC注解配置处理器映射器和处理器适配器

    一.springmvc.xml中配置方式 <!--注解映射器 --> <bean class="org.springframework.web.servlet.mvc.me ...

  5. springMVC注解方式+easyUI+MYSQL配置实例

    刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+e ...

  6. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  7. 学习笔记_J2EE_SpringMVC_02_注解配置

    SpringMVC注解配置 1.测试环境: 名称 版本 备注 操作系统 Windows10 专业版1809X64   WEB服务器 Tomcat 8.5 X64   浏览器 Google Chrome ...

  8. 《SpringMVC从入门到放肆》八、SpringMVC注解式开发(基本配置)

    上一篇我们结束了配置式开发,配置式开发目前在企业中用的并不是很多,大部分企业都在使用注解式开发,所以今天我们就来学习注解式开发.所谓SpringMVC注解式开发是指,处理器是基于注解的类的开发方式.对 ...

  9. 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置

    首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...

随机推荐

  1. 原生态js,鼠标按下后,经过了那些单元格

    本来是要判断那些单元格被选中,结果发现行不通,只能判断鼠标按下后,经过了那些单元格 之所以发出来,是觉得案例还有很多有意思的地方 onmouseover  的持续触发,导致了很多重复元素 由于将事件绑 ...

  2. iOS开发——多线程篇——快速生成沙盒目录的路径,多图片下载的原理、SDWebImage框架的简单介绍

    一.快速生成沙盒目录的路径 沙盒目录的各个文件夹功能 - Documents - 需要保存由"应用程序本身"产生的文件或者数据,例如:游戏进度.涂鸦软件的绘图 - 目录中的文件会被 ...

  3. 解决mvc部署在IIS上以后出现404错误

    首先:aspnet_regiis -i 如果还不行,更改web.config文件,添加红色的部分 <system.webServer> <modules runAllManagedM ...

  4. 分析Masonry

    一. 继承关系 1.MASConstraint (abstract) MASViewContraint MASComposisionConstraint 2. UIView NSLayoutConst ...

  5. 符瑞艺 160809228_C语言程序设计实验2 选择结构程序设计

    实验2- 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 参考: 源码: #include <stdio.h> int main() ...

  6. hough变换检测线和圆

    参考:http://blog.163.com/yuyang_tech/blog/static/21605008320130233343990/ 这篇介绍的基本思想. http://www.cnblog ...

  7. eclipse添加velocity项目

    1.首先添加jar包,记得包含以下的主要两个类别 2.新建一个servlet类(继承自VelocityViewServlet) package com.servlet; import java.uti ...

  8. JavaScript深入浅出2-表达式和运算符

    慕课网教程视频地址:Javascript深入浅出 表达式是指能计算出值的任何可用程序单元 原始表达式:常量.直接量 3.14,“test” 关键字 null,this 变量 i,k,j 表达式含:原始 ...

  9. IOS路线图

    存档,存档...

  10. 初识Flask

    首先在学习flask的前提,我是使用了很久的django和tornado,现在在写总结也是本着工作后方便使用flask 少点东西,对flask的介绍和优点总结 1.安装 pip install fla ...