zoukankan      html  css  js  c++  java
  • Spring@Autowired注解

    @Autowired注解可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
    注意:@Autowired默认是按照类型来注入的。
    看下面的例子:例子是以对成员变量(field)为例进行的

    public class Person {
        private String name;
        private String address;
        private int age;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
    
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getAddress() {
            return address;
        }
    
        @Override
        public String toString() {
            return "[name:"+getName()+
                    ",address:" + getAddress()+
                    ",age:" +getAge()+
                    "]";
        }

    另外一个Customer类有一个Person类型的成员,现用@Autowired注入:

    public class Customer
    {
        @Autowired
        private Person person;
        private int type;
        private String action;
    
        public String getAction() {
            return action;
        }
    
        public void setAction(String action) {
            this.action = action;
        }
    
        public int getType() {
            return type;
        }
    
        public void setType(int type) {
            this.type = type;
        }
    
        @Override
        public String toString() {
            return "[Type:"+getType()+
                    ",action:"+getAction()+
                    ","+"person:"+
                    person.toString();
        }
    }

    在配置文件中加入

    <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">
        <!--@Autowired-->
        <context:annotation-config/>
        <bean id="CustomerBean" class="com.mkyong.common.Customer">
            <property name="action" value="buy" />
            <property name="type" value="1" />
        </bean>
    
        <bean id="PersonBean" class="com.mkyong.common.Person">
            <property name="name" value="mkyong" />
            <property name="address" value="address ABC" />
            <property name="age" value="29" />
        </bean>
    </beans>

    测试程序:

    public class Main 
    {
        public static void main( String[] args )
        {
            ApplicationContext context = 
              new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
    
            Customer customer  = (Customer)context.getBean("CustomerBean");
            System.out.println(customer);
        }
    }

    @Autowired默认情况下,总是认为field是可以成功注入的,如果没有成功注入(例如没有匹配的类型)则会跑出异常,如果要使得在没有成功注入的情况下不抛出异常,那么可以和required属性一起使用,将required的属性设置为false:

    public class Customer 
    {
        **@Autowired(required=false)**
        private Person person;
        private int type;
        private String action;
        //getter and setter methods
    }

    @Autowired默认是按照类型来匹配的,那么当容器中有两个相同类型的bean时怎样区分要注入是哪一个呢?这时候@Qualifier注解便起作用了:

     <bean id="person1" class="com.hzsunpeng.Person">
            <property name="name" value="name1"/>
            <property name="address" value="adderss1"/>
            <property name="age" value="23"/>
        </bean>
        <bean id="person2" class="com.hzsunpeng.Person">
            <property name="name" value="name2"/>
            <property name="address" value="adderss2"/>
            <property name="age" value="22"/>
        </bean>

    这时候怎样决定要注入person1呢还是person2呢?

    如果想注入person1,那么可以这样做:

    public class Customer
    {
        @Autowired
        @Qualifier("person1")
        private Person person;
        private int type;
        private String action;
        //setter and getter
    }

    最后注意一个细节:如果使用了springMVC自动扫描组件(@Component或者是@Service等),在配置文件中加入了

    <context:component-scan base-package="***.***.***" />

    那么配置文件中的

    <context:annotation-config/>

    可以省略。

  • 相关阅读:
    如何定时备份Mysql数据库
    Mysql如何分析慢查询及优化(一)--- EXPLAIN详解
    Mysql如何分析慢查询日志--MysqlDumpSlow详解
    Mysql添加用户及用户权限管理
    Mysql表的横向拆分与纵向拆分及简单案例
    Mysql触发器详解以及简单的案例
    Mysql存储过程详解及案例
    Mysql中如何查看线程
    Nginx命令与配置详解
    SQLSERVER 2008 技术内幕 T-SQL查询 笔记1: SQL 执行顺序
  • 原文地址:https://www.cnblogs.com/sunp823/p/5601400.html
Copyright © 2011-2022 走看看