急需学习 2021-09-16 22:07 采纳率: 25%
浏览 34

spring 注解与构造同时使用时,两者发生了冲突,构造值成了注解的值

求大 神解答一下,不能同时用还是哪里弄错了?

测试文件


```java
package com.itheima.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationAssembleTest {
    public static void main(String[] args) {
        String xmlPath = "com/itheima/annotation/beans6.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        System.out.println(applicationContext.getBean("Corp2"));
        System.out.println(applicationContext.getBean("Corp1"));
        System.out.println(applicationContext.getBean("corp"));
    }
}


Crop bean文件

package com.itheima.annotation;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

@Repository
public class Corp {
    @Value("注解注入")
    private String corpName;
    //@Resource(name = "Employee")
    private List<Employee> staff;

    public void setcorpName(String corpName) {
        this.corpName = corpName;
    }

    public void setStaff(List<Employee> staff) {
        this.staff = staff;
    }

    public Corp(String corpName, List<Employee> staff) {
        super();
        this.corpName = corpName;
        this.staff = staff;
    }

    public Corp() {
        super();
        
    }

    @Override
    public String toString() {
        return "Crop [corpName=" + corpName + ", staff=" + staff + "]";
    }

}


Employee bean文件

package com.itheima.annotation;

import org.springframework.stereotype.Repository;

@Repository
public class Employee {
    private String name;
    private String workNumber;

    public void setName(String name) {
        this.name = name;
    }

    public void setWorkNumber(String workNumber) {
        this.workNumber = workNumber;
    }

    public Employee(String name, String workNumber) {
        super();
        this.name = name;
        this.workNumber = workNumber;
    }

    public Employee() {
        super();

    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", workNumber=" + workNumber + "]";
    }

}


beans.xml


```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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util.xsd">

    <util:list id="Employee">
        <bean id="Employee1" class="com.itheima.annotation.Employee">
            <constructor-arg index="0" value="张三" type="String" />
            <constructor-arg index="1" value="0000001"
                type="String"></constructor-arg>
        </bean>
        <bean id="Employee2" class="com.itheima.annotation.Employee">
            <constructor-arg index="0" value="李四" />
            <constructor-arg index="1" value="0000002" />
        </bean>
    </util:list>

    <!-- 使用构造注入方式 -->
    <bean id="Corp1" class="com.itheima.annotation.Corp">
        <constructor-arg index="0" value="构造注入" />
        <constructor-arg index="1" ref="Employee" />
    </bean>

    <!-- 使用设值注入方式 -->
    <bean id="Corp2" class="com.itheima.annotation.Corp">
        <property name="corpName" value="设值注入"></property>
        <property name="staff" ref="Employee" />
    </bean>
    <context:component-scan
        base-package="com.itheima.annotation" />
</beans>

测试结果 :构造值与注解值相同

Crop [corpName=设值注入, staff=[Employee [name=张三, workNumber=0000001], Employee [name=李四, workNumber=0000002] ] ]
Crop [corpName=注解注入, staff=[Employee [name=张三, workNumber=0000001], Employee [name=李四, workNumber=0000002] ] ]
Crop [corpName=注解注入, staff=[Employee [name=张三, workNumber=0000001], Employee [name=李四, workNumber=0000002] ] ]
  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 修改了问题 9月16日
  • 创建了问题 9月16日

悬赏问题

  • ¥15 请问Quartus的Verilog代码怎么写?
  • ¥18 光催化第一性原理计算析氢效率STH怎么计算
  • ¥100 Mac 版foxmail 收邮件问题
  • ¥15 QWebEngineView
  • ¥15 如何使用shufflenet进行手写数字识别
  • ¥15 .net core 同时编辑怎么防止数据串了
  • ¥20 微信小程序播放直播流
  • ¥15 关于迷宫自走单片机循迹小车的知识
  • ¥15 python使用selenium工具爬取网站的问题
  • ¥15 visual studio中c语言用ODBC链接SQL SERVER