Spring设值注入和构造注入(通过xml)

项目结构

在这里插入图片描述

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

汪程序猿

就当请我吃顿饭

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

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

打赏作者

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

抵扣说明:

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

余额充值