分享到QQ空间

SpringMVC学习记录

1E)Spring MVC框架
①Jar包结构:
docs+libs+schema.
版本区别:核心包,源码包。


SpringMVC文档学习;
学习三步骤:

1)是什么?
开源框架

2)做什么?
IOC :依赖注入。是一种设计模式
set()方式注入
AOP:  
3)怎么做?

②创建一个简单的Spring框架
代码目录结构如下:

步骤:
1)创建JavaBean
2)创建Dao,和Service类
3)写好配置文件applicationContext.xml
基本的配置代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  6. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  7. <bean id="userBean" class="Spring_Bean.UserBean">
  8. <property name="username" value="kam"></property>
  9. <property name="password" value="123456"></property>
  10. </bean>
  11. <bean id="userDao" class="Spring_Dao.UserDaoImpl"></bean>
  12. <bean id="userService" class="Spring_Service.UserServiceImpl">
  13. <property name="daoInf" ref="userDao"></property>
  14. </bean>
  15. </beans>
③Spring其他注入方式:注解注入
1)关键字:@Resource  @Autowired(一般情况下我们用的都是Resource注解,因为这个注解不依赖与SpringMVC).
2)在日常的开发中,我们的组件类通常有几十个甚至上百个,如果我们都用<bean></bean>字段在XML文件进行声明,这种效率是及其低下的。所以我们应该使用Spring提供的组件扫描机制,只要声明了要扫描的包,Spring容器就会自动将已经用”@Controller  @Service @Repository @Component“声明的组件类归并起来。
其中 
@Service用于标注业务层的组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。但是在目前的spring版本中,这几个注解的作用是一样的,但是在以后可能会进行区分。
代码如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. - Middle tier application context definition for the image database.
  4. -->
  5. <beans xmlns="http://www.springframework.org/schema/beans"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  12. <!-- 扫描cn.CMSystem的bean -->
  13. <context:component-scan base-package="cn.CMSystem"> </context:component-scan>
  14. </beans>












posted @ 2014-11-17 17:28  KAM【绝对思维】  阅读(178)  评论(0编辑  收藏  举报
分享到QQ空间