###解析外部配置文件
resources文件夹下,新建db.properties(和数据库连接相关的信息)

  driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db
username=root
password=root

###开发步骤
1)创建maven工程

添加web.xml
添加tomcat运行环境
添加jar spring-webmvc,junit,commons-dbcp,mysql
添加application.xml (加载类的配置)

2)配置数据库的连接池信息

 <!--
1.util:properties表示读取外部的属性文件,并实例化对象
2.id表示名称
3.location表示属性文件的位置 <util:properties id="dbConf" location="classpath:db.properties"> <!--配置数据库的连接池 1.使用spring表达式给属性赋值 2.spring表达式语法格式:#{ }---> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{dbConf.driverClassName}"/>
<property name="url" value="#{dbConf.url}"/>
<property name="username" value="#{dbConf.username}"/>
<property name="password" value="#{dbConf.password}"/>
</bean>

##基于注解的bean管理
##1.基于注解的方式实例化对象(推荐使用)
    实例化对象和依赖注入有两种方式:配置文件,注解

1.扫描包  (配置文件)
     <!--扫描包可以扫描当前包和子包下的所有类-->
     <context:component-scan  base-package="cn.sjl.dao">

2.特定功能(实例化功能)的注解
      @Component:通用注解:实例化对象
      @Controller:实例化控制层类的对象
      @Service:实例化业务层类的对象
      @Reponsitory:实例化持久层类的对象

 @Repository("userDao")
public class UserDaoImpl implements UserDao{
public void insertUser(){
System.out.println("添加成功!");
}

//在mybatis里,一般是定义一个接口,接口里面定义抽象方法,并在配置文件里面实现相应的抽象方法。

##2.生命周期管理

//@Component表示实例化对象
@Component
public class BeanLife {
public BeanLife(){
System.out.println("BeanLife");
}
//@PostConstruct表示定义初始化方法
//@PostConstruct:Tomcat运行环境依赖的jar包
@PostConstruct
public void init(){
System.out.println("init");
}
public void execute(){
System.out.println("execute");
}
//@PreDestroy表示定义销毁的方法
//@PreDestroy:Tomcat运行环境依赖的jar包
@PreDestroy
public void destroy(){
System.out.println("destroy");
} }

##3.bean作用域

//通过注解实例化对象,默认为单例singleton
//@Scope定义bean的作用域
//@Scope("prototype")表示bean作用域为多例
@Component
@Scope("prototype")
public class DemoScope { }

##4.延迟加载

//默认bean对象的实例化,是立即加载
//@Lazy设置bean是否延迟加载的注解
//@Lazy(true)设置bean对象为延迟加载

@Component
@Lazy(true)
public class DemoLazy {
public DemoLazy(){
System.out.println("DemoLazy");
} }

#DI(动态地向某个对象提供它所要的对象)

参考:http://www.360doc.com/content/18/0125/09/27831725_724899826.shtml
##1.@Resource(推荐使用)

//1.@Resource  tomcat运行环境依赖jar包中定义的注解
//2.@Resource 实现依赖注入
//3.@Resource 实现依赖注入,可以省略set方法
//4.@Resource默认依赖注入的方式为byName
//5.@Resource如果没有匹配的属性
//               按照byType方式实现依赖注入
//6.@Resource(name="userDaoImpl")

  @Resource(name="userDaoImpl")
private UserDao userDao;

##2.@Autowired(了解)

public class UserServiceImpl2 implements UserService2{
//1.@Autowired 依赖注入,相当于bean的配置文件中的<id=""   class="">,所以@AutoWired这个注解的作用是声明并加载类的过程 ; 只是@AutoWired是按类型注入的。
//2.@Autowired 默认依赖注入的方式byType;
// 如果有多个相同类型的对象,那么按照byName依赖注入
//3.如果使用byName实现依赖注入,
//  使用@Qualifier注解定义匹配的名称
//4.@Qualifier不能单独使用

@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao; public void addUser() {
userDao.insertUser(); } }

##3.@Qualifier

@Qualifier这个注解一般和@AutoWired联合起来使用,因为@AutoWired是按类型进行值的注入,假如同一个父接口Hello,两个子实现类,然后进行id的配置,最后

值的注入的时候要注意联合@Qualifier使用。

package com.sjl.hello;

public Interface Hello{
void hello();
} package com.sjl.hello; public class Hello1 implements Hello{
public void hello(){
System.out.println("this is hello1");
}
} package com.sjl.hello; public class Hello1 implements Hello{
public void hello(){
System.out.println("this is hello1");
}
}
SpringBeans.xml 

<bean id="hello1" class="com.sjl.hello.Hello1"/>

<bean id="hello2" class="com.sjl.hello.Hello2"/>
import com.sjl.hello;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:SpringBeans.xml"})
public class HelloTest { @Autowired
@Qualifier("hello1")
private Hello hello; @Test
public void sayHello(){
hello.sayHello();
}
}

##4.@Required

该注解主要对set注入的值进行非空判断,若没有注入值则运行报错。

##5.@Value(了解)

@Component
public class Student {
//@Value("Admin")给String或者基本数据类型依赖注入
//@Value("#{conf.name}")使用spring表达式实现依赖注入
@Value("#{conf.name}")
private String name; public String toString(){
return "name="+name;
} }

spring(读取外部数据库配置信息、基于注解管理bean、DI)的更多相关文章

  1. Spring配置文件外部化配置及.properties的通用方法

    摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...

  2. Spring+MyBatis双数据库配置

    Spring+MyBatis双数据库配置 近期项目中遇到要调用其它数据库的情况.本来仅仅使用一个MySQL数据库.但随着项目内容越来越多,逻辑越来越复杂. 原来一个数据库已经不够用了,须要分库分表.所 ...

  3. Spring Boot 外部化配置(一)- Environment、ConfigFileApplicationListener

    目录 前言 1.起源 2.外部化配置的资源类型 3.外部化配置的核心 3.1 Environment 3.1.1.ConfigFileApplicationListener 3.1.2.关联 Spri ...

  4. Spring Boot 外部化配置(二) - @ConfigurationProperties 、@EnableConfigurationProperties

    目录 3.外部化配置的核心 3.2 @ConfigurationProperties 3.2.1 注册 Properties 配置类 3.2.2 绑定配置属性 3.1.3 ConfigurationP ...

  5. 基于注解的bean配置

    基于注解的bean配置,主要是进行applicationContext.xml配置.DAO层类注解.Service层类注解. 1.在applicationContext.xml文件中配置信息如下 &l ...

  6. spark通过JDBC读取外部数据库,过滤数据

    官网链接: http://spark.apache.org/docs/latest/sql-programming-guide.html#jdbc-to-other-databases http:// ...

  7. [Xcode 实际操作]九、实用进阶-(8)实现App的Setting设置:添加和读取程序的配置信息

    目录:[Swift]Xcode实际操作 本文将演示如何实现添加和读取程序的配置信息. 在项目文件夹[DemoApp]上点击鼠标右键->[New File]创建一个设置束文件 ->[Sett ...

  8. 泛微ecology OA系统某接口存在数据库配置信息泄露漏洞

    2漏洞详情 攻击者可通过该漏洞页面直接获取到数据库配置信息,攻击者可通过访问存在漏洞的页面并解密从而获取数据库配置信息,如攻击者可直接访问数据库,则可直接获取用户数据,由于泛微e-cology默认数据 ...

  9. 泛微e-cology OA系统某接口存在数据库配置信息泄露漏洞复现

    1.简介(开场废话) 攻击者可通过存在漏洞的页面直接获取到数据库配置信息.如果攻击者可直接访问数据库,则可直接获取用户数据,甚至可以直接控制数据库服务器. 2.影响范围 漏洞涉及范围包括不限于8.0. ...

随机推荐

  1. internal table operation

    1: the basic operation *&---------------------------------------------------------------------* ...

  2. matplotlib画的图保存为emf格式

    在用matplotlib保存图片时,发现不能直接保存为emf格式.百度有人说要先另存为svg格式,再使用INKSCAPE软件转换成emf格式.我试了一下,发现还是不行,后来,发现先用matplotli ...

  3. iOS UI基础-13.0 数据存储

    应用沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒 应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Lay ...

  4. cocos2dx 3.x(for 循环让精灵从中间往上下两边排列)

    最近很多游戏都喜欢房卡类的游戏,就是创建房间时(),选择玩法与规则,今天耗费2小时处理这个数学问题:例如选择规则两条,则背景框中间显示两条规则,若选择三条,则背景框中间显示三条规则与玩法,依次从中间往 ...

  5. Unity shader学习之屏幕后期处理效果之Bloom效果

    Bloom特效是游戏中常见的一种屏幕效果.这种特效可以模拟真实摄像机的一种图像效果,它让画面中较亮的区域“扩散”到周围的区域中,造成一种朦胧的效果. Bloom的实现原理很简单,首先根据一个阈值提取出 ...

  6. uvm设计分析——callback

    uvm_callback,设计者在进行class的function设计时,有意留下的一些hook,总是遍历某个pool中的对象: 使用者在使用时,将实现添加到某个pool中: callback中,最重 ...

  7. 强化学习--Actor-Critic---tensorflow实现

    完整代码:https://github.com/zle1992/Reinforcement_Learning_Game Policy Gradient  可以直接预测出动作,也可以预测连续动作,但是无 ...

  8. 在统一软件开发过程中使用UML

    如何在统一软件开发过程中使用UML? 起始阶段常用UML图 在起始阶段,通常有用例图.类图.活动图.顺序图等UML图的参与. 获取用户需求之后首先要将这些需求转化为系统的顶层用例图. 在确定了用例之后 ...

  9. linux本地机上传文件到服务器

    最近工作全部切换到了linux环境下,就是吃喝拉撒全在linux下,微信,web端,qq,web端,-------,各种socket编程,网络通讯- 本地linux机从阿里云下载文件

  10. Math对象属性

    2018-11-28 11:18:46