user2471976
user2471976

Reputation: 57

Spring autowire object with value

Suppose i have class named SomeClass which takes value in constructor and populates the field.

String text;
String sometext;

public someClass(String text, String sometext){
this.text = text;
this.sometext = sometext;
}

SomeClass has a method which creates a new object. In java when we create a new object we can instantiate with values like

ClassName variable = new ClassName(text, sometext);

and populate the fields in ClassName like this using constructor

public ClassName(String text, String sometext){
this.text = text;
this.sometext = sometext;
}

But in spring using autowired how can we do that?

@Autowired
public ClassName(SomeClass someClass){
this.text = someClass.text;
this.sometext = someClass.sometext;
}

This wont work. how spring will know which the instance of SomeClass


UPDATE:

I was wrong. i was not thinking in DI way. instead of autowiring SomeClass. i had to autowire ClassName.

Without DI i created a new object in a method of class ClassName

When using DI i had to autowire in the method of the class ClassName

@Autowired
ClassName className;

I can populate the fields directly making the fields public

className.text = text;
className.sometext = sometext;

and i can do using javabeans. but how to do via constructor.

NOTE: there is nothing wrong with spring config. base scan is enabled.

Upvotes: 1

Views: 4371

Answers (2)

Essex Boy
Essex Boy

Reputation: 7940

You can not use new with Spring, everything has to be created in the spring context. In your example your spring config would be

<?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"
    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">

    <bean class="SomeClass">
        <constructor-arg index="0" name="text" value="some value" />
        <constructor-arg index="1" name="sometext" value="some other value" />
    </bean>

</beans>

Your code woud be

@Autowired
private SomeClass someClass;

Upvotes: 2

Sajib Acharya
Sajib Acharya

Reputation: 1733

You are right in the sense that Spring will not automatically know of all the classes or create the required beans. Spring does not work like magic. We can use annotations but we still need to tell Spring which beans to create and how to create them. That is where the @Component, @Service, @Repository, @Controller comes into play. have a look here. When you want SomeClass to be a dependency in ClassName, you have to explicitly let Spring know that a bean of SomeClass needs to be created before it can be injected as a dependency in ClassName, and you do that by annotating SomeClass with the correct annotation. Also you need to do a <context:component-scan base-package="com.your.package"/> for Spring to resolve all the annotations correctly before it can autowire dependencies.

Moreover, you have to realise the fact that if your SomeClass constructor arguments depend on dynamic values, you won't be able to create the bean right away, and you would not be able to inject it as a dependency in ClassName using @Autowired, since spring needs to know those values during deploy time. If that is the case, I'd prefer using getter setter methods instead in ClassName for an instance of SomeClass.

Note: This is answer is taking into consideration about annotations in Spring. You can also do the same by defining Spring Beans in XML.

Upvotes: 4

Related Questions