Spring@Autowired注解与自动装配

参考链接: 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 {
	@Autowired
	private Car car;
	@Autowired
	private 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;
	}

	@Override
	public 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;
	}

	@Override
	public 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;
	}

	@Override
	public 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:null

office:null
 end cleanUp 





















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值