Spring基础---自动装配的方式
来源:互联网 发布:大数据解决方案的公司 编辑:程序博客网 时间:2024/05/22 02:00
一、通过注解的方式实现自动装配
1、在需要自动生成和装配的类添加 注解
你的spring容器中要有一个 UserMapper接口类型的 Bean 不然自动装配 会报错
2、在spring配置文件 启用扫描注解
<!-- 需要在添加 xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd--> <!-- 启用扫描注解 --> <context:component-scan base-package="service"></context:component-scan>
3、在测试类
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService =(UserService) ac.getBean("userServiceImpl");//这样就可以获取到 业务类接口的实例
二、通过 XML配置 的方式实现自动装配
1、Spring配置文件
<!-- 引入 数据库配置文件 --><!-- 创建数据源 --><!-- 配置 sqlsessionFactory --><!-- 配置自动生成Mapper接口对象 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mapper"></property></bean><!-- 使用xml配置 实现自动装填 --><bean id="userServiceImpl" class="service.UserServiceImpl" autowire="byType" ></bean>
<bean>节点的autowire属性实现了自动装配 autowire属性值共有4个
阅读全文
0 0
- Spring基础---自动装配的方式
- spring基础-Bean的装配方式(三)自动装配
- SPRING的自动装配
- spring的自动装配
- spring的自动装配
- Spring的自动装配
- spring的自动装配
- Spring的自动装配
- spring的自动装配
- spring的自动装配
- Spring的自动装配
- Spring 的自动装配
- spring的自动装配
- spring 的自动装配
- spring的自动装配
- spring的自动装配
- spring的自动装配
- spring的自动装配
- easyUI
- AndroidStudio使用之——两种方式添加类注释模板
- centos7安装卸载软件
- 匀速运动(js)
- 解密Hibernate @GenericGenerator注解中的strategy属性
- Spring基础---自动装配的方式
- Paxos (computer science)
- HTML基本语法总结
- Struts2 ActionMethod 【DMI】(动态方法调用 Dynamic Method Invoke)
- cmd中执行Python命令时>>>与>提示符的不同结果
- Ubuntu16.04+caffe+digits安装配置
- 使用 Vim 的 jump list 看代码
- 相对父元素的位置坐标
- MySQL 5.7.20免安装版安装教程