一、spring框架简介
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
二、spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com"/>
<bean name="student" class="com.ly.javabean.Student">
<property name="className" value="3年5班"/>
</bean>
<bean name="student8" class="com.ly.javabean.Student">
<property name="className" value="3年4班"/>
</bean>
<!-- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name=""
</bean>-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<!--<context:component-scan base-package="com" />-->
<bean name="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean name="dao" class="com.ly.dao.daoImpl.SysDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
</beans>
context:component-scan:使用构造型(stereotype)注解所标注的类,如@Component(组件),@Service(服务),@Controller(控制器),@Repository(数据仓库)。
三、spring简单使用
@RunWith(SpringJUnit4ClassRunner.class)
@Component
public class SpringTest {
@Autowired
private Sysdao dao;
@Test
public void demo1(){
/*ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Sysdao dao = applicationContext.getBean("dao",Sysdao.class);*/
List apkInfo = dao.find("from ApkInfo", null);
System.out.println(Arrays.toString(apkInfo.toArray()));
}
}
由于有了spring的管理,对象的获取可以直接有spring管理,不用直接new一个对象。这里可以使用配置文件的格式获取,也就是上述注释的部分来获取一个对象,也可使用注解的方式获取,但是在配置文件中必须加上context:component-scan才可使用。
四、log4j
可引入log4j来记录日志。
# Global logging configuration 开发时候建议使用 debug
log4j.rootLogger=info, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS}%5p [%t] - %m%n
五、controller、service、dao的搭建

dao和service只是简单的查询,不在展示。
controller:
@WebServlet(name = "com.ly.controller.ShowViewServlet", urlPatterns = "/show/aa")
public class ShowViewServlet extends HttpServlet {
private Logger log = LoggerFactory.getLogger(ShowViewServlet.class);
public ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
Myservice service = (Myservice) context.getBean("myserviceImpl");
response.setContentType("text/html;charset=utf-8");
String param = request.getParameter("param");
param = "%" + param + "%";
String hql = "select a from FilmBtsNew a where a.name like '" + param + "'";
List list = service.find(hql);
log.info("参数:{}", param);
JSONObject jsonObject = new JSONObject();
jsonObject.put("person", list);
response.getWriter().print(jsonObject);
}
}
结果如下:
现在只是简单的将json数据上传至web,下一步学习了简单的前端就可以实现一个电影主页了!
Spring框架入门与实践

本文介绍了Spring框架的基本概念,展示了如何通过配置文件管理和创建对象,同时涵盖了log4j的日志记录,以及Controller、Service和DAO层的搭建过程,最后通过一个简单的Web应用实例演示了Spring在实际项目中的应用。
1110

被折叠的 条评论
为什么被折叠?



