

Define a public bean variable in the spring without using a setter
source link: https://www.codesd.com/item/define-a-public-bean-variable-in-the-spring-without-using-a-setter.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Define a public bean variable in the spring without using a setter
I read on a few websites that with Spring its possible to do setter based Dependency Injection without having to create a setter for the injected variable it. Would nicely tidy up the code. I read this on another site and also here on stackoverflow.
I've tried it but in my case it does not work. I'm using 3.2.0.RELEASE. I'm getting the following error.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DI_without_setter' defined in class path resource [SpringBeans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'url' of bean class [net.comsys.springpropstest.DiWithoutSetter]: Bean property 'url' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
my Java test code
package net.xxx.springpropstest;
public class DiWithoutSetter {
private String url;
}
I've just added it to my main code. Not displayed here. I don't even use DiWithoutSetter in the main code.
SpringBeans.xml
<beans 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-3.0.xsd">
<bean id="DI_without_setter" class="net.xxx.springpropstest.DiWithoutSetter">
<property name="url" value="jan" />
</bean>
If someone could shed some light on his issue that would be appreciated. Is it possible to set the value of a (public) variable in a Spring bean without using a setter method?
You can't do that (you need a setter), and that tutorial appears to be wrong (note in the example source zip, the classes have getters and setters).
Either add a setter or @Inject
the field implicitly using autowiring.
Recommend
-
37
每日前端夜话 0x92 每日前端夜话,陪你聊前端。 每天晚上18:00准时推送。 正文共:1602 字 预计阅...
-
12
getter 和setter 子类中为什么要写get和set方法获取释放数据,不写会怎样?在什么情况下要用getter和setter? getter通常与一个私有的实例变量对应,用于返回该变量的值
-
13
PostCSS Custom CSS Units Define custom css unit and convert them to CSS variable. Input: :root { --cusomt-base-unit: 1vw; } div { width: 100rpx; } Output: div { width: c...
-
8
How to define a variable in Go depending on the operating system advertisements I'm aware that I can name particular go fil...
-
9
Swap two variables without using third variable in Java In the current post, we will discuss/write a code to swap two variables without using third/temporary variable. The below first example is to swap two integ...
-
9
Define A String Variable in LaTeX 2022-03-14 LaTeX 220 words 2 mins read 42 times read Like other programming languages, the...
-
4
C Program to Swap 2 Numbers without using a Temporary VariableC Program to Swap 2 Numbers without using a Temporary VariableHello friends. Welcome back to Geeks for means.
-
4
Spring 01: Spring配置 + IOC控制反转 + Setter注入 ...
-
8
Setter Injection in Spring with ExampleSkip to content
-
8
What happens if I define one environment variable in terms of the value of another environment variable?
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK