项目结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
代码实现
package com.example.demo.domain;
public interface IPerson {
void sayHello();
void drink();
}
package com.example.demo.domain;
public interface ICup {
void fillWater();
}
package com.example.demo.domain;
public class BeerCup implements ICup{
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void fillWater() {
System.out.println("使用"+this.color+"的啤酒杯子喝酒。");
}
}
package com.example.demo.domain;
public class ClassCup implements ICup{
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void fillWater() {
System.out.println("使用"+this.color+"的玻璃杯子喝果汁。");
}
}
package com.example.demo.domain;
public class English implements IPerson{
private String name;
private ICup cup;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ICup getCup() {
return cup;
}
public void setCup(ICup cup) {
this.cup = cup;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println(this.name+"的母语是英语,会说英语!");
}
@Override
public void drink() {
// TODO Auto-generated method stub
cup.fillWater();
}
}
package com.example.demo.domain;
public class Russian implements IPerson{
private String name;
private ICup cup;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ICup getCup() {
return cup;
}
public void setCup(ICup cup) {
this.cup = cup;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println(this.name+"会说俄语!");
}
@Override
public void drink() {
// TODO Auto-generated method stub
cup.fillWater();
}
}
启动函数
package com.example.demo;
import com.example.demo.domain.English;
import com.example.demo.domain.IPerson;
import com.example.demo.domain.Russian;
// import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.BeanFactory;
// import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
IPerson anyone=((BeanFactory) act).getBean("beerman",Russian.class);
anyone.sayHello();
anyone.drink();
System.out.println("--------############---------");
anyone=((BeanFactory) act).getBean("ygman",English.class);
anyone.sayHello();
anyone.drink();
}
}
applicationContent.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
<bean id="dao" class="com.hw.dao.impl.StudentDaoImpl"></bean> -->
<bean id="beercup" class="com.example.demo.domain.BeerCup">
<property name="color" value="white"></property>
</bean>
<bean id="bolicup" class="com.example.demo.domain.ClassCup">
<property name="color" value="red"></property>
</bean>
<bean id="beerman" class="com.example.demo.domain.Russian">
<property name="name" value="heibu"></property>
<property name="cup" ref="beercup"></property>
<!-- <constructor-arg value="maozi" index="0"></constructor-arg> -->
<!-- <constructor-arg value="maozicss" index="1"></constructor-arg> -->
</bean>
<bean id="ygman" class="com.example.demo.domain.English">
<property name="name" value="英国人"></property>
<property name="cup" ref="bolicup"></property>
</bean>
</beans>
总结
建议以设值注入为主,构造注入为辅助。
下载地址:https://download.csdn.net/download/m0_61504367/85199838