SpringMVC学习记录

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


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

1)是什么?
开源框架

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

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

步骤:
1)创建JavaBean
2)创建Dao,和Service类
3)写好配置文件applicationContext.xml
基本的配置代码如下:
181504173475115.png
 
  
  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>












转载于:https://www.cnblogs.com/kamzhuyuqing/p/4c64dee2ae001a402f75a2af61c3e438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值