I'm working with Spring Boot and I'm having a little trouble understanding Beans. I'm led to believe Beans replace the new
keyword.
I identified when using just Autowire my Beans wouldn't create a new instance on an Object and my REST application would return the same response the User first asked for no matter what (i.e. If I visited url/id/1 initially, then visited url/id/2 the REST response would be the same as url/id/1).
I tried to solve this by creating an @Configuration file to define a bean.
@Configuration
public class UserConfig {
@Autowired
UserDAO DAO;
@Bean
public User getUser(int uid) {
try {
return DAO.getUser(uid);
} catch (SIDException e) {
return null;
}
}
}
But I keep getting this error on runtime: Parameter 0 of method getUser in com.application.Config.UserConfig required a bean of type 'int' that could not be found.
I don't understand this, as I'm trying to define the Bean in the Configuration file.
In my main file I have these annotations:
@SpringBootApplication(scanBasePackages = {"com.application.Config","com.application"})
@ComponentScan({"com.application.Config","com.application"})
And I'm using my bean in this context, if it helps:
@Service
public class UserService {
@Autowired
private UserDAO DAO;
public User getUser(int uid) {
try {
return DAO.getUser(uid);
} catch (SIDException e) {
return null;
}
}
}
Thank you :)