Spring 属性依赖注入
1.1 属性依赖注入
依赖注入方式:手动装配 和 自动装配
手动装配:一般进行配置信息都采用手动
基于xml装配:构造方法、setter方法
基于注解装配:
自动装配:struts和spring 整合可以自动装配
byType:按类型装配
byName:按名称装配
constructor:构造装配,
auto: 不确定装配。
一.构造方法注入
User.java
public class User { private Integer uid;
private String username;
private Integer age; public User(Integer uid, String username) {
super();
this.uid = uid;
this.username = username;
} public User(String username, Integer age) {
super();
this.username = username;
this.age = age;
}
}
ApplicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 构造方法注入
* <constructor-arg> 用于配置构造方法一个参数argument
name :参数的名称
value:设置普通数据
ref:引用数据,一般是另一个bean id值 index :参数的索引号,从0开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。
type :确定参数类型
例如:使用名称name
<constructor-arg name="username" value="jack"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
例如2:【类型type 和 索引 index】
<constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
<constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
-->
<bean id="userId" class="com.itheima.f_xml.a_constructor.User" >
<constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
<constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
</bean>
</beans>
二.property属性注入
User.java
package com.zk.myspring; public class User {
private Integer uid;
private String username;
private Integer age; public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [uid=" + uid + ", username=" + username + ", age=" + age
+ "]";
}
public User(Integer uid, String username, Integer age) {
super();
this.uid = uid;
this.username = username;
this.age = age;
}
public User() {
super();
} }
Test.java
package com.zk.myspring; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[]args)
{
String path="/com/zk/myspring/ApplicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(path); User user=(User) ac.getBean("UserId");
System.out.println(user);
}
}
ApplicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="UserId" class="com.zk.myspring.User">
<!--
setter方法注入:
普通数据:
<property name="" value="值">
等效
<property name="">
<value>
值
</value>
</property>
引用数据
<property name="" ref="另一个bean">
</property>
等效
<property name="">
<ref bean="" />
</property>
--> <property name="username" value="jack"></property>
<property name="uid" value="1"></property>
<property name="age" value="24"></property>
<!-- p命名空间,对setter方法注入进行简化,替换<property name="属性名">,而是在<bean 属性名="普通值"> -->
</bean>
</beans>
ApplicationContext的p命名空间
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="UserId" class="com.zk.myspring.User" p:uid="1" p:age="24" p:username="zk" p:company-ref="company1"></bean>
<bean id="company1" class="com.zk.myspring.Company" p:name="computer science" p:address="徐州市中国矿业大学计算机科学与技术学院">
</bean>
</beans>
三.集合属性注入
CollData.java
package com.zk.myspringcoll; import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class CollData {
private String[] arrayData;
private List<String> listData;
private Set<String> setData;
private Map<String,String> mapData;
private Properties propersData;
public String[] getArrayData() {
return arrayData;
}
public void setArrayData(String[] arrayData) {
this.arrayData = arrayData;
}
public List<String> getListData() {
return listData;
}
public void setListData(List<String> listData) {
this.listData = listData;
}
public Set<String> getSetData() {
return setData;
}
public void setSetData(Set<String> setData) {
this.setData = setData;
}
public Map<String, String> getMapData() {
return mapData;
}
public void setMapData(Map<String, String> mapData) {
this.mapData = mapData;
} public Properties getPropersData() {
return propersData;
}
public void setPropersData(Properties propersData) {
this.propersData = propersData;
}
@Override
public String toString() {
return "CollData [arrayData=" + Arrays.toString(arrayData)
+ ", \nlistData=" + listData + ", \nsetData=" + setData
+ ", \nmapData=" + mapData + ", \nproperties=" + propersData + "]";
} }
Test.java
package com.zk.myspringcoll; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestColl {
public static void main(String[]args)
{
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
CollData colldata=(CollData) ac.getBean("CollData"); System.out.println(colldata);
}
}
ApplicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--
集合注入都是给<property>添加子标签
数组:<array>
List:<List>
Set:<set>
Map:<map>
Properties:<props><prop key="">value</prop></props> 普通数据:<value>
引用数据:<ref>
-->
<bean id="CollData" class="com.zk.myspringcoll.CollData">
<property name="arrayData">
<array>
<value>DS</value>
<value>DZD</value>
<value>屌丝</value>
<value>吊中吊</value>
</array>
</property>
<property name="listData">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
<value>list4</value>
<value>list5</value>
</list>
</property>
<property name="setData">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
<value>set4</value>
<value>set5</value>
</set>
</property>
<property name="mapData">
<map>
<entry key="1" value="value1"></entry>
<entry key="2" value="value2"></entry>
<entry key="3" value="value3"></entry>
<entry key="4" value="value4"></entry>
<entry key="5" value="value5"></entry>
<!--
<entry>
<key><value>6</value></key>
<value>value6</value>
</entry>
-->
</map>
</property> <property name="propersData">
<props>
<prop key="高富帅">嫐</prop>
<prop key="白富美">嬲</prop>
<prop key="屌丝">挊</prop>
</props>
</property>
</bean>
</beans>
四.自动装配bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 自动装配(用的不多):
byName按名称自动匹配(即要装配的bean中的属性名称和配置中的bean名称相同就会自动装配,如UserService类中的属性和userDAO的bean名称相同就自动装配)
byType按类型自动匹配 (即要装配的bean中的属性类型和配置中的bean的类型相同就会自动装配,如UserService类中的属性类型和userDAO的bean类型相同就自动装配)
-->
<bean id="user" class="com.zk.myspring.User" autowire="default">
</bean> </beans>
自动装配-byName
employee.java
package com.zk.company; public class employee {
private String name;
private Integer age;
private Double Salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return Salary;
}
public void setSalary(Double salary) {
Salary = salary;
}
@Override
public String toString() {
return "employee [name=" + name + ", age=" + age + ", Salary=" + Salary
+ "]";
}
}
employer.java
package com.zk.company; public class employer {
private String name;
private employee e1;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public employee getE1() {
return e1;
}
public void setE1(employee e1) {
this.e1 = e1;
}
@Override
public String toString() {
return "老板"+name + "给员工" + e1.getName() + "发"+ e1.getSalary() + "的薪水";
} }
ApplicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="e1" class="com.zk.company.employee">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="salary" value="100000000000000000"></property>
</bean>
<bean id="employer1" class="com.zk.company.employer" autowire="byName">
<property name="name" value="Boss"></property>
</bean>
</beans>
MainApp.java
package MainApp; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zk.company.employer; public class MainApp {
public static void main(String[]args)
{
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
employer er1=(employer) ac.getBean("employer1");
System.out.println(er1.toString());
}
}
运行结果:
自动装配-byType
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="e1" class="com.zk.company.employee">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="salary" value="100000000000000000"></property>
</bean>
<bean id="employer1" class="com.zk.company.employer" autowire="byType">
<property name="name" value="Boss"></property>
</bean>
</beans>
五.使用注解
<?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"
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">
<context:annotation-config></context:annotation-config>
</beans>
六.扫描包名+配合注解
<?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"
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">
<context:component-scan base-package="com.bjsxt"></context:component-scan>
</beans>
七.内部Bean
employer.java
package com.zk.company; public class employer {
private String name;
private employee e1;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public employee getE1() {
return e1;
}
public void setE1(employee e1) {
this.e1 = e1;
}
@Override
public String toString() {
return "老板"+name + "给员工" + e1.getName() + "发"+ e1.getSalary() + "的薪水";
} }
employee.java
package com.zk.company; public class employee {
private String name;
private Integer age;
private Double Salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return Salary;
}
public void setSalary(Double salary) {
Salary = salary;
}
@Override
public String toString() {
return "employee [name=" + name + ", age=" + age + ", Salary=" + Salary
+ "]";
} }
ApplicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="employee1" class="com.zk.company.employee">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="salary" value="100000000000000000"></property>
</bean>
<bean id="employer1" class="com.zk.company.employer">
<property name="name" value="Boss"></property>
<property name="e1" ref="employee1"></property>
</bean>
</beans>
Main.java
package MainApp; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zk.company.employer; public class MainApp {
public static void main(String[]args)
{
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
employer er1=(employer) ac.getBean("employer1");
System.out.println(er1.toString());
}
}
运行结果:
Spring 属性依赖注入的更多相关文章
- spring属性依赖注入
一.构造方法方式注入 1.项目结构如下: 2.新建Customer类 package hjp.spring.attributeinject; public class Customer { priva ...
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...
- Spring的依赖注入(DI)三种方式
Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...
- 一步一步深入spring(3)--spring的依赖注入方式
对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...
- spring的依赖注入是什么意思
最近学习spring框架,对依赖注入有些模糊,遂上网翻阅资料,做了下列总结,原博客为CSDN 南夏的 spring的依赖注入是什么意思,侵删! Spring 能有效地组织J2EE应用各层的对象.不管是 ...
- Spring.NET依赖注入框架学习--实例化容器常用方法
Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...
- Spring.NET依赖注入框架学习--简单对象注入
Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...
- Spring.NET依赖注入框架学习-- 泛型对象的创建和使用
Spring.NET依赖注入框架学习-- 泛型对象的创建和使用 泛型对象的创建方法和普通对象是一样的. 通过构造器创建泛型对象 下面是一个泛型类的代码: namespace GenericsPlay ...
- Spring中依赖注入的四种方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
随机推荐
- 1080 Graduate Admission
大致题意就是有N个学生,有M个学校,每个学校的名额都是正整数.每个学生可以填K个学校志愿,N个学生一起排名以后,排名高的学生先挑学校,不保护一志愿. 题目要求: 首先,把所有学生按总成绩SUM(GE+ ...
- 转载:openmax基本概念
https://yellowmax.blog.csdn.net/article/details/78080168 https://yellowmax.blog.csdn.net/article/det ...
- 打包Windowsform项目出现File 'Cognex.VisionPro3D.dll' targeting 'AMD64' is not compatible with the project's target platform 'x86'错误
错误信息: 个人理解此错误的大概意思是:打包的文件是64位的但是打包后的文件设置的是32位的,就出现冲突了. 解决方案:选择打包程序项目的属性窗口设置TargetPlatform属性为对应的值,本项目 ...
- vue 动态组件的传值
vue项目开发中会用到大量的父子组件传值,也会用到动态组件的传值,常规子组件获取父组件的传值时,第一次是获取不到的,这时候有两种解决方案 第一种: 父组件向子组件传的是一个json对象,ES6的方法O ...
- JS高级---正则表达式练习身份证号码
写正则表达式, 根据字符串来写正则表达式进行匹配 经验: 1.找规律 2.不要追求完美 身份证的正则表达式 15位或者18位 ([1-9][0-9]{14})|([1-9][0-9]{16}[0 ...
- JDBC添加为null的数据报错
定义实体类的时候定义为了基本数据类型,不能接收null需要改为引用数据类型
- C 库函数 - strcpy()
描述 C 库函数 char *strcpy(char *dest, const char *src) 把 src 所指向的字符串复制到 dest. 需要注意的是如果目标数组 dest 不够大,而源字符 ...
- 数组的concat连接
let arr1 = [1,3,5],arr2 = [2,32,78],arr3 = [];arr3 = arr1.concat(arr2);// arr1 = [1,3,5] arr2 = [2,3 ...
- Java的三种循环:1、for循环 2、while循环 3、do...while循环
Java的三种循环 Java三种循环结构: 1.for循环 2.while循环 3.do...while循环 循环结构组成部分:1.条件初始化语句,2.条件判断语句 , 3.循环体语句,4.条件控制语 ...
- DeepLearningDTU: Building a RNN step by step
exercise 5: Week 5 - Recurrent Neural Networks Building your Recurrent Neural Network - Step by Step