spring注解方式在一个普通的java类里面注入dao

@Repository
public class BaseDaoImpl implements BaseDao {
这是我的dao
如果在service层注入的话很简单
public class BaseServiceImpl implements BaseService {
@Autowired
public BaseDao baseDao;

这样就可以了
现在我想在一个普通的java类中注入进来改怎么写
比如我的普通java类是

package com.util.page;
public class PageUtil {

ApplicationContext ctx = new ClassPathXmlApplicationContext("../../WEB-INF/index-servlet.xml");
pageDao pagedao = (pageDao) ctx.getBean("pageDao");

接下来就可以使用DAO中的方法了。
eg:pagedao.getcount("xxx");

}

//pageDao类

@Repository
public class pageDao {

@Resource 
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) { 
this.jdbcTemplate = new JdbcTemplate(dataSource); 

}

public int getcount(String tablename){

.......
}

//index-servlet.xml

 <bean ></bean>

}

@Repository
public class BaseDaoImpl implements BaseDao {
这是我的dao
如果在service层注入的话很简单
public class BaseServiceImpl implements BaseService {
@Autowired
public BaseDao baseDao;

这样就可以了
现在我想在一个普通的java类中注入进来改怎么写
比如我的普通java类是

package com.util.page;
public class PageUtil {

ApplicationContext ctx = new ClassPathXmlApplicationContext("../../WEB-INF/index-servlet.xml");
pageDao pagedao = (pageDao) ctx.getBean("pageDao");

接下来就可以使用DAO中的方法了。
eg:pagedao.getcount("xxx");

}

//pageDao类

@Repository
public class pageDao {

@Resource 
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) { 
this.jdbcTemplate = new JdbcTemplate(dataSource); 

}

public int getcount(String tablename){

.......
}

//index-servlet.xml

 <bean ></bean>

}

相关文章:

  • 2021-08-19
  • 2021-10-05
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
相关资源
相似解决方案