2016-11-28 3 views
1

Я создал проект с: http://start.spring.io/ Я строю его с maven и бегом.Spring Boot Vaadin7 Интеграция - не удается связать с @Autowired

Так я создаю объект 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 = "[email protected]+\\.[a-z]+", message = "Must be valid email") 
private String email; 

//----- GETTERS AND SETTERS ----------- 
} 

Для этого объект Im создания хранилища PersonRepo:

public interface PersonRepo extends JpaRepository<Person, Long> { 

@Override 
<S extends Person> S save(S arg0); 

@Override 
long count(); 
} 

Вот myUI класс:

@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, создавая нового человека и сохраняя в db, чем в Label. Я показываю количество людей. Но personRepo имеет значение null, @Autowire не работает. Я не знаю, где моя ошибка ...

ответ

0

Well @SpringBootApplication просматривает только компоненты, которые находятся на одном и том же пакете и ниже. Вот почему приложение не может сделать @Autowire. В моем примере каждый класс находится в другом пакете.

Это можно исправить, если мы поместим класс Application в верхний пакет или можем вручную определить @ComponentScan для сканирования конкретного пакета.

Смежные вопросы