Spring @Autowired 用法

来源:互联网 发布:刀卡 淘宝 编辑:程序博客网 时间:2024/06/05 20:00

1:在set 上 或者  对象上  添加注解 如:



package com.bb.com;import org.springframework.beans.factory.annotation.Autowired;public class A { B b; @Autowired C c; public A (){ System.out.println("加载AAA"); }public B getB() {return b;}@Autowiredpublic void setB(B b) {this.b = b;}public C getC() {return c;}public void setC(C c) {this.c = c;}}



注意导包:import org.springframework.beans.factory.annotation.Autowired;


2:在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"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"><bean id="b" class="com.bb.com.B"/><bean id="c" class="com.bb.com.C"/> <bean id="abc" class="com.bb.com.A">  <property name="cc" ref="c"/> </bean> <bean   class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/></beans>



3:目录结构:(下图)



原创粉丝点击