0

Labels from DB using Spring Boot

asked 2021-09-16 20:38:57 +0800

FGabbr gravatar image FGabbr
3 1

I'm using ZK 6.5 and trying to load labels from DB using SpringBoot, but the DI does not seem to work inside the LabelLocator (instantiated by a WebAppInit listener)

What is the correct way? This gives NullPointer:

@Slf4j
@Transactional
@Service
class DBLabelLocator implements LabelLocator2 {

    @Autowired
    LabelService labelService

    InputStream locate(Locale locale) {
        StringBuilder data = new StringBuilder()
        for (MyLabel label : labelService.listAll())
            data.append(label.id).append("=").append(label.value).append("\n")
        log.info("Labels: \n {}", data.toString())
        return new ByteArrayInputStream(data.toString().getBytes(getCharset()))
    }
delete flag offensive retag edit

1 Answer

Sort by » oldest newest most voted
0

answered 2021-09-17 10:14:17 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2021-09-17 10:15:22 +0800

The question is: How do you instantiate the DBLabelLocator inside your WebAppInit?

Annotations don't do anything by themselves. So you need to ask Spring to create the bean for you. E.g. (pseudo code, not tested just highlighting the principle)

in your WebAppInit:

public void init(WebApp wapp) {
    // get spring's application context
    ApplicationContext ctx = WebApplicationContextUtils
        .getRequiredWebApplicationContext(wapp.getServletContext());

    // ask for a bean by type
    LabelLocator2 dbLabelLocator = ctx.getBean(DBLabelLocator.class);

    // register the locator with ZK
    Labels.register(dbLabelLocator);
}

if you just say new DBLabelLocator() spring won't process the annotations @Service/@Transactional/@Autowired. However you forgot to provide that code so my initial guess might just be wrong ... please specify the details if needed.

link publish delete flag offensive edit

Comments

class WebAppInit implements org.zkoss.zk.ui.util.WebAppInit {

    @Autowired
    DBLabelLocator dbLabelLocator

    @Override
    void init(WebApp webApp) throws Exception {
        Labels.register(dbLabelLocator)
    }
}

both your solution and this do not work

FGabbr ( 2021-09-17 18:55:37 +0800 )edit

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'config.DBLabelLocator': Unsatisfied dependency expressed through bean property 'metaClass': Set this property value or disable dependency checking for this bean.

FGabbr ( 2021-09-17 19:17:18 +0800 )edit

using

LabelLocator2 dbLabelLocator = ctx.autowireCapableBeanFactory.autowire(DBLabelLocator.class, Autowire.BY_TYPE.value(), false)

in place of ctx.getBean seam to work! Thanks for your answer, it helped getting there!

FGabbr ( 2021-09-17 19:24:01 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2021-09-16 20:38:57 +0800

Seen: 5 times

Last updated: Sep 17 '21

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More