spring注入的四种方式
<?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注入的四种方式的更多相关文章
- 普通java类加入spring容器的四种方式
今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的s ...
- Spring注入值得2种方式:属性注入和构造注入
Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...
- Spring中依赖注入的四种方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
- spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入
三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...
- Spring通过构造方法注入的四种方式
通过构造方法注入,就相当于给构造方法的参数传值 set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选 的,构造注入的优势是通过构造强制依赖关系,不可能实例化不 完全的或无法使用的bean. Me ...
- Spring 注入的两种方式
Spring 的两种注入方式: 1. 属性注入:通过无参构造函数+setter方法注入 2. 构造注入:通过有参的构造函数注入. 优缺点: 1. 属性注入直白易懂,缺点是对于属性可选的时候,很多个构造 ...
- 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式
Spring的属性注入: 构造方法的属性注入 set方法的属性注入
- Spring依赖注入的四种方式
首先,我们需要定义一个Bean的class类: package framework.spring; import org.springframework.beans.BeansException; i ...
- R3注入的四种方式
DLL注入 1.首先要获取想要注入的进程句柄(OpenProcess) 2.从要注入的进程的地址空间中分配一段内存(VirtualAllocEx) 3.往分配的内存位置写入要注入的DLL名称(Writ ...
随机推荐
- delphi 正则表达式
常用正则表式 正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的 ...
- CDOJ 1502 string(简单贪心)
题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...
- Git-从远程仓库克隆
本人拜读了廖雪峰老师关于Git的讲述后整理所得 上次我们讲了先有本地库,后有远程库的时候,如何关联远程库. 现在,假设我们从零开发,那么最好的方式是先创建远程库,然后,从远程库克隆. 首先,登陆Git ...
- spark 作业提交
kafka-topics.sh --describe --zookeeper xxxxx:2181 --topic testkafka-run-class.sh kafka.tools.GetOffs ...
- Python的进程、线程和threading模块
(注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 怀念在学校念书的时候,我不小心触碰到了错误,老师会说:你错了:而我却总是倔强得以为自己没错.我的内心是不屑的,直到在真理面前 ...
- 【Python】IO编程
文件读写 StringIO和BytesIO 操作文件和目录 序列化 学习廖老师的py官网的笔记 1.stream的概念.数据交换通常需要建立两根“水管”. 2.同步IO和异步IO.异步性能高,但是编程 ...
- 如果css足够强大了,你还会用编程的方式实现去实现css已有的功能吗?
现在css3 都出来的,但是其实我由于一些原因,有些css2中都能支持的样式,我都没有使用过.我感觉我真的有必要静下心来,去看看那些东西,看看哪些以前都被忽视掉的. 今天我主要来讲三个对于我们编程经常 ...
- Centos为mysql开启binlog
1.查询mysql配置文件所在位置 2.编辑配置文件/etc/my.cnf 在文件尾部添加: log-bin=/var/lib/mysql/mysql-bin server-id=123454 (5 ...
- LeetCode——largest-rectangle-in-histogram1
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
- jsp选项卡导航实现——模板
效果 刚进来页面的样子 在第二个选项卡上方时 点击后 离开 同样第三个 点击 移走鼠标 代码 <%@ page contentType="text/html;charset=UTF-8 ...