使用注解需要修改bean.xml:

<?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.xsd
"> <context:annotation-config/> <bean class="example.SimpleMovieCatalog">
<qualifier value="main"/>
<!-- inject any dependencies required by this bean -->
</bean> <bean class="example.SimpleMovieCatalog">
<qualifier value="action"/>
<!-- inject any dependencies required by this bean -->
</bean> <bean id="movieRecommender" class="example.MovieRecommender"/> </beans>

黄色部分内容会使spring加载AutowiredAnnotationBeanPostProcessor等可以识别注解的bean。

@AutoWire,自动注入,一般放在属性的set方法上,会为该属性自动注入。默认的注入是使用byType,就是根据xml中bean的类型去匹配。

可以和@Qualifier匹配使用。

注解实现方式:

package com.bjsxt.service;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import com.bjsxt.dao.UserDAO;

import com.bjsxt.model.User;

public class UserService {

   
    private UserDAO userDAO; 
   
    public void init() {

System.out.println("init");

}

public void add(User user) {

userDAO.save(user);

}

public UserDAO getUserDAO() {

return userDAO;

}

@Autowired

public void setUserDAO(@Qualifier("u") UserDAO userDAO) {//Qualifier:根据qualifier标签去查找,也可以根据name或者id去查找匹配的bean

this.userDAO = userDAO;

}

public void destroy() {

System.out.println("destroy");

}

}

对应的xml实现方式:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://www.springframework.org/schema/beans              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"             >

<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">       <property name="daoId" value="1"></property>      </bean>        <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">          <property name="daoId" value="2"></property>      </bean>         <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byType"><!-- 也可以设在beans标签上,好像是default-autowire scope="prototype"表示每次都创建一个新的实例对应的有singleton--只有一个实例 -->     </bean>

</beans>

@Resource

此标签也用于注入属性。相对来说用的比较多。

该标签需要导入J2EE的JAR包:common-annotations.jar

javax.annotation.Resource,默认byName,如果找不到对应name会去找对应type

@Resource

public void setUserDAO(UserDAO userDAO) { 
        this.userDAO = userDAO;

}

@Resource(name=”uuu”) //可以byName

以上两种注解虽然可以在代码中设置注入,但是还要求在xml文件中配置bean。

Component可以省去。

5.10.3 Using filters to customize scanning

By default, classes annotated with @Component, @Repository, @Service, @Controller, or a custom annotation that itself is annotated with @Component are the only detected candidate components. However, you can modify and extend this behavior simply by applying custom filters. Add them as include-filter or exclude-filter sub-elements of thecomponent-scan element. Each filter element requires the type and expression attributes. The following table describes the filtering options.

<?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.bjsxt"/>

</beans>

scan:扫描。表示扫描某个包和其自包,将能作为组件(添加了@Component)的内容解析出来。

package com.bjsxt.dao.impl;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;

import com.bjsxt.model.User;

@Component("u") //这样写表示这个类是一个组件,在另外一个类看来它就是一个资源。不指定它的key默认会是userDAOImpl(即类名首字母改成小写)

public class UserDAOImpl implements UserDAO {

public void save(User user) {

        //Hibernate

//JDBC

//XML

//NetWork

System.out.println("user saved!");

}

}

package com.bjsxt.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;

import com.bjsxt.model.User;

@Component("userService")

public class UserService {

private UserDAO userDAO; 
   
    public void init() {

System.out.println("init");

}

public void add(User user) {

userDAO.save(user);

}

public UserDAO getUserDAO() {

return userDAO;

}

@Resource(name="u")

public void setUserDAO( UserDAO userDAO) {

this.userDAO = userDAO;

}

public void destroy() {

System.out.println("destroy");

}

}

额外的一些内容:

<bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype">

scope对应的注解是@Scope(“”)

init-method对应的注解是@PostConstruct,表示构造完成之后再执行它注解的方法。

destroy-method对应的注解是@PreDestroy,容器销毁之前。

Spring 常用注入注解(annotation)和其对应xml标签的更多相关文章

  1. Spring 常用的注解

    目录 Spring 常用的注解 前言 SpringMVC配置 web配置 @ComponentScan @PropertySource @PropertySources @Value @Control ...

  2. Spring框架 全注解annotation不使用配置文件(SpringConfiguration.java类代替) 补充 xml配置文件没有提示解决

    全注解不使用配置文件 首先还是倒包 在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.b ...

  3. 关于Spring常用的注解

    参考文献:http://www.cnblogs.com/xdp-gacl/p/3495887.html 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationC ...

  4. spring常用的注解

    一.使用注解之前要开启自动扫描功能,其中base-package为需要扫描的包(含子包). <context:component-scan base-package="cn.test& ...

  5. Spring源码阅读笔记04:默认xml标签解析

    上文我们主要学习了Spring是如何获取xml配置文件并且将其转换成Document,我们知道xml文件是由各种标签组成,Spring需要将其解析成对应的配置信息.之前提到过Spring中的标签包括默 ...

  6. Spring源码阅读笔记05:自定义xml标签解析

    在上篇文章中,提到了在Spring中存在默认标签与自定义标签两种,并且详细分析了默认标签的解析,本文就来分析自定义标签的解析,像Spring中的AOP就是通过自定义标签来进行配置的,这里也是为后面学习 ...

  7. spring笔记--通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

  8. Spring依赖注入:注解注入总结

    更多11   spring   依赖注入   注解   java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...

  9. Spring常用注解式开发

    1.组件注册@Configuration.@Bean给容器中注册组件. 注解,@Configuration告诉Spring这是一个配置类,相当于bean.xml配置文件. 注解,@Bean给Sprin ...

随机推荐

  1. (二)shell变量

    (1)自定义变量 定义变量 变量名=变量值 变量名必须以字母或下划线开头,不能数字开头,区分大小写,ip=114.114.114.114 引用变量: $变量名 ${变量名} 查看变量: echo $变 ...

  2. mongodb复制集搭建

    注:mongodb当前版本是3.4.3 1.准备三个虚拟机做服务器 192.168.168.129:27017 192.168.168.130:27017 192.168.168.131:27017 ...

  3. HDU 6324.Problem F. Grab The Tree-博弈(思维) (2018 Multi-University Training Contest 3 1006)

    6324.Problem F. Grab The Tree 题目看着好难,但是题解说的很简单,写出来也很简单.能想出来就是简单的,想不出来就难(讲道理,就算是1+1的题目,看不出来就是难的啊). 和后 ...

  4. MYSQL从入门到放弃系列:mysql基础语法

    Mysql基本语法 启动MySQL net start mysql 连接与断开服务器 mysql -h 地址 -P 端口 -u 用户名 -p 密码 跳过权限验证登录MySQL mysqld --ski ...

  5. Vim求生

    [TOC] Vim 是从 vi 发展出来的一个文本编辑器.其代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用.和 Emacs 并列成为类 Unix 系统用户最喜欢的编辑器. —— ...

  6. tarjan算法与无向图的连通性(割点,桥,双连通分量,缩点)

    基本概念 给定无向连通图G = (V, E)割点:对于x∈V,从图中删去节点x以及所有与x关联的边之后,G分裂为两个或两个以上不相连的子图,则称x为割点割边(桥)若对于e∈E,从图中删去边e之后,G分 ...

  7. Coloring Dominoes

    问题 E: Coloring Dominoes 时间限制: 1 Sec  内存限制: 128 MB提交: 279  解决: 95[提交] [状态] [讨论版] [命题人:] 题目描述 We have ...

  8. mysql使用default来设置字段的默认值

    mysql创建表时,我们可以使用default来设置表字段的默认值,这样当向表中插入或添加数据时,如果没有为此字段设置任何值,则使用default默认值来填充该字段的值 在使用create table ...

  9. 【spfa】bzoj1295 [SCOI2009]最长距离

    题意:给你一个n*m的点阵.有些点是障碍,求一个欧几里得距离最大的点对(A,B),使得在移走的障碍≤T的情况下,可以从A走到B. 建图,跑n*m次spfa,求出从 每个点 出发到 其他所有点 的 经过 ...

  10. Problem Q: 多项式求和x+(x^2)/2!+(x^3)/3!+...

    #include<stdio.h> #include<math.h> int main() { float x,i; scanf("%f",&x); ...