m0_50791220
2021-09-16 22:07
采纳率: 0%
浏览 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条回答 默认 最新

相关推荐 更多相似问题