使用ref有什么前提条件么 ,为什么 我报错
先不排错了,你们写一个完整的调用其它bean的例子,我先弄明白 到底是怎么调用 的,然后再进行排错
package gouzao_zhuru;
public class User {
private String name;
private Integer age;
private char sex;
public User(String name, Integer age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void Out() {
System.out.println("name: " + name);
System.out.println("age: " + age);
System.out.println("sex: " + sex);
}
}
package kongzhiqi_zhuru;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import gouzao_zhuru.User;
public class Main{
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("UserBean_gouzao_zhuru.xml");
User user = (User)applicationContext.getBean("User1");
user.Out();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="User" class="gouzao_zhuru.User">
<constructor-arg>
<value>张三</value>
</constructor-arg>
<constructor-arg>
<value>20</value>
</constructor-arg>
<constructor-arg>
<value>女</value>
</constructor-arg>
</bean>
<bean id="User1" class="kongzhiqi_zhuru.Main">
<property name="User" ref="User"></property>
</bean>
</beans>
三月 23, 2023 10:03:53 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4783da3f: startup date [Thu Mar 23 10:03:53 CST 2023]; root of context hierarchy
三月 23, 2023 10:03:53 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [UserBean_gouzao_zhuru.xml]
三月 23, 2023 10:03:54 上午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'User1' defined in class path resource [UserBean_gouzao_zhuru.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'name' of bean class [kongzhiqi_zhuru.Main]: Bean property 'name' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'User1' defined in class path resource [UserBean_gouzao_zhuru.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'name' of bean class [kongzhiqi_zhuru.Main]: Bean property 'name' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1639)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1354)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at kongzhiqi_zhuru.Main.main(Main.java:10)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'name' of bean class [kongzhiqi_zhuru.Main]: Bean property 'name' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:243)
at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:426)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:266)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1635)
... 13 more
你直接 获取 User 的Bean
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("UserBean_gouzao_zhuru.xml");
User user = (User)applicationContext.getBean("User");
user.Out();
下面这个Main的Bean配置,去除掉, Main是主类,用于你自测运行,无需添加配置
另外调用指定的带参构造方法给属性注入值,参考如下:
<bean id="" class="">
<constructor-arg index="" name="" value="" ref="">
<value></value>
</constructor-arg>
</bean>
--- 写了个测试demo:
package com.demo.bean;
/**
* @author huazie
* @version 2.0.0
* @since 2.0.0
*/
public class User {
private String name;
private Integer age;
private char sex;
public User(String name, Integer age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void printInfo() {
System.out.println("name: " + name);
System.out.println("age: " + age);
System.out.println("sex: " + sex);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
}
package com.demo.bean;
/**
* @author huazie
* @version 2.0.0
* @since 2.0.0
*/
public class Person {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
import com.demo.bean.Person;
import com.demo.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author huazie
* @version 2.0.0
* @since 2.0.0
*/
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) applicationContext.getBean("User");
user.printInfo();
Person person = (Person) applicationContext.getBean("Person");
person.getUser().printInfo();
}
}
<bean id="User" class="com.demo.bean.User">
<constructor-arg>
<value>张三</value>
</constructor-arg>
<constructor-arg>
<value>20</value>
</constructor-arg>
<constructor-arg>
<value>女</value>
</constructor-arg>
</bean>
<bean id="Person" class="com.demo.bean.Person">
<property name="user" ref="User"></property>
</bean>
运行结果:
下面那一段main类没有name成员,所以不能这样设置,直接调用上面的user就行.去掉main试一下
<bean id="User" class="gouzao_zhuru.User">
<constructor-arg name="name" value="张三"/>
<constructor-arg name="age" value="12"/>
<constructor-arg name="sex" value="女">
</bean>
试试
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
问题分析:
首先我们从报错信息来看:Invalid property 'name' of bean class [kongzhiqi_zhuru.Main],发现是主类的 name 属性被错误引用了,不是我们想要的原因。其次,我们根据 xml 配置文件中的代码,发现我们在应用上下文中定义了一个 User1 的 bean,并将其 class 设置为 Main,这理解起来就像是在将一个 Main 类的实例注入到 User1 中。因此我们在 Main 类中必须定义一个与 User1 的 bean 对应的 setter 方法。
解决方法:
我们需要修改 Main 类中的属性,使其能接受并运行 User 的实例。修改代码如下:
public class Main{
private User User;
public kongzhiqi_zhuru.User getUser() {
return User;
}
public void setUser(kongzhiqi_zhuru.User user) {
User = user;
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("UserBean_gouzao_zhuru.xml");
Main main = (Main)applicationContext.getBean("User1");
User user = main.getUser();
user.Out();
}
}
在 User1 的 bean 中添加:
<property name="User" ref="User" />
这里的 name 属性为 Main 中的属性名,ref 属性指向被依赖的 User bean。
完整代码:
javaUser类
package gouzao_zhuru;
public class User {
private String name;
private Integer age;
private char sex;
public User(String name, Integer age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void Out() {
System.out.println("name: " + name);
System.out.println("age: " + age);
System.out.println("sex: " + sex);
}
}
javaMain类
package kongzhiqi_zhuru;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import gouzao_zhuru.User;
public class Main{
private User User;
public kongzhiqi_zhuru.User getUser() {
return User;
}
public void setUser(kongzhiqi_zhuru.User user) {
User = user;
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("UserBean_gouzao_zhuru.xml");
Main main = (Main)applicationContext.getBean("User1");
User user = main.getUser();
user.Out();
}
}
xml文件
```xml
<beans
xmlns:aop="http://w/