配置文件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. 文本文件显示 删除文本文件前n个字符

    #include<iostream>#include<string.h>using namespace std;void displayContent(const char * ...

  2. XDU 1140 寻找万神(字符串匹配)

    学会strstr的使用 strstr(str1,str2)函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地址:否则,返回NULL. #include ...

  3. JVM内存—堆(heap)栈(stack)方法区(method) (转)

    JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区:1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令 ...

  4. centos7 最小化安装后的配置优化

    echo #CENTOS7echo #1.最小化安装之后需要做的事echo 2.配置echo 2.1 安装网络yum install net-tools -y echo 2.2 更新机器名echo h ...

  5. 设置eclipse编码格式

    1.修改eclipse默认工作空间编码方式.点击Window-->Preferences-->General-->Workspace,设置编码格式为UTF-8,然后点击OK.

  6. ReentrantLock的底层实现机制 AQS

    ReentrantLock的底层实现机制是AQS(Abstract Queued Synchronizer 抽象队列同步器).AQS没有锁之类的概念,它有个state变量,是个int类型,为了好理解, ...

  7. c++之旅:类型的强制转换

    类型强制转换 在编程的时候我们经常遇到类型的强制转换,C++为此提供了更安全的转换方式,在编程中我们更多的应该采用C++提供的类型转换方式 基本类型转换 基本类型转换用的最多,一般将高精度转换为低精度 ...

  8. jvm2

    垃圾回收器的实现: 1.让用户都暂停,不再产生垃圾,就去收集垃圾.新生代用复制算法清理垃圾,老生代用标记整理算法搜集垃圾. 优秀的算法:服务端默认是CMS收集器. %..jvm案例演示 内存: Jco ...

  9. 重命名文件或文件夹(mv命令与rename命令)

    在Linux下重命名文件或目录,可以使用mv命令或rename命令,这里分享下二者的使用方法. mv命令既可以重命名,又可以移动文件或文件夹. 例子:将目录A重命名为B mv A B 例子:将/a目录 ...

  10. Java管程解决生产者消费者问题

    同样是实验存档.//.. 依然以生产者消费者问题作为背景. 管程(=“资源管理程序”)将资源和对资源的操作封装起来,资源使用者通过接口操作资源就ok,不用去考虑进程同步的问题. 管程: package ...