配置文件spring.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 注入方式一,直接使用类全名注入,属性必须有相应的set方法才可以注入成功,否则会失败 -->

<!-- 假如没有a属性,但是有setA方法,也可以配置a的property和value,但这样做并没有什么意义 -->

<bean id="userDao1" class="com.colorlight.spring.UserDaoImpl">

<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>

<property name="driverClass" value="com.mysql.jdbc.Driver"></property>

<property name="username" value="root"></property>

<property name="password" value="1"></property>

</bean>

<!-- 注入方式二:使用静态的工厂方法创建实例,必须配置相应的工厂类和工厂方法 -->

<bean id="userDao2" class="com.colorlight.spring.StaticDaoFactory"

factory-method="createUserDaoInstance">

<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>

<property name="driverClass" value="com.mysql.jdbc.Driver"></property>

<property name="username" value="root"></property>

<property name="password" value="2"></property>

</bean>

<!-- 注入方式三:使用非静态的工厂方法创建实例,必须先生成工厂的实例 -->

<bean id="daoFactory" class="com.colorlight.spring.NonStaticDaoFactory">

</bean>

<bean id="userDao3" factory-bean="daoFactory" factory-method="createUserDao">

<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>

<property name="driverClass" value="com.mysql.jdbc.Driver"></property>

<property name="username" value="root"></property>

<property name="password" value="3"></property>

</bean>

<!-- 注入方式四:使用构造函数注入 -->

<bean id="userDao4" class="com.colorlight.spring.UserDaoImpl">

<constructor-arg value="jdbc:mysql://localhost:3306/test" />

<constructor-arg value="com.mysql.jdbc.Driver" />

<constructor-arg value="root" />

<constructor-arg value="4" />

</bean>

</beans>

接口UserDao:

package com.colorlight.spring;

public interface UserDao {

void printInfo();

}

实现类UserDaoImpl:

package com.colorlight.spring;

public class UserDaoImpl implements UserDao {

private String jdbcUrl;

private String driverClass;

private String username;

private String password;

public UserDaoImpl(){

}

public UserDaoImpl(String url, String driver, String name, String pw) {

this.jdbcUrl = url;

this.driverClass = driver;

this.username = name;

this.password = pw;

}

public void printInfo() {

System.out.println("jdbcUrl  = " + jdbcUrl);

System.out.println("driverClass = " + driverClass);

System.out.println("username = " + username);

System.out.println("password = " + password);

}

public String getJdbcUrl() {

return jdbcUrl;

}

public void setJdbcUrl(String jdbcUrl) {

this.jdbcUrl = jdbcUrl;

}

public String getDriverClass() {

return driverClass;

}

public void setDriverClass(String driverClass) {

this.driverClass = driverClass;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

静态方法类:

package com.colorlight.spring;

public class StaticDaoFactory {

public static UserDao createUserDaoInstance(){

return new UserDaoImpl();

}

}

非静态方法类:

package com.colorlight.spring;

public class NonStaticDaoFactory {

public UserDao createUserDao() {

return new UserDaoImpl();

}

}

测试类:

package com.colorlight.springscope;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

ApplicationContext ac = new ClassPathXmlApplicationContext(

"applicationContext.xml", this.getClass());

// 单例

@Test

public void test1() {

System.out.println("在getBean()调用之前");

User user1 = (User) ac.getBean("user1");

User user2 = (User) ac.getBean("user1");

System.out.println(user1 != null);

System.out.println(user1 == user2);

}

// 多例

@Test

public void test2() {

System.out.println("在getBean()调用之前");

User user1 = (User) ac.getBean("user2");

User user2 = (User) ac.getBean("user2");

System.out.println(user1 != null);

System.out.println(user1 == user2);

}

}

spring注入的四种方式的更多相关文章

  1. 普通java类加入spring容器的四种方式

    今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的s ...

  2. Spring注入值得2种方式:属性注入和构造注入

    Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...

  3. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

  4. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  5. Spring通过构造方法注入的四种方式

    通过构造方法注入,就相当于给构造方法的参数传值 set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选 的,构造注入的优势是通过构造强制依赖关系,不可能实例化不 完全的或无法使用的bean. Me ...

  6. Spring 注入的两种方式

    Spring 的两种注入方式: 1. 属性注入:通过无参构造函数+setter方法注入 2. 构造注入:通过有参的构造函数注入. 优缺点: 1. 属性注入直白易懂,缺点是对于属性可选的时候,很多个构造 ...

  7. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

  8. Spring依赖注入的四种方式

    首先,我们需要定义一个Bean的class类: package framework.spring; import org.springframework.beans.BeansException; i ...

  9. R3注入的四种方式

    DLL注入 1.首先要获取想要注入的进程句柄(OpenProcess) 2.从要注入的进程的地址空间中分配一段内存(VirtualAllocEx) 3.往分配的内存位置写入要注入的DLL名称(Writ ...

随机推荐

  1. 工作笔记——限定input上传文件对话框中能选取的文件的格式

    原文:http://www.dengzhr.com/frontend/1059 input[file]标签的accept属性可用于指定上传文件的 MIME类型 . 例如,想要实现默认上传图片文件的代码 ...

  2. PKU 1379 Run Away(模拟退火算法)

    题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用 ...

  3. XDU 1022 (数论筛法+前缀和)

    解法一:数论筛法+前缀和 //其实题目中f[n]的值可理解为存在多少个整数对使a*b<=n #include<cstdio> #define N 1007 #define maxn ...

  4. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C - Jon Snow and his Favourite Number

    地址:http://codeforces.com/contest/768/problem/C 题目: C. Jon Snow and his Favourite Number time limit p ...

  5. mysql 手动安装和管理

    版本:5.7.10 my.ini简单配置 [client] default-character-set=utf8 [mysqld] port = 3306 basedir =D:/programs/M ...

  6. Swoole学习(七)Swoole之异步TCP服务器的创建

    环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...

  7. cl.exe 命令行编译sqlite3 sqlite3.dll及sqlite3.exe

    有点被宇宙最强的ide惯坏了,封装的太好,不能像gcc那样一步步了解其原理,其实强大的vs背后也有类似gcc的cl.exe 看到How To Compile SQLite http://sqlite. ...

  8. 20135320赵瀚青LINUX第五章读书笔记

    第五章--系统调用 5.1 与内核通信 作用 1.为用户空间提供一种硬件的抽象接口 2.保证系统稳定和安全 3.除异常和陷入,是内核唯一的合法入口. API.POSIX和C库 关于Unix接口设计:提 ...

  9. 求生之路 Hammer World Editor打开后闪退解决办法

    试过WinXp.Win7.Win10 都无法正常启动Hammer,搜索N多资料后发现如图修改 控制面板 -> 区域 -> 格式 -> 英语[美国] 即可正常启动了!!!

  10. Jquery3 常规选择器

    学习要点: 1.简单选择器 2.进阶选择器 3.高级选择器 jQuery 最核心的组成部分就是:选择器引擎.它继承了 CSS 的语法,可以对 DOM 元素的标签名.属性名.状态等进行快速准确的选择,并 ...