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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--context:component-scan 指定 扫描的包 -->
<!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class"
的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
<!-- <context:component-scan base-package="imooc_spring.test.anotation"
resource-pattern="myrepository/*.class"></context:component-scan> --> <!--
context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"
子节点指定排除哪些注解
context:include-filter type="annotation" 需要结合context:component-scan 标签的
use-default-filters="false"来使用 context:exclude-filter type="assignable"
这个expression指的是自己写的类,意思排除哪些类
expression="imooc_spring.test.anotation.TestObj"
-->
<context:component-scan base-package="imooc_spring.test.anotation" >
<!-- <context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository" /> --> <context:exclude-filter type="assignable"
expression="imooc_spring.test.anotation.TestObj" />
</context:component-scan>
</beans>
@Repository注解:
package imooc_spring.test.anotation.myrepository; import org.springframework.stereotype.Repository; /**
* 指定id,默认为dAO,即类名首字母小写,如果指定了名称那么只能ctx.getBean(指定名称)来获取bean
* 这个例子里就只能通过ctx.getBean("wyldao)来获取DAO 的实例了;
*
* @author Wei
*/
@Repository("wyldao")
public class DAO {
/**
* 返回x和y的乘积
*
* @param x
* @param y
* @return x*y
*/
public int multi(int x, int y) {
return x * y;
}
}
@Component 注解:
package imooc_spring.test.anotation; import org.springframework.stereotype.Component;
/**
* Component 注解
* @author Wei
*
*/
@Component
public class TestObj {
public void SayHi(){
System.out.println("\nHi this is TestObj.SayHi()...");
}
}
@Controller注解:
package imooc_spring.test.anotation; import org.springframework.stereotype.Controller; @Controller
public class UserController {
public void execute(){
System.out.println("\nUserController.execute()...");
}
}
@Repository注解:
package imooc_spring.test.anotation; import org.springframework.stereotype.Repository; //@Repository
@Repository("wyl_repo")
public class UserRepositoryImpl implements IUserRepository {
//模拟持久化层
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("\nUserRepositoryImpl.save()...");
} }
@Service注解:
package imooc_spring.test.anotation; import org.springframework.stereotype.Service; @Service
public class UserService {
public void add(){
System.out.println("\nUserService.add()...");
}
}
2.另外一组注解:
@Autowired、@Resource 这两个注解的功能可以说是一样的,前面一组注解的功能也是类似的。
@Autowired 注解自动装配具有兼容类型的单个 Bean属性
可以用来标识
1 构造器,
2 普通字段(即使是非 public),
3 一切具有参数的方法
这三种都可以应用@Authwired 注解
默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
@Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似
@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
@Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
建议使用 @Autowired 注解
package imooc_spring.test.anotation; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserService {
@Autowired(required=false)
public UserRepositoryImpl rep ; public void add(){
System.out.println("\nUserService.add()...");
}
}
package imooc_spring.test.anotation; import org.springframework.stereotype.Repository; //@Repository
@Repository("wyl_repo")
public class UserRepositoryImpl implements IUserRepository {
//模拟持久化层
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("\nUserRepositoryImpl.save()...");
} }
@Autowired(required=false)
这个就相当于IOC容器里的ref属性,比如
<bean id="people" class="test.spring.autowired.Person" scope="prototype"
autowire="byName">
<property name="name" value="小明"></property>
<property name="cat" ref="cat222"></property>
</bean>
<bean id="cat222" class="test.spring.autowired.Cat">
<property name="name" value="我是小喵喵"></property>
</bean>
整个IOC容器:
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="moocAppctx" class="imooc_spring.test.aware.MoocApplicationContext"
init-method="hhhh">
</bean> <!--context:component-scan 指定 扫描的包 -->
<!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class"
的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
<!-- <context:component-scan base-package="imooc_spring.test.anotation"
resource-pattern="myrepository/*.class"></context:component-scan> --> <!-- context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
子节点指定排除哪些注解 context:include-filter type="annotation" 需要结合context:component-scan
标签的 use-default-filters="false"来使用 context:exclude-filter type="assignable"
这个expression指的是自己写的类,意思排除哪些类 expression="imooc_spring.test.anotation.TestObj" -->
<context:component-scan base-package="imooc_spring.test.anotation">
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
/> --> <!-- <context:exclude-filter type="assignable" expression="imooc_spring.test.anotation.TestObj"
/> --> </context:component-scan> </beans>
中的ref,一个bean引用另一个bean,上面的例子中就是
UserService 这个bean引用 UserRepositoryImpl 这个bean,
当然了,使用这些注解的时候都需要结合
<context:component-scan base-package="imooc_spring.test.anotation">
</context:component-scan>
来一起使用。
@Autowired(required=false)
中的required属性可以不写,不写的时候默认为required=true,表示如果没有找到要引用的bean,那么引用的这个bean就为null,
但是这样子的话可能会由于引用的bean属性为null,从而可能会引起空指针异常。

Spring注解配置的更多相关文章
- 关于Spring注解配置的步骤
今天分享一下 关于Spring注解配置的流程 1 导包:如下图所示 2 书写User和Car类 代码如下 package cn.lijun.bean; public class Car { priv ...
- Spring注解配置和xml配置优缺点比较
Spring注解配置和xml配置优缺点比较 编辑 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...
- spring注解配置实例
在spring中使用注解配置前需要先在配置文件指定需要扫描的包. 通过注解的方式依赖注入,可以不用创建set方法,也不用在xml文件中申明注入关系. 实例结构如下: 整个流程是: 先创建好数据库的表对 ...
- spring注解配置启动过程
最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...
- 【Spring实战】Spring注解配置工作原理源码解析
一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...
- Spring注解配置Aop
之前学习了SpringAop的基本原理.http://www.cnblogs.com/expiator/p/7977975.html 现在尝试使用注解来配置SpringAop. Aop,面向切面编程. ...
- 5.spring:注解配置 Bean
在classpath中扫描组件 组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定的组件包括: ->@Componment:基于注解,标识一个受Spring管理的 ...
- 【转】【Spring实战】Spring注解配置工作原理源码解析
一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...
- Spring 注解配置Bean
一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...
- Spring注解配置、Spring aop、整合Junit——Spring学习 day2
注解配置: 1.为主配置文件引入新的命名空间(约束) preference中引入文件 2.开启使用注解代理配置文件 <?xml version="1.0" encoding= ...
随机推荐
- JavaEE Tutorials (13) - 使用锁定控制对实体数据的并发访问
13.1实体锁定和并发概述180 13.1.1使用乐观锁定18113.2锁模式181 13.2.1设置锁模式182 13.2.2使用悲观锁定183
- spring集成mongodb封装的简单的CRUD
1.什么是mongodb MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB MongoDB是一个介 ...
- 黑客瑞士军刀NC使用教程
###################################################################### 1. 写在前面的话 ################### ...
- c/c++测试程序运行时间
算法分析中需要对各种算法进行性能测试,下面介绍两种通用的测试方法,由于只用到标准c语言函数,所以在各种平台和编译器下都能使用. 方法1: clock()函数 开始计时:start = clock() ...
- BootStrap 智能表单系列 四 表单布局介绍
表单的布局分为自动布局和自定义布局两种: 自动布局就是根据配置项中第二级配置项中数组的长度来自动使用不同的bootstrap栅格,通过设置autoLayout为true可以实现自动布局 自动以布局就是 ...
- Javascript之Dom学习
1.简介:DOM是一套对文档的内容进行抽象和概念化的方法.在现实世界里,人们对"世界对象模型"一定不会陌生,当人们用"房子","汽车"等这一 ...
- CentOS下成功挂载xxxxxDVDx.iso并使用yum安装软件
CentOS下成功挂载xxxxxDVDx.iso并使用yum安装软件 **不断尝试,终能到达彼岸** 测试环境为Win7 32位,VirtualBOx4.2.16+CentOS6.5,可分别到virt ...
- 4.PHP 教程_PHP 变量
PHP 变量 变量是用于存储信息的"容器": <?php $x = 5; $y = 6; $z = $x + $y; echo $z; ?> 与代数相似 x=5 y=6 ...
- Linux必学的60个命令【转载】
Linux提供了大量的命令,利用它可以有效地完成大量的工 作,如磁盘操作.文件存 [转载地址]http://blog.chinaunix.net/uid-16728139-id-3154272.ht ...
- [转] IOS中AppDelegate中的生命周期事件的调用条件
IOS中AppDelegate中的生命周期事件的调用条件 //当应用程序将要进入非活动状态执行,在此期间,应用程序不接受消息或事件,比如来电 - (void)applicationWillResign ...