我从春天开始。我得到了一个提供的@ RestController-Application,我知道我得到了它。现在我尝试使用POJO并且无法使@Autowire工作。 这是我的环境:
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;
}
}
守则
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();
}
}
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>
bean&#34;用户&#34;永远不会变成实例化,即使是明确定义bean(现在是注释)。有人看到了,这里有什么不对吗?非常感谢