springmvc注解配置
<?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注解配置的更多相关文章
- spring-mvc注解配置小记
Controller中注解Service时,Service的实现类需加@Service,dao的实现类需加@Repository. 另:配置文件中对应的包也需要扫描到!!! <context:a ...
- SpringInAction--Spring Web应用之SpringMvc 注解配置
Spring MVC 是当前Web服务器中常用的结构,今天就来学习这相关的知识,首先上图——Spring请求的时候所经历的坎坷之路: (书上原话,算是解释..) 在请求离开浏览器时① ,会带有用户所请 ...
- SpringMVC 注解配置
使用注解配置spring mvc (1)spring mvc的配置文件 <?xml version="1.0" encoding="UTF-8"?> ...
- SpringMVC注解配置处理器映射器和处理器适配器
一.springmvc.xml中配置方式 <!--注解映射器 --> <bean class="org.springframework.web.servlet.mvc.me ...
- springMVC注解方式+easyUI+MYSQL配置实例
刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+e ...
- SpringMVC基础配置(通过注解配置,非xml配置)
SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...
- 学习笔记_J2EE_SpringMVC_02_注解配置
SpringMVC注解配置 1.测试环境: 名称 版本 备注 操作系统 Windows10 专业版1809X64 WEB服务器 Tomcat 8.5 X64 浏览器 Google Chrome ...
- 《SpringMVC从入门到放肆》八、SpringMVC注解式开发(基本配置)
上一篇我们结束了配置式开发,配置式开发目前在企业中用的并不是很多,大部分企业都在使用注解式开发,所以今天我们就来学习注解式开发.所谓SpringMVC注解式开发是指,处理器是基于注解的类的开发方式.对 ...
- 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置
首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...
随机推荐
- Redis学习笔记之ABC
Redis学习笔记之ABC Redis命令速查 官方帮助文档 中文版本1 中文版本2(反应速度比较慢) 基本操作 字符串操作 set key value get key 哈希 HMSET user:1 ...
- JQuery选择器细节-遁地龙卷风
1.层次选择器-子元素选择器 <body> <div> <p>lol</p> <div> <p></p> </ ...
- SpringBoot使用的心得记录
security配置 import com.yineng.corpsysland.security.*; import com.yineng.corpsysland.web.filter.Author ...
- 使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB)
使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB) http://www.cnblogs.com/mrkelly/p/4015245.html 以往调试Androi ...
- trigger() & bind() 使用心得
trigger(type) 在每一个匹配的元素上触发某类事件. 返回值:jQuery 参数: type (String): 要触发的事件类型 示例: $("p").trigger( ...
- [转载]JavaEE学习篇之——JDBC详解
原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/26164629 目录1.摘要2.JDBC的使用步骤 1.注册驱动 只做一次 ...
- Python自动化之pickle和面向对象初级篇
pickle模块扩展 1 pickle之文件操作 示例1 with open("test", 'rb') as f: lines = f.readlines() print(pic ...
- 2.6---找有环链表的开头结点(CC150)
public ListNode detectCycle(ListNode head) { ListNode fast = head; ListNode slow = head; int flag = ...
- sublime-text3配置编译php
在sublime-text中配置php的编译环境非常简单,只需要新建一个build system就可以了 步骤: 1. 工具->编译系统->新编译系统,将默认的内容替换为如下代码:蓝字部分 ...
- ubuntu查看端口占用
查看端口号 sudo netstat -ltnp | 结束进程 sudo kill pid