I'm starting with Spring. I got to work with a provided @RestController-Application and I thougt I got it. Now I try to work with a POJO and cannot get @Autowire working. This is my environment:
The Bean:
package mybeans;
import org.springframework.stereotype.Repository;
@Repository
public class User {
private String surname;
private String lastname;
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
The Code
package mycode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import mybeans.User;
@Component
public class Main {
private User user;
@Autowired
public void setUser(User user) {
this.user=user;
}
public Main() {
System.out.println("autowired user is instantiated true/false: " + !(user==null));
}
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
// User user=(User) context.getBean("user");
// System.out.println("explicit user is instantiated true/false: " + !(user==null));
new Main();
}
}
The Context-xml
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- enable autowire -->
<context:annotation-config />
<context:component-scan base-package="mybeans" />
<!--
<bean id="user" class="mybeans.User" autowire="byName">
<property name="surname" value="Ulrich" />
<property name="lastname" value="Schmidt" />
</bean>
-->
</beans>
The bean "user" never becomes instantiated, even when exlicit defining the bean (is a comment now). Does anybody see, whats wrong here? Thanks a lot