Spring 引导 Vaadin7 集成 - 无法与 @Autowired 绑定
Spring Boot Vaadin7 Integration - Cannot bind with @Autowired
我创建了项目:http://start.spring.io/
我用 Maven 构建它,它是 运行.
所以我正在创建实体 Person:
@Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Temporal(TemporalType.TIMESTAMP)
private Date birthDay;
@NotNull(message = "Name is required")
@Size(min = 3, max = 50, message = "Name must be longer than 3 and less than 40 characters")
private String name;
private Boolean colleague;
private String phoneNumber;
@NotNull(message = "Email is required")
@Pattern(regexp = ".+@.+\.[a-z]+", message = "Must be valid email")
private String email;
//----- GETTERS AND SETTERS -----------
}
我正在为此实体创建存储库 PersonRepo:
public interface PersonRepo extends JpaRepository<Person, Long> {
@Override
<S extends Person> S save(S arg0);
@Override
long count();
}
这是我的用户界面 class:
@SpringUI
@Theme("valo")
public class MyUI extends UI {
private static final long serialVersionUID = 1L;
private final PersonRepo personRepo;
@Autowired
private MyUI(PersonRepo personRepo) {
this.personRepo = personRepo;
}
@Override
protected void init(VaadinRequest request) {
VerticalLayout layout = new VerticalLayout();
layout.setDefaultComponentAlignment(Alignment.MIDDLE_CENTER);
Person person = new Person();
person.setName("Person");
person.setEmail("Email");
person.setColleague(false);
person.setBirthDay(new Date());
personRepo.save(person);
Label helloWorld = new Label();
helloWorld.setValue("Persons: " + personRepo.count());
helloWorld.setStyleName(ValoTheme.LABEL_H1);
helloWorld.setSizeUndefined();
layout.addComponent(helloWorld);
setContent(layout);
}
}
这里我只@Autowire PersonRepo,创建新的Person 并保存在数据库中,而不是在Label 中显示Persons 的数量。但是 personRepo 为空,@Autowire 不工作。我不知道我的错误在哪里...
好吧@SpringBootApplication 只扫描同一包及以下的组件。这就是应用程序无法创建@Autowire 的原因。在我的示例中,每个 class 都在不同的包中。
如果我们将应用程序class放入顶级包中,我们可以解决这个问题,或者我们可以手动定义@ComponentScan,以扫描特定的包。
我创建了项目:http://start.spring.io/ 我用 Maven 构建它,它是 运行.
所以我正在创建实体 Person:
@Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Temporal(TemporalType.TIMESTAMP)
private Date birthDay;
@NotNull(message = "Name is required")
@Size(min = 3, max = 50, message = "Name must be longer than 3 and less than 40 characters")
private String name;
private Boolean colleague;
private String phoneNumber;
@NotNull(message = "Email is required")
@Pattern(regexp = ".+@.+\.[a-z]+", message = "Must be valid email")
private String email;
//----- GETTERS AND SETTERS -----------
}
我正在为此实体创建存储库 PersonRepo:
public interface PersonRepo extends JpaRepository<Person, Long> {
@Override
<S extends Person> S save(S arg0);
@Override
long count();
}
这是我的用户界面 class:
@SpringUI
@Theme("valo")
public class MyUI extends UI {
private static final long serialVersionUID = 1L;
private final PersonRepo personRepo;
@Autowired
private MyUI(PersonRepo personRepo) {
this.personRepo = personRepo;
}
@Override
protected void init(VaadinRequest request) {
VerticalLayout layout = new VerticalLayout();
layout.setDefaultComponentAlignment(Alignment.MIDDLE_CENTER);
Person person = new Person();
person.setName("Person");
person.setEmail("Email");
person.setColleague(false);
person.setBirthDay(new Date());
personRepo.save(person);
Label helloWorld = new Label();
helloWorld.setValue("Persons: " + personRepo.count());
helloWorld.setStyleName(ValoTheme.LABEL_H1);
helloWorld.setSizeUndefined();
layout.addComponent(helloWorld);
setContent(layout);
}
}
这里我只@Autowire PersonRepo,创建新的Person 并保存在数据库中,而不是在Label 中显示Persons 的数量。但是 personRepo 为空,@Autowire 不工作。我不知道我的错误在哪里...
好吧@SpringBootApplication 只扫描同一包及以下的组件。这就是应用程序无法创建@Autowire 的原因。在我的示例中,每个 class 都在不同的包中。
如果我们将应用程序class放入顶级包中,我们可以解决这个问题,或者我们可以手动定义@ComponentScan,以扫描特定的包。