开发者

How to insert property in Autodetected Component in Spring?

开发者 https://www.devze.com 2023-03-27 09:49 出处:网络
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 c

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号