简体   繁体   中英

how to configure spring and vaadin to inject my own beans into vaadin views?

Gys, help! I am going mad. I have spring + vaadin project generated with spring initialzr, which is based on spring boot. I want to have my backend and frontend functionality in the same project and to inject my beans into vaadin views with "Autowired annotation. Where i am wrong? I've created my context xml configuration file to describe my beans:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="ru.yudnikov.blog.backend.PostManager" id="postManager"/>
<bean class="ru.yudnikov.blog.backend.PostModel" id="postModel" scope="prototype"/>
</beans> 

i've declared Application class which implementns ApplicationContextAware (to place static link to my context) but when i've debugged method setApplicationContext(ApplicationContext applicationContext) i've seen that applicationContext is not empty. It contained delaration of vaadin beans, such as PostView, where i want to inject my Manager (controller) with "Authwired annotation. But if i would set centext as new classpath context from xml file, i'll lose views defenition. And i can't find a way to add my beans definitions into existing context. I've tried setParent() setClassLoader() and refresh() but it doesn't help. How to solve this? Should i try to "merge" this two context or what?

Use Vaadin Spring addon ( https://github.com/vaadin/spring ).

Here is example of main UI class (I'm using here javax Inject, but you can use Spring Autowired annotation):

@SpringUI
@Theme("new")
@Widgetset("com.example.NewWidgetSet")
public class NewUI extends UI implements Constants {
    private static final Logger logger = LoggerFactory.getLogger(NewUi.class);
    private CssLayout contentLayout;
    private SpringViewProvider viewProvider;

    @Inject
    public RessuUi(HeaderLayout headerLayout, SpringViewProvider viewProvider) {
        this.headerLayout = headerLayout;
        this.viewProvider = viewProvider;
    }

    @Override
    protected void init(VaadinRequest request) {
        initNavigator();
        initRootLayout(); // init here content
    }

    private void initNavigator() {
        Navigator newNavigator = new Navigator(this, contentLayout);
        newNavigator.addProvider(viewProvider);
        setNavigator(newNavigator);
    }

    @Override
    public void detach() {
        logger.debug("Detaching UI.");
    }

}

Example of View class:

@SpringView(name = UserFormView.NAME)
public class UserFormView extends VerticalLayout implements View {

    private MyService myService;

    @Autowired
    public UserFormView(MyService myService) {
        this.myService = myService;
    }

}

You also need to create applicationContext.xml in main/webapp/WEB-INF directory:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="com.mypackage.to.scan"/>
</beans>

Create also MainConfiguration class:

@Configuration
@EnableVaadin
@ComponentScan
@PropertySource("classpath:application.properties")
public class MainConfiguration {

}

Add also web servlet:

@Slf4j
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = true, ui = NewUi.class, widgetset = "fi.ssm.ressu.NewWidgetSet",
    heartbeatInterval = 60, closeIdleSessions = true)
public class NewServlet extends SpringVaadinServlet {

}

You can read more at Vaadin Injection and Scopes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM