Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: Helper Class replacement

Tags:

spring

I'm new to Spring.

Back in the not so long ago days.

I have a Helper Class with static methods help to assemble and build objects.

But i realize I can't @Autowired static variables.

May I know what is spring replacement for helper class with static method? Or I should make them into @Service classes too?

like image 227
user4127 Avatar asked Sep 19 '26 04:09

user4127


1 Answers

You can use @Component annotated classes. It is the base of all other components. Your class would be something like :

import org.springframework.stereotype.Component;

@Component("assembler") // giving name to component is not mandatory, could be @Component
public class Assembler {

    public boolean assemble(Object obj) {
        // your stuff here
    }
}

This is your assembler component. You can use this in other classes using :

@Controller
public class MyController {

    @Autowired
    private Assembler assembler;

    @RequestMappings(//mappings done here)
    public String showMsg() {
        // here you use assembler component
        boolean response = assembler.assemble(new Object());
        System.out.println(response);
    }
}

This is just an example. I hope you get my point.

like image 171
Jeevan Patil Avatar answered Sep 21 '26 17:09

Jeevan Patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!