Showing posts with label autowired. Show all posts
Showing posts with label autowired. Show all posts

Monday, July 6, 2015

Use a new keyword and still inject the object : Spring

Spring works on dependency injection. This means there is no need to create an object explicitly, spring will do it for you.

But there are situations where you have to create new objects (ex., Reflection). By going with this approach you are conveying string that you will have control over the current object. i.e., any Autowired fields inside the object class will be null and spring will not consider injecting it. 

But there is a way to autowire those fields as well,

appctx.getAutowireCapableBeanFactory().autowireBean(wsdlParserAPIObject);

where appctx is ApplicationContext which is autowired.

http://stackoverflow.com/questions/31237062/using-class-forname-but-want-to-autowire-members-of-the-target-class

Friday, June 26, 2015

Autowire datasource while using JdbcDAOSupport

public class EntityDAO extends JdbcDaoSupport{

Logger logger = LoggerFactory.getLogger(EntityDAO.class);

@Autowired
private DataSource datasource;


/*
* Setting data source as it extends jdbcDAOSupport
*/
@Autowired
private void initialize()
{
setDataSource(datasource);
}