3

I am new to Spring and I have the following problem: I have the following bean:

<bean id="lastName" class="java.lang.String"> <constructor-arg value="Johnson"/> </bean>

Then, in my class RegisterPerson, I have the following

@Configurable
public class RegisterPerson
   @Resource(name="lastName")
   private String lastName;

then in my method displayName(){ System.out.println(lastName) }

lastName is null.

I looked for some examples, but couldn't find anything similar to my problem. Any advice or suggestions will be appreciated.

Thanks!

user3603634
  • 95
  • 2
  • 11

2 Answers2

2

The best way to inject String into your class is declare properties configurer in your Spring XML: something like this:

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:/cfg/yourPropsFile.properties</value>
        </list>
    </property>
</bean>

If you are using annotations make sure that you have these lines in your spring file:

<context:annotation-config/>
<context:component-scan base-package="com.your.app"/>

Then just add that string you want to be injected in your spring bean into that file, for e.g.:

my.prop=Johnson

Then inject it in your controller using @Value annotation:

@Value("${my.prop}")
private String lastName;

This is assuming that your value needs to be configurable.

Otherwise if you need to inject String value as you are trying to do now, please note that by default using @Configurable annotation autowiring won't work. To enable autowiring you need to enable it using: @Configurable(autowire = Autowire.BY_NAME), but it shouldn't be used unless you really need it. @Configurable annotation is mostly used with AspecJ, which is not what you are trying to achieve here.

So define your class with @Component and using:

@Autowired 
@Qualifier("lastName")
private String lastName;

will solve your problem.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
1

You should use Autowired and Qualifier annotation..

@Autowired 
@Qualifier("lastName")
private String lastName;

Try this configuration

public class Test {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("app-context.xml");
        String s = (String) context.getBean("lastName");
    }
} 
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • It still says it is null. Is there any other way to declare something in the spring.xml as a bean and then to get its value in a class? All I need is to pass this string from the spring.xml to the class RegisterPerson – user3603634 Sep 05 '14 at 12:49
  • You could have a problems if RegisterPerson will be creates by new statement. – Xstian Sep 05 '14 at 12:52
  • Yes, I have in another method: return new RegisterPerson(). Is there a workaround? – user3603634 Sep 05 '14 at 12:55
  • see this [link](http://stackoverflow.com/a/25640125/3364187), anyway @Resource don't perform a spring injection. – Xstian Sep 05 '14 at 12:56
  • I saw the link and I added @Configurable to the class: so now the first 4 lines are: `@Configurable public class RegisterPerson{ @Autowired @Qualifier("lastName) private String lastName; ` but it still tells me lastName is null. – user3603634 Sep 05 '14 at 13:05
  • Have you used this config to enable @Configurable? see this [link](http://vikdor.blogspot.it/2012/08/using-configurable-to-inject-resources.html) – Xstian Sep 05 '14 at 13:12