我从三点多开始解决一个问题,在服务器启动的时候去调用dao层(通过service层)实现查询,
此处用的是不是原生jdbc,用mybatis封装了jdbc,去调用对应得service服务执行相关了dao实
现对数据库的访问,查了很多博客,第一种方法监听器实现没成功,第二种方法是自定义一个
在里面取对象,但是取得的对象不能强制转换成对应的实体类
对象,不能是实体类,不能是实体类,这个只能是其接口去强转,方法调用的是实体类的方法,
但是不能用实体类去强制转换。以上两种均可以解决。重点就是不要把动态代理对象想着强转
为实体类对象,就用接口。
在项目启动时加载数据库中的东西到applicationContext域中的方法(SSM)
第一种实现方式:监听器实现
public class CarsInBlackListListener implements ServletContextListener {
private WebApplicationContext springContext;
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
CarInBlackListService carInBlackListService = null;
if(springContext!=null){
System.out.println("非空的");
try {
carInBlackListService
= (CarInBlackListService)springContext.getBean("carInBlackListService");
} catch (BeansException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
List<CarInBlackList> allCarsInBlackList = carInBlackListService.getAllCarsInBlackList();
System.out.println("=================================================");
for(CarInBlackList temp:allCarsInBlackList){
System.out.println(temp);
}
System.out.println("========================================");
System.out.println("Hello , I am Here , application ");
System.out.println("========================================");
}
}
其在web.xml中的配置如下:
<listener>
<listener-class>
com.svm.listener.CarsInBlackListListener
</listener-class>
</listener>
这个监听器最好放后面,不要太靠前,如果在spring容器加载之前加载到这个监听器的结果就是控制着异常
第二种方式就是使用servlet,利用servlet的生命周期方法中的init()方法,但是这个方法不是生命周期方法,init(ServletConfig config)
这个才生命周期方法,前面那个方法是让我们去重写,去覆盖的,init(ServletConfig config)方法会调用init()方法。所以我们使用这个方法,
记住带参数的尽量不要去覆盖,覆盖的话要调用super.init(ServletConfig config)
public class InitServlet extends HttpServlet{
@Autowired
private SpringFactory springFactory = new SpringFactory();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(req, resp);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
@Override
public void init() throws ServletException {
ServletContext application = this.getServletContext();
System.out.println(springFactory);
Object object = springFactory.getObject("carInBlackListService");
System.out.println(object);
Constructor<?>[] clone = object.getClass().getConstructors().clone();
if(object instanceof CarInBlackListService){
CarInBlackListService carInBlackListService = (CarInBlackListService)object;
List<CarInBlackList> allCarsInBlackList = carInBlackListService.getAllCarsInBlackList();
application.setAttribute("black_list", allCarsInBlackList);
for(CarInBlackList temp:allCarsInBlackList){
System.out.println(temp);
}
System.out.println("转换成功");
}else{
System.out.println("转换失败");
}
super.init();
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
}
其在web.xml中的配置
<servlet>
<servlet-name>initServlet</servlet-name>
<servlet-class>com.svm.listener.InitServlet</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>