<?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表单元素checked、radio被选中的几种方式-遁地龙卷风

    0.环境 <input type="checkbox"  value="lol"/>lol var lol = document.getElemen ...

  2. BZOJ 3907: 网格

    Description 求不跨过直线 \(y=x\) ,到达 \((n,m)\) 的方案数. Sol 组合数学+高精度. 这个推导过程跟 \(Catalan\) 数是一样的. 答案就是 \(C^{n+ ...

  3. Eclipse中项目红叉但找不到错误解决方法

    首先windows-show view-problems 根据地址查找错误 若提示: Description    Resource    Path    Location    TypeJava c ...

  4. iOS关于UILabel 基本属性 背景图片 背景色

    [代码] iOS关于UILabel 基本属性 背景图片 背景色 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  5. php for循环建数据

    $data = []; ; $i<; $i++) { $data[] = $i; } print_r($data); 执行后打印的结果为: Array ( [] => [] => [ ...

  6. Remove Invalid Parentheses

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  7. Linux之绝处逢生------SysRq

    参考: http://www.linuxfly.org/post/545/ http://www.jb51.net/article/13525.htm 做法: 未雨绸缪 # " > / ...

  8. Appium 服务器参数

    # Appium 服务器参数 使用方法: `node . [标志]` ## 服务器标志所有的标志都是可选的,但是有一些标志需要组合在一起才能生效. <expand_table> |标志|默 ...

  9. K3安装记录

    安装的某些客户端没有客户端管理工具,测试问题所在 K3安装目录选择到D盘就可以,它自动生成kingdee\k3路径

  10. Session原理浅析

    什么是Sesson? 简单说就是一个会话级的cookie,外加服务器端内存中一组散列表. 当你关闭浏览器的时候,这个cookie将消失. 这个cookie不写在磁盘上,而是存在于浏览器缓存. 关于Se ...