在现实中,使用XML或者注解各有道理,建议在自己的工程中所开发的类尽量使用注解方式,因为使用它并不困难,甚至可以说更为简单,而对于引入第三方包或者服务的类,尽量使用XML方式,这样的好处是可以尽量对三方包或者服务的细节减少理解,也更加清晰和明朗。
  Spring同时支持这两种形式的装配,所以可以自由选择,只是无论采用XML还是注解方式的装配都是将Bean装配到Spring IoC容器中,这样就可以通过Spring IoC容器去管理各类资源了。

  spring-data.xml:

<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.ssm.chapter10.annotation"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/springmvc?useSSL=false&amp;serverTimezone=Hongkong&amp;characterEncoding=utf-8&amp;autoReconnect=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean> </beans>

  使用注解@ImportResource,引入spring-data.xml所定义的内容

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; @Configuration
// @ComponentScan(basePackages = {"com.ssm.chapter10.annotation"})
@ImportResource({"classpath:ssm/chapter10/spring-dataSource.xml"})
public class ApplicationConfig2 {
}
import com.ssm.chapter10.annotation.pojo.Role;
import com.ssm.chapter10.annotation.service.RoleDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; @Component
public class RoleDataSourceServiceImpl implements RoleDataSourceService { @Autowired
// @Qualifier("dataSource2")
DataSource dataSource = null; // @Override
public Role getRole(Long id) {
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
Role role = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement("select id, role_name, note from t_role where id = ?");
ps.setLong(1, id);
rs = ps.executeQuery();
while (rs.next()) {
role = new Role();
role.setId(rs.getLong("id"));
role.setRoleName(rs.getString("role_name"));
role.setNote(rs.getString("note"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
/**********close database resources************/
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return role;
}
}
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig2.class);
RoleDataSourceServiceImpl roleDataSourceServiceImpl = context.getBean(RoleDataSourceServiceImpl.class);
Role role = roleDataSourceServiceImpl.getRole(1L);
System.out.println(role.toString());
context.close();

spring 装配bean的混合使用的更多相关文章

  1. Spring 装配Bean

    Spring 装配Bean 装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介 ...

  2. Spring装配bean

    Spring配置的可选方案 Spring提供了如下三种装配机制: (1)在XML中显式配置 (2)在Java中显式配置 (3)隐式的bean发现机制和自动装配 Spring有多种方式可以装配bean, ...

  3. Spring装配Bean之XML装配bean

    在Spring刚出现的时候,XML是描述配置的主要方式,在Spring的名义下,我们创建了无数行XML代码.在一定程度上,Spring成为了XML的同义词. 现在随着强大的自动化配置和Java代码的配 ...

  4. Spring装配Bean的过程补充

    对上一篇的<Spring装配Bean的过程>的过程说一下,不然真产生了误区. 误区在哪里呢?那就是spring bean的作用域问题. 说哈常用的两种作用域:默认是scope = sing ...

  5. Spring装配Bean的过程

    首先说一个概念:“懒加载” 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spring配置文件中be ...

  6. Spring 装配Bean入门级

    装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介绍 1 :声明Bean  B ...

  7. 【转】spring 装配Bean中构造参数的注入

    转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...

  8. spring 装配bean的三种方式

    这段时间在学习Spring,依赖注入DI和面向切面编程AOP是Spring框架最核心的部分.这次主要是总结依赖注入的bean的装配方式. 什么是依赖注入呢?也可以称为控制反转,简单的来说,一般完成稍微 ...

  9. Spring装配Bean之组件扫描和自动装配

    Spring从两个角度来实现自动化装配: 组件扫描:Spring会自动发现应用上下文中所创建的bean. 自动装配:Spring自动满足bean之间的依赖. 案例:音响系统的组件.首先为CD创建Com ...

随机推荐

  1. Java - 基础到进阶

    # day01 一:基本操作 package _01.java基本操作; /** * 文档注释 */ public class _01Alls { public static void main(St ...

  2. 011——MATLAB清除工作控件变量

    (一):参考文献:https://zhidao.baidu.com/question/234530287.html 清除当前工作空间全部变量:clear: 清除当前工作空间某些变量:clear 变量名 ...

  3. Day15:大前端

    垂直水平居中 css: display: table-cell; text-align: center; vertical-align: middle; div: display: inline-bl ...

  4. ELK教程2:Kibana的安装

    kibana作为ElastciSearch的数据查询展示界面,集成了很多的功能,本文主要讲述如下部署kibana. 安装 安装命令如下: # 下载kibana的npm wget https://art ...

  5. Pytest权威教程27-Bash自动补全设置

    目录 Bash自动补全设置 返回: Pytest权威教程 Bash自动补全设置 在Linux/Mac bash shell环境下,可以使用argcomplete对pytest命令进行自动补全.首先要安 ...

  6. Ruby on Rails框架(1)-安装全攻略

    序 关于Rails的三句箴言 (1)DRY:Don't Repeat Yourself(不要重复你自己) rails的开发理念,不要用你的代码不停的重复,rails框架给开发者提供了一套非常完善的支持 ...

  7. 怎么样在vue-cli的项目里面引入element ui

    第一步:先进入到项目里面 npm i element-ui -S 第二步: import Vue from 'vue'; import ElementUI from 'element-ui'; //这 ...

  8. Android中Popupwindow和Dialog的区别

    Android中的对话框有两种:PopupWindow和AlertDialog.它们都可以实现弹窗功能,但是他们之间有一些差别,下面总结了一点. (1)Popupwindow在显示之前一定要设置宽高, ...

  9. Linux文件的权限的基本介绍

    一. ls  -l    显示的内容如下: 二.rwx权限详解 1.rwx作用到文件 2. rwx作用在目录 三.文件及目录实际案例 四.修改权限  -  chmod 1. 基本说明: 2.第一种方式 ...

  10. RabbitMQ Management API调用

    RabbitMQ在运行时,偶尔会有一些死信,即消息未正常消费,造成消息积压在消息队列中, 一直卡住,重复循环消费原来的消息队列 那么就需要有一种机制,来查看RabbitMQ是否有消息未正常消费,从而让 ...