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= ...
随机推荐
- HDU 5787 K-wolf Number(数位DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5787 [题目大意] 求区间[L,R]内十进制数相邻k位之间不相同的数字的个数. [题解] 很显然的 ...
- 时尚B2B方兴未艾-Maker’s Row 获100万美元种子投资 |华丽志
时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 |华丽志 华丽志 » 网internet, 时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 由 luxeco 发 ...
- Candy----HDU4465----数学题
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4465 题目意思: 有两个箱子,每个箱子装有N个糖果 打开第一个箱子的概率是P,另外一个就是1-P 当小 ...
- Sql 字符串操作类COALESCE
SqlServer中肯定有过将表中某列的值拼接成字符串,以","或者其他符号隔开的情况吧,一般情况我们会这样做: declare @returnValue nvarchar(max ...
- 使用命令行将Excel数据表导入Mysql中的方法小结
从Excel数据表导入MySQL,已经做过好几次了,但每次都会碰到各种问题:invalid utf8 character string, data too long, ...,浪费了不少时间 为了提高 ...
- ftp读取txt数据并插入数据库
去官网下载http://enterprisedt.com/ .netftp组件 目前最新版本为2.2.3,下载后在bin目录中找到edtFTPnet.dll,在项目中添加引用. using Enter ...
- java动手动脑课后思考题
public class SquareInt { public static void main(String[] args) { int result; ; x <= ; x++) { res ...
- USACO Section 5.3 Big Barn(dp)
USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1 (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...
- jQuery.validate 中文 API
名称 返回类型 描述 validate(options) Validator 验证所选的 FORM. valid() Boolean 检查是否验证通过. rules() Options 返回元素的验证 ...
- C#指定目录存放DLL
C#开发中,常常会用到不少扩展库,把这些扩展库的大量DLL放在软件目录下面,非常不美观. 通过设置自定义的DLL存放目录,可以把DLL存在指定的目录下面. 代码如下: <?xml version ...