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= ...
随机推荐
- lombk在IDEA中报ClassNotFoundException错误
今天接手了一个项目,用到了lombk,第一次用到,做为纯JAVA来说,确实不错. 不过在使用中碰到了一个问题,就是在IDEA中,可以在结构中看到getter和setter等都已经正确的生成了,但是运行 ...
- es5 api
ES5 Object.create(prototype, descriptors) //创建对象 var o1 = {foo:'bar'}; var o2 = new Object(); //Obje ...
- 由Mifare 1卡破解带来的危险以及应对方法
今年年初以来,一个消息的传出震惊了整个IC卡行业.最近,德国和美国的研究人员成功地破解了NXP的Mifare1芯片的安全算法.Mifare1芯片主要用于门禁系统访问控制卡,以及一些小额支付卡,应用范围 ...
- QtSoap开发web services客户端程序
首先需要下载QtSoap开源包,下载地址为: http://www.filestube.com/q/qtsoap+download, 我使用的是:qtsoap-2.6-opensource(不 ...
- 让.net程序自动运行在管理员权限下
原文:让.net程序自动运行在管理员权限下 如何让.net程序自动运行在管理员权限下 VS2010 c# 编译的WINFORM程序 在Win7 以管理员身份运行 windows 7和vista提高的系 ...
- javascript - Show mouse cursor in phantom.js - Stack Overflow
javascript - Show mouse cursor in phantom.js - Stack Overflow Show mouse cursor in phantom.js
- Android SQLite之乐学成语项目数据库存储
一.SQLite是什么?为什么要用SQLite?SQLite有什么特点?(下面小编一 一解答) ①SQLite是一个轻量级的关系型数据库,运算速度快,占用资源少,很适合在移动设备上使用, 不仅支持 标 ...
- java中如何计算两个时间段的月份差
直接计算,先取得两个日期的年份和月份,月份差=(第二年份-第一年份)*12 + 第二月份-第一月份
- C++基础知识梳理--C++的6个默认函数
C++有六个默认函数:分别是 1.default构造函数; 2.默认拷贝构造函数; 3.默认析构函数; 4.赋值运算符; 5.取值运算符; 6.取值运算符const; // 这两个类的效果相同 cla ...
- html5储存篇(二)
indexedDB 相对于html5 中提到 web SQL Database,w3c已经明确声明放弃对其的继续支持,开始支持新的客户端数据库 indexedDB ,indexedDB 是一种no ...