JavaEE框架整合开发入门到实战:Spring+SpringMVC+MyBAtis(微课版)——代码练习第一章

纯手敲,谢谢支持

	使用eclipse编写,工程目录结构如下:

在这里插入图片描述

第一单元

目录结构
在这里插入图片描述
其中使用的Spring核心jar放在WEB-INF的lib目录下

TestDao.java

package dao;

public interface TestDao {
    
    
	public void sayHello();
}

TestDaoImpl.java

package dao;

public class TestDaoImpl implements TestDao {
    
    

	@Override
	public void sayHello() {
    
    
		// TODO Auto-generated method stub
        System.out.println("Hello,Study hard!");
	}

}

Test.java

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

import dao.TestDao;

public class test {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		/*初始化Spring容器,加载配置文件
		   BeanFactory工厂
		  BeanFactory beanFac=new XmlBeanFactory(new FileSystemResource("C:\\Users\\gao\\Desktop\\Spring环境搭建\\ch1\\src\\applicationContext.xml"));      
		   TestDao tt=(TestDao)beanFac.getBean("test");
		 */
			 
		//ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
        //TestDao tt=(TestDao)appCon.getBean("test");
		ApplicationContext appcon=
				new FileSystemXmlApplicationContext("C:\\\\Users\\\\gao\\\\Desktop\\\\Spring环境搭建\\\\ch1\\\\src\\\\applicationContext.xml");
		TestDao tt=(TestDao)appcon.getBean("test");
		tt.sayHello();
	}
}

application.xml

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

import dao.TestDao;

public class test {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		/*初始化Spring容器,加载配置文件
		   BeanFactory工厂
		  BeanFactory beanFac=new XmlBeanFactory(new FileSystemResource("C:\\Users\\gao\\Desktop\\Spring环境搭建\\ch1\\src\\applicationContext.xml"));      
		   TestDao tt=(TestDao)beanFac.getBean("test");
		 */
			 
		//ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
        //TestDao tt=(TestDao)appCon.getBean("test");
		ApplicationContext appcon=
				new FileSystemXmlApplicationContext("C:\\\\Users\\\\gao\\\\Desktop\\\\Spring环境搭建\\\\ch1\\\\src\\\\applicationContext.xml");
		TestDao tt=(TestDao)appcon.getBean("test");
		tt.sayHello();
	}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ch1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41827511/article/details/115030826
今日推荐