First of all I’m quite new to Springframwork.
Let’s say I have a controller in Spring-MVC:
@Controller
public class FooController {
@Autowired
private Foo foo;
@Autowired
private FooService fooService;
@RequestMapping(value="/addfoo", method = RequestMethod.GET)
public void addRequest(
@RequestParam(value="rq_param", required=true) String param){
foo.setValue(param);
fooService.addFoo(foo);
}
}
I need to add Foo into a database. But before I need to set a value. This should happen when a certain request comes in (from elsewhere).
Here’s my Service:
@Service
public class FooServiceImpl implements FooService {
@Autowired
private FooDAO fooDAO;
@Transactional
public void addFoo(Foo foo) {
fooDAO.addFoo(foo);
}
}
But this doesn’t work. I get
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fooController': Injection of autowired dependencies failed;
I’m quite sure that I’ve a made a basic mistake due to my lack of knowledge about IoC…
Thanks!
I don’t think you should inject the Foo. It looks like a model object to me, not an interface-based service or controller.
You should create one using new when the request comes in, outside of Spring’s control. You want to bind the value from the request parameter into the new Foo object and persist it.
Every object in a Spring project need not be under the control of the bean factory.
Usually you see calls to new for objects within method scope. They’re usually POJO model objects that don’t have interfaces. Your cases seems to be one of them to me.