配置文件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. Python(异常处理)

    一 错误和异常 程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) 2.逻辑错误(逻辑错误) 什么是异常 异常就是程序运行时发 ...

  2. delphi程序中定义热键

    delphi程序中定义热键   用到3个API函数          BOOL RegisterHotKey        (        HWND hWnd,        //响应该热键的窗口句 ...

  3. Multiple encodings set for module chunk explatform "GBK" will be used by compiler

    项目用idea启动的时候,突然报了个这个 Multiple encodings set for module explatform "GBK" will be used by co ...

  4. UVA - 12298 Super Poker II (FFT+母函数)

    题意:有四种花色的牌,每种花色的牌中只能使用数值的约数个数大于2的牌.现在遗失了c张牌.每种花色选一张,求值在区间[a,b]的每个数值的选择方法有多少. 分析:约数个数大于2,即合数.所以先预处理出5 ...

  5. 关于TOSCA自动化测试工具, 我想问一些问题(持续整理中)

    通过学习,实践踩坑,有以下问题不太明白 1.  Artifacts and results from your complete test portfolio (cross-browser, mobi ...

  6. Linux查看端口占用情况,并强制释放占用的端口

    1.查找被占用的端口 netstat -tln netstat -tln | grep 8080 netstat -tln 查看端口使用情况,而netstat -tln | grep 8080则是只查 ...

  7. IIS如何确定请求的处理程序

    1. 给定一个url请求,IIS需要确定它的文件名,扩展名,以及最相似的与本请求资源合适的"ScriptMaps"metadata (缓存的ISAPI扩展 - 应用程序扩展名映射列 ...

  8. Apache httpd服务部署

    1. yum安装 yum install httpd yum install httpd-devel yum install httpd-manual 2. 配置 vim /etc/httpd/con ...

  9. SpringBoot 集成Netty实现UDP Server

    注:ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动). UDPServer package com.vmware.vCenterEvent.netty; im ...

  10. SaltStack配置salt-api第十二篇

    介绍 SaltStack官方提供有REST API格式的 salt-api项目,将使Salt与第三方系统集成变得尤为简单.本文讲带你了解如何安装配置Salt-API, 如何利用Salt-API获取想要 ...