配置文件:

 <?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注解配置的更多相关文章

  1. 关于Spring注解配置的步骤

    今天分享一下 关于Spring注解配置的流程 1 导包:如下图所示 2 书写User和Car类  代码如下 package cn.lijun.bean; public class Car { priv ...

  2. Spring注解配置和xml配置优缺点比较

    Spring注解配置和xml配置优缺点比较 编辑 ​ 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...

  3. spring注解配置实例

    在spring中使用注解配置前需要先在配置文件指定需要扫描的包. 通过注解的方式依赖注入,可以不用创建set方法,也不用在xml文件中申明注入关系. 实例结构如下: 整个流程是: 先创建好数据库的表对 ...

  4. spring注解配置启动过程

    最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...

  5. 【Spring实战】Spring注解配置工作原理源码解析

    一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...

  6. Spring注解配置Aop

    之前学习了SpringAop的基本原理.http://www.cnblogs.com/expiator/p/7977975.html 现在尝试使用注解来配置SpringAop. Aop,面向切面编程. ...

  7. 5.spring:注解配置 Bean

    在classpath中扫描组件 组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定的组件包括: ->@Componment:基于注解,标识一个受Spring管理的 ...

  8. 【转】【Spring实战】Spring注解配置工作原理源码解析

    一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...

  9. Spring 注解配置Bean

    一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...

  10. Spring注解配置、Spring aop、整合Junit——Spring学习 day2

    注解配置: 1.为主配置文件引入新的命名空间(约束) preference中引入文件 2.开启使用注解代理配置文件 <?xml version="1.0" encoding= ...

随机推荐

  1. HDU 5755 Gambler Bo(高斯消元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5755 [题目大意] 一个n*m由0,1,2组成的矩阵,每次操作可以选取一个方格,使得它加上2之后对 ...

  2. java对象的比较分析

    关于对象的比较我们可以通过以下三种手段来实现 一.利用"=="比较引用 Java中,当比较简单类型变量时用"==",只要两个简单类型值相等即返回ture,否则返 ...

  3. 160G 视频教程(Java+Android+项目视频)免费下载

    我不喜欢多说没用,直接给下载链接,进去直接下载,下载不动的联系网站客服解决!我只和我的好朋友们分享好的视频教程 http://edu.csdn.net/main/video.shtml 视频教程目录过 ...

  4. hdu4491 Windmill Animation (几何)

    Windmill Animation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. maven项目启动

    1服务install 2 build (tomcat:run)

  6. CSS文字样式

    font-family:通常文章的正文使用的是易读性较强的serif字体,用户长时间阅读下不easy疲劳.而标题和表格则採用较醒目的sans-serif字体.Web设计及浏览器设置中也推荐遵循此原则. ...

  7. Log4Net 使用总结

    在项目中要记录日志,便于程序调试.于是就想到了大名鼎鼎的Log4Net,这货可以方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包括MS SQL Server, Access, O ...

  8. 如何最简单的优化MySql

    1.创建索引,一定要根据实际情况来创建,如果是连接表查询,如一个主帐号连接多个子帐号,可以考虑两个或三个以上的多索引: 2.合理利用时间排序,由于大多数表格用时间来排序,数据量相当大的时候,在时间列上 ...

  9. C#连接Oracle数据库基本类

    C#用来连接oracle数据库的基本类: using System; using System.Collections.Generic; using System.Linq; using System ...

  10. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...