How to insert property in Autodetected Component in Spring?
I am using @Service annotation of spring, so that my class should be autodetected by spring and made available for autowiring.But in my class, I need a property 'sqlmap'.If I had been using way of creating beans instead of auto-detection, I would have supplied that property using property tag in that bean.. So, is there any way I can inject my property in my class?Because unless that property is made available, spring will not be a开发者_StackOverflowble to create bean of that class.
Your @Service
class can "pull" a bean into a property using @Resource
, e.g.
@Service
public class MyService {
@Resource (name="sqlMapClient")
private SqlMapClient sqlMapClient;
}
An alternative to @Resource
is @Autowired
, which will automatically select the target by type:
@Service
public class MyService {
@Autowired
private SqlMapClient sqlMapClient;
}
Try both, see which works best for you.
精彩评论