Spring@Autowired注解与自动装配

来源:互联网 发布:求一款网络休闲游戏 编辑:程序博客网 时间:2024/06/16 02:36

参考链接: http://blog.csdn.net/heyutao007/article/details/5981555

参考链接:http://blog.csdn.net/topwqp/article/details/8681467

参考链接:http://sishuok.com/forum/blogPost/list/2447.html


建立maven项目,添加依赖


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>4.0.0.Release</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>    <version>4.0.0.Release</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-beans</artifactId>    <version>4.0.0.Release</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-jdbc</artifactId>    <version>4.0.0.Release</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context-support</artifactId>    <version>4.0.0.Release</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-jms</artifactId>    <version>4.0.0.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-aop</artifactId>    <version>4.0.0.Release</version></dependency>


Boss类

package com.eastcom.first.spark.data.spring.autowired;import org.springframework.beans.factory.annotation.Autowired;public class Boss {@Autowiredprivate Car car;@Autowiredprivate Office office;public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}public Office getOffice() {return office;}public void setOffice(Office office) {this.office = office;}@Overridepublic String toString() {return "car:" + car + "\n\n" + "office:" + office;}}

Car类

package com.eastcom.first.spark.data.spring.autowired;public class Car {private String brand;private String price;public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}@Overridepublic String toString() {return "Car [brand=" + brand + ", price=" + price + "]";}}

public class Office {private String officeNo;public String getOfficeNo() {return officeNo;}public void setOfficeNo(String officeNo) {this.officeNo = officeNo;}@Overridepublic String toString() {return "Office [officeNo=" + officeNo + "]";}}


AnnoIoCTest


package com.eastcom.first.spark.data.spring.autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AnnoIoCTest {public static void main(String[] args) {// String confDir =// "file:D:/newworkspace/my-study-spark/config/base.xml";String confDir = "file:D:/newworkspace/my-study-spark/config/base2.xml";String[] locations = { confDir };ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);Boss boss = (Boss) ctx.getBean("boss");System.out.println(boss);((AbstractApplicationContext) ctx).registerShutdownHook();}}

建立base2.xml文件

<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:util="http://www.springframework.org/schema/util" xmlns:lang="http://www.springframework.org/schema/lang"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/context                         http://www.springframework.org/schema/context/spring-context.xsd                        http://www.springframework.org/schema/beans                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                         http://www.springframework.org/schema/lang                        http://www.springframework.org/schema/lang/spring-lang-3.0.xsd                        http://www.springframework.org/schema/util                        http://www.springframework.org/schema/util/spring-util-3.0.xsd                        http://www.springframework.org/schema/task                         http://www.springframework.org/schema/task/spring-task-3.0.xsd"default-init-method="init" default-destroy-method="cleanUp"><!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/><!-- 引入属性文件   --><context:property-placeholder location="file:D:/newworkspace/my-study-spark/config/application.properties" /> <!-- id 表示你这个组件的名字,class表示组件类 -->  <bean id="hello" class="com.eastcom.first.spark.data.spring.HelloImpl">   <property name="message" value="${message}"/>          <property name="index">              <value>${index}</value>          </property>  </bean>  <bean id="office" class="com.eastcom.first.spark.data.spring.autowired.Office">             <property name="officeNo" value="002"/>     </bean>             <bean id="car" class="com.eastcom.first.spark.data.spring.autowired.Car" scope="singleton">             <property name="brand" value=" 红旗 CA72"/>             <property name="price" value="2000"/>         </bean>     <bean id="boss" class="com.eastcom.first.spark.data.spring.autowired.Boss">             <property name="car" ref="car"/>             <property name="office" ref="office" />         </bean>         </beans>  

属性文件

message="this is a message,we like spring"index=3

运行结果

 begin init car:nulloffice:null end cleanUp