实现“设置注入”Spring示例 ——难点:实现逻辑有点绕

本文通过创建一个Maven Java项目,引入JUnit和Spring框架的依赖,实现了Brand、Ford、Golf接口与类,以及Car类。在CarApp中使用Spring IoC容器管理Bean,通过XML配置文件进行依赖注入,展示了如何从容器获取并打印不同品牌汽车的品牌名称。
摘要由CSDN通过智能技术生成

1、创建一个Maven JAVA项目: exp6_setjection;

2、在pom.xml中配置

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>5.0.9.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>5.0.9.RELEASE</version>
	</dependency>
  </dependencies>

 

3、实现Brand.java、Ford.java、Golf.java(参考示例文件)

Golf:

package org.exp6_setjection;

public class Golf implements Brand {

	public String brand() {
		// TODO Auto-generated method stub
		return ("高尔夫");
	}

}

 Ford:

package org.exp6_setjection;

public class Ford implements Brand {

	public String brand() {
		// TODO Auto-generated method stub
		return ("福特");
	}
}

Brand: 

package org.exp6_setjection;

public interface Brand {
	public String brand();
}

4、实现Car.java;

ps:注意这里的getBrand方法里,不能直接用生成的函数代码,要用brand类调用brand()方法

package org.exp6_setjection;
import org.exp6_setjection.Brand;
public class Car {
	private Brand brand;

//	代码实现:添加getBrand()方法和setBrand()方法
	public String getBrand() {
		return brand.brand();
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
}

5、实现CarApp.java

package org.exp6_setjection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class CarApp {
	public static void main( String[] args )
    {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
		//代码实现:添加业务逻辑代码
	    Car car = null;  
	    car = (Car) ctx.getBean("carGolf");    
	    System.out.println(car.getBrand());
	    car = (Car) ctx.getBean("carFord");    
	    System.out.println(car.getBrand());
    }
}

6、在src目录下配置applicationContext.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"
	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.1.xsd">
	<!--定义第一个Bean,注入Car类对象-->
	<bean id="carGolf" class="org.exp6_setjection.Car">
		<!--property元素用来指定需要容器注入的属性,依赖注入golf-->
		<property name="brand" ref="golf"></property>
	</bean>
	<!--定义第2个Brand,注入Golf类对象-->
	<bean id="golf" class="org.exp6_setjection.Golf"></bean>
	
	<!--定义第一个Bean,注入Car类对象-->
	<bean id="carFord" class="org.exp6_setjection.Car">
		<!--property元素用来指定需要容器注入的属性,依赖注入golf-->
		<property name="brand" ref="ford"></property>
	</bean>
	<!--定义第2个Brand,注入Golf类对象-->
	<bean id="ford" class="org.exp6_setjection.Ford"></bean>	
</beans>

7、运行程序CarApp。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

念衢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值