@Autowired的作用和用法是什么?-创新互联

@Autowired 的作用是什么?
@Autowired 是一个注释,它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
@Autowired 默认是按照类去匹配,配合 @Qualifier 指定按照名称去装配 bean。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import blog.service.ArticleService;
import blog.service.TagService;
import blog.service.TypeService;@Controller
br/>@Controller
//成员属性字段使用 @Autowired,无需字段的 set 方法@Autowired
br/>@Autowired
//set 方法使用 @Autowired
private ArticleService articleService;@Autowired
br/>@Autowired
this.articleService = articleService;
}
//构造方法使用 @Autowired
private TagService tagService;@Autowired
br/>@Autowired
this.tagService = tagService; }
}
 @Autowired的用法
br/>}
}
 @Autowired的用法
<bean id="userDao" class="..."/>
<bean id="userService" class="...">
    <property name="userDao">
      <ref bean="userDao"/>    </property>
</bean>
这样你在userService里面要做一个userDao的setter/getter方法。
但如果你用了@Autowired的话,你只需要在UserService的实现类中声明即可。
br/>    </property>
</bean>
这样你在userService里面要做一个userDao的setter/getter方法。
但如果你用了@Autowired的话,你只需要在UserService的实现类中声明即可。
private IUserDao userdao;Spring@Autowired注解与自动装配
br/>Spring@Autowired注解与自动装配
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。
Boss 拥有 Office 和 Car 类型的两个属性:  
清单 3. Boss.java
package com.baobaotao;    
public class Boss {    
    private Car car;    
    private Office office;        // 省略 get/setter     
    @Override   
br/>    // 省略 get/setter     
    @Override   
        return "car:" + car + "/n" + "office:" + office;    
    }    
}    
  System.out.println必须实现toString方法
我们在 Spring 容器中将 Office 和 Car 声明为 Bean,并注入到 Boss Bean 中:下面是使用传统 XML 完成这个工作的配置文件 beans.xml:  
清单 4. beans.xml 将以上三个类配置成 Bean   
<?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 id="boss" class="com.baobaotao.Boss">    
        <property name="car" ref="car"/>    
        <property name="office" ref="office" />    
    </bean>    
    <bean id="office" class="com.baobaotao.Office">    
        <property name="officeNo" value="002"/>    
    </bean>    
    <bean id="car" class="com.baobaotao.Car" scope="singleton">    
        <property name="brand" value=" 红旗 CA72"/>    
        <property name="price" value="2000"/>    
    </bean>    
</beans>    
当我们运行以下代码时,控制台将正确打出 boss 的信息:  
清单 5. 测试类:AnnoIoCTest.java  
import org.springframework.context.ApplicationContext;    
import org.springframework.context.support.ClassPathXmlApplicationContext;    
public class AnnoIoCTest {    
    public static void main(String[] args) {    
        String[] locations = {"beans.xml"};    
        ApplicationContext ctx =     
            new ClassPathXmlApplicationContext(locations);    
        Boss boss = (Boss) ctx.getBean("boss");            System.out.println(boss);    
    }    
}    
这说明 Spring 容器已经正确完成了 Bean 创建和装配的工作。  
 2   @Autowired
br/>        System.out.println(boss);    
    }    
}    
这说明 Spring 容器已经正确完成了 Bean 创建和装配的工作。  
 2   @Autowired
要实现我们要精简程序的目的。需要这样来处理:
 在applicationContext.xml中加入:
<!-- 该 BeanPostProcessor 将自动对标注@Autowired 的 Bean 进行注入 -->      <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
Spring 通过一个BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。  
 修改在原来注入spirng容器中的bean的方法。     在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法
br/>     在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法
package com.baobaotao;    
import org.springframework.beans.factory.annotation.Autowired;    
public class Boss {        @Autowired   
br/>    @Autowired   
br/>    @Autowired   
    …    
}     
* 在applicatonContext.xml中 把原来 引用的<porpery >标签也去掉。
<?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">    
    <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->    
br/>    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">    
    <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->    
        AutowiredAnnotationBeanPostProcessor"/>    
    <!-- 移除 boss Bean 的属性注入配置的信息 -->     
    <bean id="boss" class="com.baobaotao.Boss"/>    
    <bean id="office" class="com.baobaotao.Office">    
        <property name="officeNo" value="001"/>    
    </bean>    
    <bean id="car" class="com.baobaotao.Car" scope="singleton">    
        <property name="brand" value=" 红旗CA72"/>    
        <property name="price" value="2000"/>        </bean>    
</beans>   
 这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。  
br/>    </bean>    
</beans>   
 这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。  
当然,您也可以通过 @Autowired 对方法或构造函数进行标注,如果构造函数有两个入参,分别是 bean1 和bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建CountryService Bean。来看下面的代码:  对方法
package com.baobaotao;    
public class Boss {    
    private Car car;    
    private Office office;         @Autowired    
br/>     @Autowired    
        this.car = car;        }    
    @Autowired   
br/>    }    
    @Autowired   
        this.office = office;        }    
    …    
}    
这时,@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:  
br/>    }    
    …    
}    
这时,@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:  
public class Boss {    
    private Car car;    
    private Office office;        @Autowired   
br/>    @Autowired   
        this.car = car;    
        this.office = office ;    
    }    
    …    
}    
由于 Boss() 构造函数有两个入参,分别是 car 和 office,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 Boss(Car car ,Office office) 的入参来创建 Boss Bean。

成都创新互联公司从2013年成立,是专业互联网技术服务公司,拥有项目网站建设、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元无锡做网站,已为上家服务,为无锡各地企业和个人服务,联系电话:18980820575

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

新闻标题:@Autowired的作用和用法是什么?-创新互联
文章分享:https://www.cdcxhl.com/article0/hejoo.html

成都网站建设公司_创新互联,为您提供搜索引擎优化网站设计公司App设计用户体验电子商务网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化